Click here to Skip to main content
13,191,400 members (77,584 online)
Rate this:
Please Sign up or sign in to vote.
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
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web04 | 2.8.171017.1 | Last Updated 3 Dec 2012
Copyright © CodeProject, 1999-2017
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