gpt4 book ai didi

c# - 在电子邮件中发送内联图像

转载 作者:IT王子 更新时间:2023-10-29 03:37:18 25 4
gpt4 key购买 nike

通过电子邮件发送图像作为正文中的嵌入图像时出现问题。图像文件显示为附件,这没问题,但内联图像部分仅显示为红色 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(
"<h3>Client: " + data.client_id + " Has Sent You A Screenshot</h3>" +
@"<img src=""cid:{0}"" />", inline.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 = @"<img src='cid:" + res.ContentId + @"'/>";
AlternateView alternateView = AlternateView.CreateAlternateViewFromString(htmlBody, null, MediaTypeNames.Text.Html);
alternateView.LinkedResources.Add(res);
return alternateView;
}

关于c# - 在电子邮件中发送内联图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18358534/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com