1

I've learned that the .NET CF doesn't support a SmtpClient class. The best it has is the PocketOutlook class which I don't want to use.

I found that OpenNETCF does have a OpenNETCF.Net.Mail Namespace which makes the SmtpClient class available. It unfortunately is only partially implemented and does not support attachments directly: http://community.opennetcf.com/forums/t/11325.aspx

That post suggests that it is still possible to add an attachment using a multi-part MIME message.

Update

After reading ctacke's suggestion to look at the w3.org article I have attempted to change my method like so:

using OpenNETCF.Net.Mail;

    public void EmailPicture(string picLoc)
    {
        var smtpClient = new SmtpClient
                             {
                                 Host = MailProperties.SmtpHost,
                                 Credentials = new SmtpCredential(MailProperties.UserName, MailProperties.Password, MailProperties.Domain),
                                 DeliveryMethod = SmtpDeliveryMethod.Network,
                                 Port = MailProperties.Port
                             };

        var message = new MailMessage();
        var fromAddress = new MailAddress(MailProperties.From);

        message.To.Add(MailProperties.To);
        message.From = fromAddress;
        message.Subject = "Requested Picture";
        message.IsBodyHtml = false;

        message.Headers.Add("MIME-Version", "1.0");
        message.Headers.Add("Content-Type", "multipart/mixed; boundary=\"simple boundary\"");

        var bodyBuilder = new StringBuilder();
        //add text
        bodyBuilder.Append("--simple boundary\r\n");
        bodyBuilder.Append("Content-type: text/plain; charset=us-ascii\r\n\r\n");
        bodyBuilder.Append("Requested Picture is attached.\r\n\r\n");
        //add attachment
        bodyBuilder.Append("--simple boundary\r\n");
        bodyBuilder.Append("Content-type: image/jpg;\r\n\r\n");
        var fs = new FileStream(picLoc, FileMode.Open, FileAccess.Read);
        var picData = new byte[fs.Length];
        fs.Read(picData, 0, picData.Length);
        bodyBuilder.Append(picData);
        bodyBuilder.Append("\r\n\r\n");
        bodyBuilder.Append("--simple boundry--\r\n");

        message.Body = bodyBuilder.ToString();

        smtpClient.Send(message);
    }

The email I get ends up looking like this:

--simple boundary Content-type: text/plain; charset=us-ascii

Requested Picture is attached.

--simple boundary Content-type: image/jpg;

System.Byte[]

--simple boundry--

Do I have a format issue? or a missing header?

nitewulf50
  • 530
  • 1
  • 4
  • 17
  • I've never done this client side, but is there any way you could hand this off to a stored procedure that could send the email? – Robbie Dee Dec 05 '12 at 23:38
  • I hadn't considered a stored procedure. I wasn't aware that one could be written to send email. I am contemplating writing a webservice to pass the attachment off to which would then send the email. It would be a much cleaner implementation if the .NET CF didn't suck so much though. – nitewulf50 Dec 06 '12 at 03:46
  • Why you do not want to use PocketOutlook? It is standard on all Windows Mobile Pro devices and an easy to use API. – josef Dec 06 '12 at 09:51
  • Yes, the CF isn't as fully featured as its more bloated desktop compatriot unfortunately... – Robbie Dee Dec 06 '12 at 12:01

2 Answers2

1

As the forum post you point to says, Attachments aren't implemented in the OpenNETCF Mail code. We just never got around to doing it. An email with attachments is simply multipart MIME message, which is not terribly complex and is covered in RFC 1341. You would have to extend the code to build up a multipart MIME message, and then set the appropriate content-type.

ctacke
  • 66,480
  • 18
  • 94
  • 155
0

I did a crude work around by making my own email class that has method and property names that are the exact same names used by .NET's SMTP.

Inside my class, if there is an attachment, it is copied to our Server when my email class's Send is called. My email is also sent only to this same Server, to a minimal application that sits and listens for messages along the TCP connection.

Once the Server receives the information, it has the ability to construct an SMTP email, uses the file copied over to create an attachment (if the Attachment Path is NULL, there is no attachment), and send the email to the address specified in my email class's To, CC, and/or BCC fields.

It is a longer way about getting it done, but it works like a champ!

The code is rather large, but this is more because I want it to work with other things in the Suite of applications I write. Also, there are obviously two projects: One for Mobile and one for Windows or Web (the Server).