如何在EmailMessage中向嵌入图像添加“名称”标题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在EmailMessage中向嵌入图像添加“名称”标题相关的知识,希望对你有一定的参考价值。

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

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

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--

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

我想添加标题是因为;

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

当我尝试使用另一个组件(我没有代码)时奇怪的是我能够以正确的文件名下载该图像。这两封邮件之间的区别仅在于“名称”标题。

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

答案

这将为你做到这一点

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

以上是关于如何在EmailMessage中向嵌入图像添加“名称”标题的主要内容,如果未能解决你的问题,请参考以下文章

如何在 React Native 中向图像添加文本?

如何在 iOS 6 中向分组样式 UITableViewController 添加背景图像?

在 SwiftUI 中向 TextField/SecureField 添加图像,向占位符文本添加填充

如何在 CKeditor 上传中向 POST 值添加字段

我将如何在 kivy 中向后发送图像?

在 C 中向 UUEncoder 添加空填充