2

I want to send email using System.Web.Mail.MailMessage along with System.Web.Mail.SmtpMail class. I know that this is a deprecated class but using System.Net.Mail is not an option for me.

So far I am able to send html formatted mail but I am not able to embed image to my email while sending.

This is what I've tried so far;

private static void SendMailMethod(string mailServer, int mailServerPort, string userName, string password)
    {
        const string SMTP_SERVER = "http://schemas.microsoft.com/cdo/configuration/smtpserver";
        const string SMTP_SERVER_PORT =
            "http://schemas.microsoft.com/cdo/configuration/smtpserverport";

        const string SEND_USING = "http://schemas.microsoft.com/cdo/configuration/sendusing";

        const string SMTP_USE_SSL = "http://schemas.microsoft.com/cdo/configuration/smtpusessl";


        const string SMTP_AUTHENTICATE =
           "http://schemas.microsoft.com/cdo/configuration/smtpauthenticate";


        const string SEND_USERNAME =
            "http://schemas.microsoft.com/cdo/configuration/sendusername";
        const string SEND_PASSWORD =
            "http://schemas.microsoft.com/cdo/configuration/sendpassword";


        var mailMessage = new System.Web.Mail.MailMessage();

        mailMessage.Fields[SMTP_SERVER] = mailServer;
        mailMessage.Fields[SMTP_SERVER_PORT] = mailServerPort;
        mailMessage.Fields[SEND_USING] = 2;
        mailMessage.Fields[SMTP_USE_SSL] = false;
        mailMessage.Fields[SMTP_AUTHENTICATE] = 0;
        mailMessage.Fields[SEND_USERNAME] = userName;
        mailMessage.Fields[SEND_PASSWORD] = password;

        mailMessage.From = "abc@xyz.com";
        mailMessage.To = "abc@xyz.com";
        mailMessage.Subject = "Test mail:";
        mailMessage.BodyFormat = MailFormat.Html;

        var mailAttachment = new MailAttachment("E:\\imageToEmbed.jpg");
        mailMessage.Attachments.Add(mailAttachment);
        mailMessage.Attachments.Add(new MailAttachment("E:\\TestAttachmentFile.txt"));

        var htmlBody =
            "<!DOCTYPE html PUBLIC \" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" +
            "<html xmlns = \"http://www.w3.org/1999/xhtml\" >" +
            " <head >" +
            "<meta http - equiv = \"content-type\" content = \"text/html; charset=UTF-8\" />" +
            "</head >" +
            "<body style = \"font-family: Segoe UI; text-align:left;\" >" +
            "Following is an embedded image:" +
            "<br />" +
            "<img alt = \"\" src = \"imageToEmbed.jpg\" />" +
            "</body >" +
            "</html >";

        mailMessage.Body = htmlBody;

        try
        {
            SmtpMail.Send(mailMessage);

            Console.WriteLine("Mail sent");
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error:" + ex.ToString());
            throw ex;
        }
    }

This code sends email but instead of showing image, the mail shows small square with a cross in it. How can I embed this "imageToEmbed.jpg" to my email.

Kamalesh Wankhede
  • 1,475
  • 16
  • 37
  • 2
    your question is answered here http://stackoverflow.com/questions/18358534/send-inline-image-in-email – photowalker May 16 '16 at 07:39
  • @photowalker, The answer in link you shared does not work for me. Because I'm not using System.Net.Mail namspace. I'm using System.Web.Mail.MailMessage class to send email – Kamalesh Wankhede May 16 '16 at 07:43
  • Why are you restricted to that obsolete class? Are you on .Net 1.1? – rene May 16 '16 at 07:47
  • I'm restricted to this obsolete class because the new class in System.Net.Mail does not allow me to send mail using SSL security. I already asked about this [here](http://stackoverflow.com/questions/37070767/send-e-mail-using-ssl) – Kamalesh Wankhede May 16 '16 at 07:51

2 Answers2

2

You can't achieve that with the classes System.Web.Mail because that implementation abstracts the access to a lot of features of the underlying Collaboration Data Objects component.

Instead of using the wrapper offered by System.Web.Mail.Message you better switch to using the CDO COM object directly. Add a reference to Microsoft CDO for Windows 2000 Library from the COM tab in the reference dialog.

After that you can use the following code to create and send an html email with embedded images:

