1

I am writing a Windows service which will trigger emails. The functionality can also be triggered from a Windows application using the same piece of code.

The emails which we trigger should be having a signature with a logo, which is in a resource file. The emails are getting triggered fine but the image is not showing up, but the placeholder for image is. This is the code I am using:

try
{
    Bitmap imgAttachment = Project.Shared.Properties.Resources.Image1;
    using (MemoryStream mstream = new MemoryStream())
    {
        ContentType type = new ContentType()
        {
            MediaType = MediaTypeNames.Image.Jpeg,
            Name = "attachment"
        };
        imgAttachment.Save(mstream, ImageFormat.Jpeg);
        Attachment sigAttachment = new Attachment(mstream, type);
        string ContentId = "imagelogo";
        sigAttachment.ContentId = ContentId;
        sigAttachment.ContentDisposition.Inline = true;
        sigAttachment.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
        message.Attachments.Add(sigAttachment);

        message.Body = Body + "<htm><body> <br/><br/><table border='0' cellpadding='0'     
            cellspacing='0' width='1000'>  <tr><td>&nbsp;</td></tr>  <tr>    <td 
            rowspan='8' valign='top' align='left'><img alt = 'Logo' src=\"cid:" + 
            ContentId + "\"></td> <td width='1000' height='46' valign='top'> " + 
            SignatureLine1 + " </td>  </tr> <tr> " + SignatureLine2 + " </tr> <tr> " + 
            SignatureLine3 + " </tr> <tr> " + SignatureLine4 + " </tr> </table> 
            </body></html>";
        client.Send(message);
    }
}
catch {}

1

Can some one help me with this? Am I missing something?

djv
  • 15,168
  • 7
  • 48
  • 72
Jinith
  • 439
  • 6
  • 16
  • I suspect there's a slight difference between attachments and embedded resources, and this may be considered a duplicate of this: http://stackoverflow.com/questions/18358534/send-inline-image-in-email – David Mar 13 '14 at 11:56
  • I did try that as well, but I think my problem is a bit different as even the post you mentioned, gave the same output. (Refer attachment). The Image was not picked up fine. – Jinith Mar 13 '14 at 12:32
  • What if you set mstream.Position to 0 after saving the image? – jstedfast Mar 13 '14 at 21:10

1 Answers1

0

The problem is that you are not generating the correct MIME structure. The HTML body and the image attachment need to be part of a multipart/related and the code you are writing probably creates a multipart/mixed (I haven't actually tested your code, but having dug around in Mono's System.Net.Mail implementation, it seems your code would generate a multipart/mixed and not a multipart/related, which is what you want).

I don't have a whole lot of experience with System.Net.Mail (it sucks so I wrote my own library), but what you need to do is use an AlternateView for the HTML message body and add the image as a LinkedResource to that AlternateView (see the AlternateView.LinkedResources property).

jstedfast
  • 35,744
  • 5
  • 97
  • 110