Click here to Skip to main content
Rate this: bad
good
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();
            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 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 OriginalGriff 250
1 Jochen Arndt 155
2 PIEBALDconsult 150
3 DamithSL 125
4 Afzaal Ahmad Zeeshan 120
0 OriginalGriff 5,695
1 DamithSL 4,591
2 Maciej Los 4,012
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,190


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