Click here to Skip to main content
11,578,739 members (60,953 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: Embedded C# HTML email
If I send emails using a MailDefinition object and use html Alternateview without embedded objects , everything works fine. But as soon as I add embedded objects I get a blank email. I need to send embedded images with the alternate html view. I've been struggling with this problem for days and would appreciate it if somebody can suggest a solution.

The sample code is given below. If I comment the line "md.EmbeddedObjects.Add(emo);" the email works fine.

protected void sendEMail_Click(object sender, System.EventArgs e)
        MailMessage msg = CreateMessage();
            SmtpClient sc = new SmtpClient();

public MailMessage CreateMessage()
        MailDefinition md = new MailDefinition();
        md.BodyFileName = “htmltemplate.htm”;
        md.From = sourceFrom.Text;
        md.Subject = sourceSubject.Text;
        md.IsBodyHtml = true;
ListDictionary replacements = new ListDictionary();
        replacements.Add("<%To%>", sourceTo.Text
        replacements.Add("<%From%>", md.From);
            EmbeddedMailObject emo = new EmbeddedMailObject();
            emo.Path = "image1.png";//have tried Server.Mappath(“~/image1.png”)
            emo.Name = "hdr";
md.EmbeddedObjects.Add(emo);// With this line I get a blank email. If the line is removed the email works fine.
MailMessage fileMsg;
            fileMsg = md.CreateMailMessage(sourceTo.Text, replacements, this);
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(fileMsg.Body, null, "text/html");
return fileMsg;
Posted 2-Dec-12 20:04pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Peter Leow 135
1 Frankie-C 115
2 OriginalGriff 113
3 Richard MacCutchan 85
4 Afzaal Ahmad Zeeshan 85
0 OriginalGriff 933
1 Sergey Alexandrovich Kryukov 740
2 Abhinav S 573
3 F-ES Sitecore 420
4 Dave Kreskowiak 419

Advertise | Privacy | Mobile
Web04 | 2.8.150603.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100