我使用此方法发送包含正文中嵌入图像的电子邮件,除了在Outlook中,它几乎适用于所有客户端。它没有显示图像前景,但它在Outlook网站中显示。有人帮忙吗? 图像位于名为images的目录中 在这里你有方法
public static void SendNotificationMail(string subject, string message, string emailTo, string ccTo, string bccTo, string filename)
{
try
{
// Create a message and set up the recipients.
MailMessage msg = new MailMessage();
msg.From = new MailAddress(AppConfig.EmailFrom, AppConfig.EmailFromDisplayName);
msg.To.Add(emailTo);
if (!string.IsNullOrEmpty(ccTo))
{
msg.CC.Add(ccTo);
}
if (!string.IsNullOrEmpty(bccTo))
{
msg.Bcc.Add(bccTo);
}
msg.Subject = subject;
msg.Body = message;
String imagesPath = Environment.CurrentDirectory + Path.DirectorySeparatorChar + "images";
String[] files = Directory.GetFiles(imagesPath);
foreach(String imageFile in files){
FileInfo file = new FileInfo(imageFile);
// create the INLINE attachment
Attachment inline = new Attachment(file.FullName);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = "@"+file.Name;
inline.ContentType.MediaType = "image/jpg";
inline.ContentType.Name = Path.GetFileName(imageFile);
msg.Attachments.Add(inline);
contentID);
}
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient(AppConfig.SMTPServer);
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
Utility.PrintMessage("Sending notification e-mail to: " + emailTo);
client.Send(msg);
Logger.WriteTNSLog(subject + " - " + emailTo);
}
catch (Exception e)
{
Logger.WriteAppLog(e.Message, "Utility.SendNotificationMail()");
}
}
catch (Exception)
{
}
}
答案 0 :(得分:1)
(我在尝试将文件嵌入电子邮件正文时遇到了同样的问题......最后我不得不将Image放在一个字节数组中,然后从那里开始:)
正如我所说,将图像原始数据放在字节数组imageByteArray
中,将图像文件名(带扩展名)放在字符串imageName
中。然后,像这样附上它(我任意选择imageCid作为附件的'cid')
...
mail.Body = "<html><body><div style='text-align:center'><img src=""cid:imageCid"" style=""margin:0 auto;""/></body></html>";
mail.Attachments.Add(New Attachment(New MemoryStream(imageByteArray), imageName));
mail.Attachments(0).ContentId = "imageCid";
...
smtp.Send(mail);
我更像是一个VB人,所以也许你需要改变一下。无论如何,我想我认为这是可以理解的:)