Click here to Skip to main content
11,485,564 members (76,542 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 21:04pm

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 375
1 Maciej Los 365
2 C#developer.asp 204
3 Mehdi Gholam 190
4 OriginalGriff 149
0 Sergey Alexandrovich Kryukov 8,819
1 OriginalGriff 7,982
2 Sascha Lefèvre 3,199
3 Maciej Los 2,841
4 Richard Deeming 2,370

Advertise | Privacy | Mobile
Web04 | 2.8.150520.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