【发布时间】: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);
【问题讨论】:
-
Attachment 假设将文件附加到电子邮件中,而不是嵌入。如果要嵌入,则应将其添加到 htmlBody。
标签: c# image html-email