【发布时间】:2014-05-03 06:55:33
【问题描述】:
我正在尝试从应用程序发送一封自动电子邮件。
我遇到的主要问题是图片不显示。
其次,我应该使用的实际资源是我只能通过 URL 访问的 PSD 文件。
附加代码来自之前的实验,它附加了我从第 3 行对话框中选择的文件
那么谁能告诉我哪里出错了?
try{
OpenFileDialog diag = new OpenFileDialog();
diag.ShowDialog();
FileInfo inf = new FileInfo(diag.FileName);
string htmlBody = "<html><header><h1>TestPic</h1><br><img src=\"cid:filename\"></header></html>";
AlternateView avHtml = AlternateView.CreateAlternateViewFromString
(htmlBody, null, MediaTypeNames.Text.Html);
LinkedResource inline = new LinkedResource(@inf.FullName, MediaTypeNames.Image.Jpeg);
inline.ContentId = Guid.NewGuid().ToString();
inline.ContentLink = new Uri("PSD_Url.com");
avHtml.LinkedResources.Add(inline);
MailMessage mail = new MailMessage();
mail.AlternateViews.Add(avHtml);
Attachment att = new Attachment(inf.FullName);
att.ContentDisposition.Inline = true;
mail.From = new MailAddress("from@gmail.com");
mail.To.Add("to@gmail.com");
mail.Subject = "Test Email";
mail.Body = String.Format(
"<h3>TEST Has Sent You A Mail</h3>" +
@"<img src=""cid:{0}"" />", inline.ContentId);
mail.IsBodyHtml = true;
mail.Attachments.Add(att);
mail.BodyEncoding = Encoding.Unicode;
mail.HeadersEncoding = Encoding.Unicode;
mail.SubjectEncoding = Encoding.Unicode;
SmtpClient smp = new SmtpClient("mailhost.com", 80);
smp.Credentials = new System.Net.NetworkCredential(@"from@gmail.com", @"password");
smp.DeliveryFormat = SmtpDeliveryFormat.International;
smp.Send(mail);
}
catch(SmtpException sEx)
{
throw sEx;
}
【问题讨论】:
-
如果你使用
src="cid:filename",那么你不应该使用inline.ContentId = "filename";来链接两者吗?
标签: c# email html-email