gpt4 book ai didi

c# - 如何将 'Name' header 添加到 EmailMessage 中的嵌入图像

转载 作者:行者123 更新时间:2023-11-30 16:46:18 25 4
gpt4 key购买 nike

我正在尝试发送带有嵌入图像的电子邮件(不是作为附件文件)。我能够发送邮件。

我正在使用以下代码发送邮件:

internal static void Send(SmtpServerConfigurations configurations, EmailMessage emailMsg)
{
using (var mail = InitializeMailMessage(emailMsg))
using (var smtpClient = CreateSmtpClient(configurations))
smtpClient.Send(mail);
}

private static MailMessage InitializeMailMessage(EmailMessage emailMsg)
{
var mail = new MailMessage
{
From = new MailAddress(emailMsg.From),
Subject = emailMsg.Subject,
IsBodyHtml = emailMsg.IsBodyHtml
};

mail.To.Add(emailMsg.To);

AddMessageBody(emailMsg, mail);

return mail;
}

private static void AddMessageBody(EmailMessage emailMsg, MailMessage mail)
{
if (emailMsg.IsBodyHtml)
{
var body = GetHtmlBody(emailMsg.Body, emailMsg.EmbeddedImages);
mail.AlternateViews.Add(body);
}
else
mail.Body = emailMsg.Body;
}

private static AlternateView GetHtmlBody(string body, List<EmbeddedImage> embeddedImages)
{
var alternateView = AlternateView.CreateAlternateViewFromString(body, null,
MediaTypeNames.Text.Html);

if (embeddedImages == null) return alternateView;

foreach (var image in embeddedImages)
{
var imageToInline = new LinkedResource(image.Path, MediaTypeNames.Image.Jpeg);
imageToInline.ContentId = image.Id;
alternateView.LinkedResources.Add(imageToInline);
}
return alternateView;
}

private static SmtpClient CreateSmtpClient(SmtpServerConfigurations config)
{
var smtpClient = new SmtpClient(config.Host);
smtpClient.Port = config.PortNo;

if (config.IsAuthenticationRequired)
smtpClient.Credentials =
new NetworkCredential(config.Username, config.Password);
else
smtpClient.UseDefaultCredentials = true;

smtpClient.EnableSsl = false;
return smtpClient;
}

但是使用上面的代码发送的邮件不是我想要的格式。

我想要的是;

MIME-Version: 1.0
From: x@y.com
To: a@b.com
Date: 11 Nov 2016 11:37:52 +0530
Subject: This is subject
Content-Type: multipart/related;
boundary=--boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d; type="text/html"


----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "=
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xm=
lns =3D "http://www.w3.org/1999/xhtml" > <head ><meta http - equi=
v =3D "content-type" content =3D "text/html; charset=3DUTF-8" /><=
/head ><body style =3D"font-family: Segoe UI; text-align:left;" >=
This is body<br /><img alt =3D"" src =3D"cid:05393c56-15c1-4652-a=
31f-9cc513726bc0" height=3D"50" width=3D"50"/></body ></html >
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: image/jpeg name="filename.jpg" <<-----This is what I want.
Content-Transfer-Encoding: base64
Content-ID: <05393c56-15c1-4652-a31f-9cc513726bc0>

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMF
BwYHBwcGBwcI
.
.
.
/w20K7sPt8ul2st3/z0dd36Hj9K9I+HHwj8M6/rLaldaJp8l6y
kGRYgn6LgfpXve0pundwQmk9z//Z
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d--

我得到的是;

MIME-Version: 1.0
From: x@y.com
To: a@b.com
Date: 11 Nov 2016 11:37:52 +0530
Subject: This is subject
Content-Type: multipart/related;
boundary=--boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d; type="text/html"


----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC " -//W3C//DTD XHTML 1.0 Transitional//EN" "=
http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xm=
lns =3D "http://www.w3.org/1999/xhtml" > <head ><meta http - equi=
v =3D "content-type" content =3D "text/html; charset=3DUTF-8" /><=
/head ><body style =3D"font-family: Segoe UI; text-align:left;" >=
This is body<br /><img alt =3D"" src =3D"cid:05393c56-15c1-4652-a=
31f-9cc513726bc0" height=3D"50" width=3D"50"/></body ></html >
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d
Content-Type: image/jpeg
Content-Transfer-Encoding: base64
Content-ID: <05393c56-15c1-4652-a31f-9cc513726bc0>

/9j/4AAQSkZJRgABAQEAYABgAAD/2wBDAAIBAQIBAQICAgICAgICAwUDAwMDAwYEBAMF
BwYHBwcGBwcI
.
.
.
/w20K7sPt8ul2st3/z0dd36Hj9K9I+HHwj8M6/rLaldaJp8l6y
kGRYgn6LgfpXve0pundwQmk9z//Z
----boundary_3_1bb3db0a-d33f-46a7-a6ce-60249096160d--

如何在原始邮件的嵌入图像部分实现自定义“名称” header ?

我想添加标题是因为;

当我点击 Gmail 收件箱中图片上显示的下载按钮时,我得到了没有扩展名的“noname”文件。下载的文件没有用,除非用户将其扩展名更改为“.jpg/.jpeg”。 Download image button on gmail

当我对另一个组件(我没有代码)进行相同的尝试时,奇怪的是我能够下载具有正确文件名的图像。这两封邮件之间的唯一区别是“姓名”标题。

请建议我如何做到这一点或任何其他方式来实现它。

最佳答案

这会帮你解决问题

imageToInline.ContentType.Name = "ImageName.jpg";

关于c# - 如何将 'Name' header 添加到 EmailMessage 中的嵌入图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40542438/

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