Tarasov
Tarasov

Reputation: 3685

How I can use a Image as Background for my html mail in ASP.NET

I have a Question to send mails with html and attachment as an embedded image. I don't know how I can use the Picture from my images folder as Background for a div.

here my code:

SmtpMail oMail = new SmtpMail("TryIt");
            SmtpClient oSmtp = new SmtpClient();

            oMail.From = "[email protected]";

            oMail.To = "<my email>";

            oMail.Subject = "test";

            SmtpServer oServer = new SmtpServer("<smtp server>");

            try
            {
               // Attachment header = oMail.AddAttachment("d:\\mail_header.jpg");
                Attachment header = oMail.AddAttachment("images/mail_header.jpg"); // this don't work
                Attachment oAttachment = oMail.AddAttachment("d:\\bg_content.jpg");
                Attachment Footer = oMail.AddAttachment("d:\\mail_footer.jpg");

                string contentID_header = "header";
                header.ContentID = contentID_header; 

                string contentID = "test001@host";
                oAttachment.ContentID = contentID;

                string contentID_footer = "footer";
                Footer.ContentID = contentID_footer; 

//how I can use a pic as background

                oMail.HtmlBody = "<html><body>"+
                                    "<div style='background-image:url(" + contentID_header + ");width: 800px;height: 50px'></div>" +
                                    "<div><img src=\"cid:" + contentID + "\"></div>" +
                                    "<div><img src=\"cid:" + contentID_footer + "\"></div>" +
                                    "</body></html>";

                oSmtp.SendMail(oServer, oMail); 

            }
            catch (Exception ep)
            {
                txtSimulate.Text = ep.Message; 
            } 

Upvotes: 1

Views: 7464

Answers (4)

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98810

First of all, I found this project.

But basicly, "embed" the image in the email message itself, you'll want to add it as a Linked Resource and reference the attached resource in the email's HTML.

Upvotes: 1

Carlos Landeras
Carlos Landeras

Reputation: 11063

I use this code, it is working nice using LinkedResources with Alternateviews. Much better than hardcoding CIDS.

Check it:

System.Net.Mail.MailMessage Mensaje = new System.Net.Mail.MailMessage("[email protected]",DireccionCorreo);

System.Net.Mail.AlternateView htmlView = System.Net.Mail.AlternateView.CreateAlternateViewFromString(Body, null, "text/html");


System.Net.Mail.LinkedResource logo = new System.Net.Mail.LinkedResource("logoimage.jpg");
logo.ContentId = "logoimage";
htmlView.LinkedResources.Add(logo);

System.Net.Mail.LinkedResource logoExchange = new System.Net.Mail.LinkedResource("logoexchange.png");
logoExchange.ContentId = "logoexchange";
htmlView.LinkedResources.Add(logoExchange);

System.Net.Mail.LinkedResource tut1 = new System.Net.Mail.LinkedResource(Application.StartupPath + "/OutlookGuide/tut1.jpg");
tut1 .ContentId = "tut1";
htmlView.LinkedResources.Add(tut1 );

System.Net.Mail.LinkedResource tut2 = new   System.Net.Mail.LinkedResource(Application.StartupPath + "/OutlookGuide/tut2.jpg");
tut2.ContentId = "tut2";
htmlView.LinkedResources.Add(tut2);


Mensaje.AlternateViews.Add(htmlView);

Upvotes: 3

Ravi Vanapalli
Ravi Vanapalli

Reputation: 9942

You would have to give full path of the image .

Upvotes: 1

Kuldeep Singh
Kuldeep Singh

Reputation: 471

I do not know about asp.net but with PHP we do it by converting the image to Base64 encoding and then using

<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIA..." />

Upvotes: 1

Related Questions