图像未在动态生成的电子邮件中正确嵌入

时间:2013-08-13 20:25:51

标签: vb.net image html-email system.net.mail

我无法正确嵌入我的照片,当电子邮件到达时,它们仅作为附件显示。 “动态”部分是电子邮件中的图片数量在即将发送之前是未知的。这是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

结束班

1 个答案:

答案 0 :(得分:0)

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

  1. 构建LinkedResource时,请确保使用constructor您可以传递图像的正确ContentType

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

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

    _mail.Headers.Add(“Content-Disposition”,“inline”)