var cdo = new CDO.Message();
// configuration
var cfg = cdo.Configuration;
cfg.Fields[SMTP_SERVER].Value = "smtp.server.com";
cfg.Fields[SMTP_SERVER_PORT].Value = 22;
cfg.Fields[SEND_USING].Value = 2;
cfg.Fields[SMTP_USE_SSL].Value = true;
cfg.Fields[SMTP_AUTHENTICATE].Value = 1;
cfg.Fields[SEND_USERNAME].Value = "user@example.com";
cfg.Fields[SEND_PASSWORD].Value = "password";
cfg.Fields.Update();

cdo.To = "awesome@example.com";
cdo.Sender = "me@example.com";

// attachment
var cdoatt = cdo.AddAttachment("file:///E:/imageToEmbed.jpg");
//this is why System.Web.Mail can't embed images
cdoatt.Fields["urn:schemas:mailheader:content-id"].Value = Guid.NewGuid().ToString("N");
cdoatt.Fields.Update();

// get a reference to out content-id field
var cid = cdoatt.Fields["urn:schemas:mailheader:content-id"];
// notice the special layout of SRC on the image tag, 
//it will be somethong like CID:123456789abcdef
cdo.HTMLBody = @"<HTML><BODY><B>CDO</B><BR /> <IMG SRC=""cid:" + cid.Value + @"""/></BODY></HTML>";

cdo.Send();

Notice how you can set the Content-ID header here on the Attachment. That is needed so you can use the Content-ID in the SRC attribute of your image.

A typical mailmessage will look

MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0001_01D1AF72.C8AE8C70"
Content-Transfer-Encoding: 7bit
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0002_01D1AF72.C8AE8C70"


------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

plain cdo 

------=_NextPart_001_0002_01D1AF72.C8AE8C70
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<HTML><BODY><B>plain cdo</B> <IMG SRC="cid:42"/></BODY></HTML>
------=_NextPart_001_0002_01D1AF72.C8AE8C70--

------=_NextPart_000_0001_01D1AF72.C8AE8C70
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-ID: <42>
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMA

Notice the Content-ID (here 42).

When using the plain System.Web.Mail classes your raw email will look like this:

MIME-Version: 1.0
Content-Type: multipart/mixed;
    boundary="----=_NextPart_000_0000_01D1AF70.59FC25F0"
X-Mailer: Microsoft CDO for Windows 2000
Content-Class: urn:content-classes:message
Importance: normal
Priority: normal
X-MimeOLE: Produced By Microsoft MimeOLE V10.0.10011.16384

This is a multi-part message in MIME format.

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: multipart/alternative;
    boundary="----=_NextPart_001_0001_01D1AF70.59FC25F0"


------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/plain;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

Following is an embedded image:

  _____  

test afterit

------=_NextPart_001_0001_01D1AF70.59FC25F0
Content-Type: text/html;
    charset="iso-8859-1"
Content-Transfer-Encoding: 7bit

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns = "http://www.w3.org/1999/xhtml" > <head ><meta http - equiv = "content-type" content = "text/html; charset=UTF-8" /></head ><body style = "font-family: Segoe UI; text-align:left;" ><i>Following is an embedded image:</i><br /><img alt = "" src ="<file:///file.png>"/><hr><b>test afterit</b></body ></html >
------=_NextPart_001_0001_01D1AF70.59FC25F0--

------=_NextPart_000_0000_01D1AF70.59FC25F0
Content-Type: image/png;
    name="file.png"
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
    filename="file.png"

iVBORw0KGgoAAAANSUhEUgAAADQAAABLCAMAAAAI0l

As you can see, there is no Content-ID header added and there is no code-path from the .Net implementation to add that header.

rene
  • 41,474
  • 78
  • 114
  • 152
  • Thanks for such detailed response. I have tried using your solution but the email is still not showing image. [This](http://txt.do/5b64x) is what my raw email looks like now. – Kamalesh Wankhede May 16 '16 at 13:41
  • @krw12572 Hmm, strange, your content looks OK and even with your mark-up I get an embedded image. I'm using gmail to test it btw. – rene May 16 '16 at 13:58
  • Thanks! The code is working. Maybe it was issue related to email server before. Its working with gmail now. – Kamalesh Wankhede May 17 '16 at 05:38
0

It might not find the image in the specified path or it might be a permission thing hence the broken image thingy. You need to check that first but if you would use base64 you will have the image in a string.

Try changing the image to a base64 link to a free online converter page.

This link might help as well c# embed base64 image for emailing.

Community
  • 1
  • 1
photowalker
  • 353
  • 1
  • 8