【发布时间】:2016-08-01 12:03:52
【问题描述】:
我使用以下代码发送带有嵌入图像的电子邮件,但是它导致了一个问题,即某些消息与以前消息中的图像一起发送。
using (MailMessage msg = new MailMessage())
{
msg.From = new MailAddress(mailFrom);
msg.To.Add(mail);
msg.Subject = assunto;
msg.Body = MSG;
msg.IsBodyHtml = true;
//create Alternative HTML view
AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MSG, null, "text/html");
//Add Image - logo
LinkedResource theEmailImage = null;
switch (TYPE_MAIL)
{
case "1":
case "2":
case "3":
theEmailImage = new LinkedResource(ImagePath + "logo_rgb.jpg");
break;
default:
theEmailImage = new LinkedResource(ImagePath + "header_report_pt_700.jpg");
break;
}
theEmailImage.ContentId = "logo";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);
//Add Images - email (limit 8)
LinkedResource img1;
int count = 0;
switch (TYPE_MAIL)
{
case "1":
string image = "";
for (int i = 0; i < 3; i++)
{
try
{
image = Images[i].ToString();
img1 = new LinkedResource(AttachmentsPath + image);
}
catch (Exception)
{
img1 = new LinkedResource(AttachmentsPath + "empty.png");
}
img1.ContentId = "img" + (i + 1);
htmlView.LinkedResources.Add(img1);
}
break;
case "2":
foreach (string imagem in Images)
{
try
{
img1 = new LinkedResource(AttachmentsPath + imagem);
}
catch (Exception)
{
img1 = new LinkedResource(AttachmentsPath + "empty.png");
}
img1.ContentId = "img" + count;
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(img1);
count++;
}
break;
default:
break;
}
//Add views to the Email Message (all images)
msg.AlternateViews.Add(htmlView);
try
{
//RELAY (configuration on App.config)
using (SmtpClient smtp = new SmtpClient(ServidorSmtp))
{
smtp.Send(msg);
result = "OK";
}
}
catch (Exception ex)
{
cLog.addItemToLog("E-MAIL FAILED! " + DateTime.Today.ToString("dd-MM-yyyy") + " - " + ex);
}
}
然后,由于我的假设是问题与未处理的对象有关,因此我将其稍微更改为:
using (var msg = new MailMessage())
{
msg.From = new MailAddress(mailFrom);
msg.To.Add(mail);
msg.Subject = assunto;
msg.Body = MSG;
msg.IsBodyHtml = true;
//create Alternative HTML view
using (AlternateView htmlView = AlternateView.CreateAlternateViewFromString(MSG, null, "text/html"))
{
//Add Image - logo
LinkedResource theEmailImage = null;
switch (TYPE_MAIL)
{
case "1":
case "2":
case "3":
theEmailImage = new LinkedResource(ImagePath + "logo_rgb.jpg");
break;
default:
theEmailImage = new LinkedResource(ImagePath + "header_report_pt_700.jpg");
break;
}
theEmailImage.ContentId = "logo";
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(theEmailImage);
//Add Images - email (limit 8)
LinkedResource img1;
int count = 0;
switch (TYPE_MAIL)
{
case "1":
string image = "";
for (int i = 0; i < 3; i++)
{
try
{
image = Images[i].ToString();
img1 = new LinkedResource(AttachmentsPath + image);
}
catch (Exception)
{
img1 = new LinkedResource(AttachmentsPath + "empty.png");
}
img1.ContentId = "img" + (i + 1);
htmlView.LinkedResources.Add(img1);
}
break;
case "2":
foreach (string imagem in Images)
{
try
{
img1 = new LinkedResource(AttachmentsPath + imagem);
}
catch (Exception)
{
img1 = new LinkedResource(AttachmentsPath + "empty.png");
}
img1.ContentId = "img" + count;
//Add the Image to the Alternate view
htmlView.LinkedResources.Add(img1);
count++;
}
break;
default:
break;
}
//Add views to the Email Message (all images)
msg.AlternateViews.Add(htmlView);
try
{
//RELAY (configuration on App.config)
using (var smtp = new SmtpClient(ServidorSmtp))
{
smtp.Send(msg);
result = "OK";
}
}
catch (Exception ex)
{
cLog.addItemToLog("E-MAIL FAILED! " + DateTime.Today.ToString("dd-MM-yyyy") + " - " +
ex);
}
}
}
现在,有些电子邮件在发送时没有嵌入图像,<img>标签存在但图像不存在。
现在我的假设是邮件服务器阻止了一些嵌入的图像,但我不知道如何测试。
有什么建议吗?
更新
这是邮件 HTML 的要点(必须编辑邮件以删除机密信息,但 Outlook 不允许我将其保存回 .EML):https://gist.github.com/julianonunes/477362fe505bd3e49b31038472ae7403
【问题讨论】:
标签: c# asp.net mailmessage