我正在使用帶有 aspx 電子郵件模板的 Mailkit,該模板帶有徽標的影像控制元件。電子郵件發送正常,但內嵌徽標作為附件發送。我花了幾天時間在這里查看類似的問題和示例,但無法弄清楚我自己的情況是什么問題。我需要影像顯示在郵件正文中,而不是作為附件顯示。
這是代碼檔案:
Private Sub EmailCustomer()
Dim ToAddress As String = CustomerEmail
Const FromAddress As String = "[email protected]"
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==--
這是它的發送方式:

uj5u.com熱心網友回復:
根據檔案,我認為您不需要設定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 />標簽,電子郵件客戶端不知道如何處理它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/351524.html
上一篇:如何讓輸入框只顯示一次
下一篇:如何編輯訪問資料庫?
