HTML格式的邮件能够使用所有html/css使得邮件更丰富,比如现在很多newsletter 都是使用的html邮件. 今天试了一下,如何把图片嵌入到html中呢?
方法一,你的图片host到了internet上
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
message.Body = "<div style='border:thin solid #00FFFF'> <p>Dear xixifusi,</p> <p>We wanted to take this opportunity to say thank you for your business and to wish you a wonderful holiday season and a very happy New Year.</p> <img alt='' src='http://i.microsoft.com/global/en-us/homepage/PublishingImages/Header/IELogo.png' /></div>";
MailAddressCollection tos = new MailAddressCollection();
message.To.Add(new MailAddress("xixifusi@gmail.com"));
message.From = new MailAddress("xixifusi@gmail.com", "Ultraman");
message.Subject = "Hello xixifusi";
smtpClient.Credentials = new NetworkCredential("xixifusi@gmail.com", "123456", "");
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
smtpClient.Send(message);
方法二, 把本地图片嵌入到html邮件中
SmtpClient smtpClient = new SmtpClient();
MailMessage message = new MailMessage();
message.IsBodyHtml = true;
MailAddressCollection tos = new MailAddressCollection();
message.To.Add(new MailAddress("xixifusi@gmail.com"));
message.From = new MailAddress("xixifusi@gmail.com", "Ultraman");
message.Subject = "Hello xixifusi";
smtpClient.Credentials = new NetworkCredential("xixifusi@gmail.com", "123456", "");
smtpClient.Host = "smtp.gmail.com";
smtpClient.Port = 587;
smtpClient.EnableSsl = true;
AlternateView htmlView = AlternateView.CreateAlternateViewFromString("<div style='border:thin solid #00FFFF'> <p>Dear xixifusi,</p> <p>We wanted to take this opportunity to say thank you for your business and to wish you a wonderful holiday season and a very happy New Year.</p> <img alt='' src='cid:logo' /></div>", null, "text/html");
LinkedResource logo = new LinkedResource(@"D:IElogo.png");
logo.ContentId = "logo";
htmlView.LinkedResources.Add(logo);
message.AlternateViews.Add(htmlView);
smtpClient.Send(message);