C#将图像添加到电子邮件正文 [英] C# add image to email body

查看:145
本文介绍了C#将图像添加到电子邮件正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码会发送一封附有Logo.JPG附件的电子邮件,但不会将其附加到电子邮件正文中。我只是得到了图像占位符。
如何将图像添加到邮件文本中?

This code sends an email with the attachment "Logo.JPG" but does not attach it to the body of the email. I just get the image place holder. How can i add the image to the text of the message?

string emailType = "NewMember";
string sMessage = GetData.emailText(emailType);
string sEmail = GetData.userEmails(userName);
string sSubject = GetData.emailSubject(emailType);
string sImage = System.Web.HttpContext.Current.Server.MapPath("~/images/logo.jpg");
SmtpClient smtpClient = new SmtpClient();
string htmlBody = "<html><body>Dear " + userName + sMessage + "<br/><br/><img src=" + sImage + "></body></html></body></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
   (htmlBody, null, MediaTypeNames.Text.Html);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
FileStream fileToStream = new FileStream(System.Web.HttpContext.Current.Server.MapPath("~/images/logo.jpg"), FileMode.Open, FileAccess.Read);
Attachment att = new Attachment(fileToStream, "Logo.jpg", MediaTypeNames.Image.Jpeg);

att.ContentDisposition.Inline = true;
MailAddress sFrom = new MailAddress("info@inveshub.com");
MailAddress sTo = new MailAddress(sEmail);
mail.From = sFrom;
mail.To.Add(sTo);
mail.Subject = sSubject;
mail.Attachments.Add(att);
mail.Body = String.Format(
           htmlBody);
mail.IsBodyHtml = true;
//  mail.Attachments.Add(att);
smtpClient.Send(mail);


推荐答案

Html正文为

 string htmlBody = "<html><body>Dear " + userName + sMessage + "<br/><br/><img src=cid:sImage /></body></html></body></html>"

这篇关于C#将图像添加到电子邮件正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