在电子邮件中发送内联图片
在电子邮件中发送内联图片
发送嵌入式图像邮件的过程中遇到问题。图像文件作为附件显示正常,但内联图像部分只显示为红色的X。
这是我目前的进展情况。
LinkedResource inline = new LinkedResource(filePath); inline.ContentId = Guid.NewGuid().ToString(); MailMessage mail = new MailMessage(); Attachment att = new Attachment(filePath); att.ContentDisposition.Inline = true; mail.From = from_email; mail.To.Add(data.email); mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot"; mail.Body = String.Format( "
Client: " + data.client_id + " Has Sent You A Screenshot
" + @"", inline.ContentId); mail.IsBodyHtml = true; mail.Attachments.Add(att);
admin 更改状态以发布 2023年5月21日
试试这个
string htmlBody = "Picture
"; AlternateView avHtml = AlternateView.CreateAlternateViewFromString (htmlBody, null, MediaTypeNames.Text.Html); LinkedResource inline = new LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg); inline.ContentId = Guid.NewGuid().ToString(); avHtml.LinkedResources.Add(inline); MailMessage mail = new MailMessage(); mail.AlternateViews.Add(avHtml); Attachment att = new Attachment(filePath); att.ContentDisposition.Inline = true; mail.From = from_email; mail.To.Add(data.email); mail.Subject = "Client: " + data.client_id + " Has Sent You A Screenshot"; mail.Body = String.Format( "Client: " + data.client_id + " Has Sent You A Screenshot
" + @"", att.ContentId); mail.IsBodyHtml = true; mail.Attachments.Add(att);
用于嵌入图像的一些最小的C#代码,可以是:
MailMessage mailWithImg = GetMailWithImg(); MySMTPClient.Send(mailWithImg); //* Set up your SMTPClient before! private MailMessage GetMailWithImg() { MailMessage mail = new MailMessage(); mail.IsBodyHtml = true; mail.AlternateViews.Add(GetEmbeddedImage("c:/image.png")); mail.From = new MailAddress("yourAddress@yourDomain"); mail.To.Add("recipient@hisDomain"); mail.Subject = "yourSubject"; return mail; } private AlternateView GetEmbeddedImage(String filePath) { LinkedResource res = new LinkedResource(filePath); res.ContentId = Guid.NewGuid().ToString(); string htmlBody = @""; AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html); alternateView.LinkedResources.Add(res); return alternateView; }