Mailkit 将内联徽标显示为附件
Mailkit Displaying Inline logo As Attachment
我正在使用带有 aspx 电子邮件模板的 Mailkit,该模板带有用于徽标的图像控件。电子邮件发送正常,但内联徽标作为附件发送。我花了几天时间在这里查看类似的问题和示例,但无法弄清楚我自己的问题是什么。我需要图片显示在邮件正文中,而不是作为附件。
这是代码文件:
Private Sub EmailCustomer()
Dim ToAddress As String = CustomerEmail
Const FromAddress As String = "Support@sealtizen.com"
Try
Dim mail As MimeMessage
Dim bodybuilder As New BodyBuilder
mail = New MimeMessage()
mail.From.Add(New MailboxAddress("", FromAddress.ToUpper))
mail.To.Add(New MailboxAddress("", CustomerEmail))
mail.Subject = "Order Confirmation"
Dim contentType As New ContentType("image", "png")
Dim contentId = MimeUtils.GenerateMessageId()
Dim image = CType(bodybuilder.LinkedResources.Add(Server.MapPath(".") & "/Img/MainLogo.png", contentType), MimePart)
image.ContentTransferEncoding = ContentEncoding.Default
image.ContentId = contentId
image.ContentDisposition = New ContentDisposition() With {.Disposition = ContentDisposition.Inline}
bodybuilder.HtmlBody = PopulateEmailHtmlBody(CustomerEmail, Session("GatewayOrderID"), "cid:" & image.ContentId)
mail.Body = bodybuilder.ToMessageBody()
Using client As New MailKit.Net.Smtp.SmtpClient
client.Connect("xxx", 465, True)
client.Authenticate(FromAddress, ConfigurationManager.AppSettings("WebMailPsw"))
client.AuthenticationMechanisms.Remove("XOAUTH2")
client.Send(mail)
client.Disconnect(True)
End Using
Catch ex As Exception
ShowMessage("Connection issues sending email.", MessageType.Info)
End Try
End Sub
Private Function PopulateEmailHtmlBody(ByVal customeremail As String, ByVal orderid As String, ByVal logoUrl As String) As String
Using reader As StreamReader = New StreamReader(Server.MapPath("~/OrderStatusEmailTemplate.aspx"))
Dim body As String = String.Empty
body = reader.ReadToEnd
body = body.Replace("{LogoUrl}", logoUrl)
body = body.Replace("{CustomerEmail}", customeremail)
body = body.Replace("{OrderID}", orderid)
Return body
End Using
End Function
这是模板:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="Scriptmanager1" runat="server" ></asp:ScriptManager>
<div class="row">
<div style="background: #40B800; width: 100%; height: 10px;">
</div>
</div>
<div id="logoDiv" style="border-bottom: solid 4px #919191; padding-bottom: 2px">
<asp:Image ID="logo" runat="server" src="{logourl}"/>
</div>
<br />
Hello <em>{CustomerEmail} </em>
<br />
<br />
Your Order was placed successfully with OrderID:
<em>{OrderID} </em>
This Order has been forwarded for payment processing. Your Order shall be processed and shipped when this process completes.
<br />
<br />
Yours faithfully
<br />
For: Xxx.
<div class="row">
<div style="background-color: #eaeaea; border-top: solid 2px #919191">
</div>
</div>
</form>
</body>
部分原始数据如下:
Message-Id: <SHDL2FYG7FU4.BD3A7K1JDSQJ@plesk3001>
To:xxxx
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="=- 4ZC71hL1CWyK9Sdx8oPqnQ=="; type="text/html"
--=-4ZC71hL1CWyK9Sdx8oPqnQ==
Content-Type: text/html; charset=utf-8
Content-Id: <TPQO2FYG7FU4.UIEK2B0GWEC33@plesk3001>
这里:
--=-4ZC71hL1CWyK9Sdx8oPqnQ==
Content-Type: image/png; name=MainLogo.png
Content-Transfer-Encoding: base64
Content-Location: MainLogo.png
Content-Id: <SHDL2FYG7FU4.7PB5WR21L9MX@plesk3001>
Content-Disposition: inline
--=-4ZC71hL1CWyK9Sdx8oPqnQ==--
发送方式如下:
基于the documentation,我认为你不需要设置ContentTransferEncoding
或ContentDisposition
;你应该只需要使用:
Dim image As MimeEntity = bodybuilder.LinkedResources.Add(Server.MapPath(".") & "/Img/MainLogo.png", contentType)
image.ContentId = MimeUtils.GenerateMessageId()
bodybuilder.HtmlBody = PopulateEmailHtmlBody(CustomerEmail, Session("GatewayOrderID"), "cid:" & image.ContentId)
您也不想在模板中使用任何服务器控件,因为您正在读取文件的内容而不是执行模板。
<body>
<div class="row">
<div style="background: #40B800; width: 100%; height: 10px;">
</div>
</div>
<div id="logoDiv" style="border-bottom: solid 4px #919191; padding-bottom: 2px">
<img src="{logourl}"/>
</div>
<br />
Hello <em>{CustomerEmail} </em>
<br />
<br />
Your Order was placed successfully with OrderID: <em>{OrderID} </em>
This Order has been forwarded for payment processing. Your Order shall be processed and shipped when this process completes.
<br />
<br />
Yours faithfully
<br />
For: Xxx.
<div class="row">
<div style="background-color: #eaeaea; border-top: solid 2px #919191">
</div>
</div>
</body>
我怀疑第二点是问题的原因;您的电子邮件正文包含文字文本 <asp:Image .../>
而不是 <img />
标记,并且电子邮件客户端不知道如何处理它。
我正在使用带有 aspx 电子邮件模板的 Mailkit,该模板带有用于徽标的图像控件。电子邮件发送正常,但内联徽标作为附件发送。我花了几天时间在这里查看类似的问题和示例,但无法弄清楚我自己的问题是什么。我需要图片显示在邮件正文中,而不是作为附件。
这是代码文件:
Private Sub EmailCustomer()
Dim ToAddress As String = CustomerEmail
Const FromAddress As String = "Support@sealtizen.com"
Try
Dim mail As MimeMessage
Dim bodybuilder As New BodyBuilder
mail = New MimeMessage()
mail.From.Add(New MailboxAddress("", FromAddress.ToUpper))
mail.To.Add(New MailboxAddress("", CustomerEmail))
mail.Subject = "Order Confirmation"
Dim contentType As New ContentType("image", "png")
Dim contentId = MimeUtils.GenerateMessageId()
Dim image = CType(bodybuilder.LinkedResources.Add(Server.MapPath(".") & "/Img/MainLogo.png", contentType), MimePart)
image.ContentTransferEncoding = ContentEncoding.Default
image.ContentId = contentId
image.ContentDisposition = New ContentDisposition() With {.Disposition = ContentDisposition.Inline}
bodybuilder.HtmlBody = PopulateEmailHtmlBody(CustomerEmail, Session("GatewayOrderID"), "cid:" & image.ContentId)
mail.Body = bodybuilder.ToMessageBody()
Using client As New MailKit.Net.Smtp.SmtpClient
client.Connect("xxx", 465, True)
client.Authenticate(FromAddress, ConfigurationManager.AppSettings("WebMailPsw"))
client.AuthenticationMechanisms.Remove("XOAUTH2")
client.Send(mail)
client.Disconnect(True)
End Using
Catch ex As Exception
ShowMessage("Connection issues sending email.", MessageType.Info)
End Try
End Sub
Private Function PopulateEmailHtmlBody(ByVal customeremail As String, ByVal orderid As String, ByVal logoUrl As String) As String
Using reader As StreamReader = New StreamReader(Server.MapPath("~/OrderStatusEmailTemplate.aspx"))
Dim body As String = String.Empty
body = reader.ReadToEnd
body = body.Replace("{LogoUrl}", logoUrl)
body = body.Replace("{CustomerEmail}", customeremail)
body = body.Replace("{OrderID}", orderid)
Return body
End Using
End Function
这是模板:
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="Scriptmanager1" runat="server" ></asp:ScriptManager>
<div class="row">
<div style="background: #40B800; width: 100%; height: 10px;">
</div>
</div>
<div id="logoDiv" style="border-bottom: solid 4px #919191; padding-bottom: 2px">
<asp:Image ID="logo" runat="server" src="{logourl}"/>
</div>
<br />
Hello <em>{CustomerEmail} </em>
<br />
<br />
Your Order was placed successfully with OrderID:
<em>{OrderID} </em>
This Order has been forwarded for payment processing. Your Order shall be processed and shipped when this process completes.
<br />
<br />
Yours faithfully
<br />
For: Xxx.
<div class="row">
<div style="background-color: #eaeaea; border-top: solid 2px #919191">
</div>
</div>
</form>
</body>
部分原始数据如下:
Message-Id: <SHDL2FYG7FU4.BD3A7K1JDSQJ@plesk3001>
To:xxxx
MIME-Version: 1.0
Content-Type: multipart/related;
boundary="=- 4ZC71hL1CWyK9Sdx8oPqnQ=="; type="text/html"
--=-4ZC71hL1CWyK9Sdx8oPqnQ==
Content-Type: text/html; charset=utf-8
Content-Id: <TPQO2FYG7FU4.UIEK2B0GWEC33@plesk3001>
这里:
--=-4ZC71hL1CWyK9Sdx8oPqnQ==
Content-Type: image/png; name=MainLogo.png
Content-Transfer-Encoding: base64
Content-Location: MainLogo.png
Content-Id: <SHDL2FYG7FU4.7PB5WR21L9MX@plesk3001>
Content-Disposition: inline
--=-4ZC71hL1CWyK9Sdx8oPqnQ==--
发送方式如下:
基于the documentation,我认为你不需要设置ContentTransferEncoding
或ContentDisposition
;你应该只需要使用:
Dim image As MimeEntity = bodybuilder.LinkedResources.Add(Server.MapPath(".") & "/Img/MainLogo.png", contentType)
image.ContentId = MimeUtils.GenerateMessageId()
bodybuilder.HtmlBody = PopulateEmailHtmlBody(CustomerEmail, Session("GatewayOrderID"), "cid:" & image.ContentId)
您也不想在模板中使用任何服务器控件,因为您正在读取文件的内容而不是执行模板。
<body>
<div class="row">
<div style="background: #40B800; width: 100%; height: 10px;">
</div>
</div>
<div id="logoDiv" style="border-bottom: solid 4px #919191; padding-bottom: 2px">
<img src="{logourl}"/>
</div>
<br />
Hello <em>{CustomerEmail} </em>
<br />
<br />
Your Order was placed successfully with OrderID: <em>{OrderID} </em>
This Order has been forwarded for payment processing. Your Order shall be processed and shipped when this process completes.
<br />
<br />
Yours faithfully
<br />
For: Xxx.
<div class="row">
<div style="background-color: #eaeaea; border-top: solid 2px #919191">
</div>
</div>
</body>
我怀疑第二点是问题的原因;您的电子邮件正文包含文字文本 <asp:Image .../>
而不是 <img />
标记,并且电子邮件客户端不知道如何处理它。