Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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.

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


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");
fileMsg.AlternateViews.Add(htmlView);
return fileMsg;
        }
Posted

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900