Yas*_*ser 8 html c# email smtpclient
我正在使用C#使用C#发送邮件SmtpClient.我在发送邮件之前做了以下事情.
var mailMessage = new MailMessage();
model.ToAddresses.ForEach(to => mailMessage.To.Add(to));
mailMessage.Subject = "Test Email - By Yasser";
mailMessage.Body = String.Format("{0}{1}{2}",
"<html><body>",
GetEmailContent(model),
"</body></html>");
mailMessage.IsBodyHtml = true;
return MailService.SendEmail(mailMessage);
Run Code Online (Sandbox Code Playgroud)
以下是我的MailService类:
public class MailService
{
public static bool SendEmail(MailMessage mailMessage)
{
var smtpClient = new SmtpClient();
try
{
smtpClient.Send(mailMessage);
return true;
}
catch(Exception exp)
{
return false;
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在当我发送邮件时,邮件会被发送,当我按下查看源时,这就是我在Outlook中看到的邮件内容.以下是带有查看源的电子邮件的内容(显然我只保留了部分图像数据)
<html>
<body>
<h1>Test</h1>
<h2>Hello World</h2>
<h3>Missing close h3 tag</h3>
<p>
<a href="www.google.com">
<img src="data:image/gif;base64,/9j/4AAQSkZJRgABAgEAYABgAAD/4Q8HRXhpZgAAT" />
</a>
</p>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
因此,这在邮件中显得破碎(图像),但是当我复制此源并将其粘贴到编辑器中并使用浏览器打开文件时,一切看起来都很好(甚至是图像).
更新:添加了Outlook的邮件图像

有任何想法吗 ????
Yas*_*ser 16
这是我尝试和工作的东西,在outlook,thunderbird和gmail中测试过.工作良好 !
您可能想查看我提到的以下资源,以实现此目的:
示例代码:
// we need to use the prefix 'cid' in the img src value
string emailReadyHtml = string.empty;
emailReadyHtml += "<p>Hello World, below are two embedded images : </p>";
emailReadyHtml += "<img src=\"cid:yasser\" >";
emailReadyHtml += "<img src=\"cid:smile\" >";
MailMessage mailMessage = new MailMessage();
mailMessage.To.Add("yasser@mail.yy");
mailMessage.From = new MailAddress("info@mail.yy", "Info");
mailMessage.Subject = "Test Mail";
mailMessage.IsBodyHtml = true;
string image1Path = HttpContext.Current.Server.MapPath("~/Content/images/yasser.jpg");
byte[] image2Bytes = someArrayOfByte;
ContentType c = new ContentType("image/jpeg");
// create image resource from image path using LinkedResource class.
LinkedResource linkedResource1 = new LinkedResource(imagePath);
linkedResource1.ContentType = c ;
linkedResource1.ContentId = "yasser";
linkedResource1.TransferEncoding = TransferEncoding.Base64;
// the linked resource can be created from bytes also, which may be stored in database (which was my case)
LinkedResource linkedResource2 = new LinkedResource(new MemoryStream(image2Bytes));
linkedResource2.ContentType = c;
linkedResource2.ContentId = "smile";
linkedResource2.TransferEncoding = TransferEncoding.Base64;
AlternateView alternativeView = AlternateView.CreateAlternateViewFromString(emailReadyHtml, null, MediaTypeNames.Text.Html);
alternativeView.ContentId = "htmlView";
alternativeView.TransferEncoding = TransferEncoding.SevenBit;
alternativeView.LinkedResources.Add(linkedResource1) ;
alternativeView.LinkedResources.Add(linkedResource2);
mailMessage.AlternateViews.Add(alternativeView);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Send(mailMessage);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6807 次 |
| 最近记录: |