【问题标题】:C# add image to email bodyC# 将图像添加到电子邮件正文
【发布时间】:2014-09-28 03:15:09
【问题描述】:

此代码发送带有附件“Logo.JPG”的电子邮件,但未将其附加到电子邮件正文中。我只是得到图像占位符。 如何将图像添加到消息的文本中?

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);

【问题讨论】:

标签: c# image html-email


【解决方案1】:

HTML 正文为

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-10-25
    • 2011-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-12-17
    • 2011-07-11
    • 1970-01-01
    • 2019-04-16
    相关资源
    最近更新 更多