C# add image to email body

£可爱£侵袭症+ 提交于 2019-12-11 02:50:59

问题


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

回答1:


Html body as

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


来源:https://stackoverflow.com/questions/25139157/c-sharp-add-image-to-email-body

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!