0

当电子邮件到达时,我无法正确嵌入我的图片,它们仅显示为附件。“动态”部分是电子邮件中的图片数量在即将发送之前是未知的。这是 HTML 在发送之前的样子的示例。

<html>
<body>
<h1>LineOne</h1>
<p>LineTwo</p>
<img src='cid:1' alt='ImageOne'></img>
<img src='cid:2' alt='ImageTwo'></img>
</body>
</html>

电子邮件到达,但仅显示文本,图像显示为附件。如果有什么不同,我在 VB.NET 中使用 LinkedResources。当我使用相同的代码发送单个图像(即不是动态的)时,它会正确嵌入。这次我想不出有什么不同。

编辑:这是我的 VB 代码,添加一些上下文。

Private Sub TimeBeforeEmailSent_Tick(sender As System.Object, e As System.EventArgs)      Handles TimeBeforeEmailSent.Tick
    Dim sendEmail As New sendEmail
    Dim image(numberOfPhotosTaken) As Object
    For i = 1 To numberOfPhotosTaken
        image(i) = New LinkedResource((Mid(Application.ExecutablePath, 1, (Application.ExecutablePath.Length - 9)) + i.ToString + ".jpg"), "image/jpeg")
    Next
    sendEmail.write(numberOfPhotosTaken, image)
    TimeBeforeEmailSent.Enabled = False
End Sub

这里是 sendEmail 类:

Imports System.Net.Mail
Imports System.IO


Public Class sendEmail
Dim bodyText As String = "<html><body><h1>Heading</h1><p>Body</p>"
Dim alternateView As AlternateView = AlternateView.CreateAlternateViewFromString(bodyText, Nothing, "text/html")



Public Sub write(numberOfPhotosTaken As Integer, image As Array)
    For i = 1 To numberOfPhotosTaken
        Try
            image(i).ContentId = i
            image(i).TransferEncoding = Net.Mime.TransferEncoding.Base64
            alternateView.LinkedResources.Add(image(i))
            bodyText = bodyText & "<img src='cid:" & i & "' alt='image from webcam'></img>"
        Catch ex As Exception
            'This exception means two or more photos were taken in such quick succession that the camera couldn't keep up
            'Therefore, the program thinks more photos were taken than actually exist
        End Try
    Next
    bodyText = bodyText & "</body></html>"
    Dim Mail As New MailMessage
    Mail.Headers.Add("Content-Type", "multipart/related")
    Mail.Headers.Add("Content-Disposition", "inline")
    Mail.AlternateViews.Add(alternateView)
    Mail.Subject = "Subject"
    Mail.To.Add(Lockscreen.sendToAddress)
    Mail.From = New MailAddress(removed for privacy)
    Mail.Body = Nothing
    send(Mail)
End Sub


Sub send(Mail)
    Dim SMTP As New SmtpClient
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential(removed for privacy)
    SMTP.Port = "587"
    SMTP.Host = "smtp.gmail.com"
    SMTP.Send(Mail)
    Dim filePath As String
    For i = 1 To Lockscreen.numberOfPhotosTaken
        Try
            filePath = (Mid(Application.ExecutablePath, 1, (Application.ExecutablePath.Length - 9)) + i.ToString + ".jpg")
            File.Delete(filePath)
        Catch ex As Exception
        End Try
    Next
End Sub

结束类

4

1 回答 1

0

您没有在构建的位置添加代码并将其添加LinkedResource到邮件中,但请确保:

  1. 构造 时LinkedResource,请确保使用构造函数,您可以在其中传递图像的正确 ContentType

  2. MailMessage对象添加 2 个标头:

    _mail.Headers.Add("Content-Type", "multipart/related")

    _mail.Headers.Add("Content-Disposition", "inline")

于 2013-08-13T22:05:45.623 回答