如何发送带有图像的电子邮件

时间:2017-03-15 03:26:16

标签: vb.net email attachment

我正在尝试发送带有嵌入式图片的电子邮件,但我发现了这一点并尝试过,但我收到错误的电子邮件发送失败。

 Try
        Dim strMsg As String = Nothing

        Dim img1 As LinkedResource = New LinkedResource("C:\Users\myname\Desktop\Login.jpg", MediaTypeNames.Image.Jpeg)
        Dim img2 As LinkedResource = New LinkedResource("C:\Users\myname\Desktop\Login.jpg", MediaTypeNames.Image.Jpeg)

        img1.ContentId = "Image1"
        img2.ContentId = "Image2"

        Dim mail As New MailMessage()
        mail.From = New MailAddress("myemail.com", "password")
        'mail.CC.Add("ex@test.com.au")
        mail.Bcc.Add("jhaymalagasang@gmail.com")
        mail.Subject = "Reports"

        mail.Priority = MailPriority.High

        strMsg = ""
        strMsg = strMsg & "<html><body><p></p>"
        strMsg = strMsg & "<table width='70%' border='0' cellpadding='0' cellspacing='0' bgcolor='white'>"
        strMsg = strMsg & "<tr>"
        strMsg = strMsg & "<td><h2><center><span style='color:#1C8DFF'>Example Report</span></center></h2></td>"
        strMsg = strMsg & "</tr>"
        strMsg = strMsg & "<tr>"
        strMsg = strMsg & "<td>&nbsp;</td>"
        strMsg = strMsg & "</tr>"
        strMsg = strMsg & "<tr>"
        strMsg = strMsg & "<td><img src=cid:Image1 alt='image description'></td>"
        strMsg = strMsg & "</tr>"
        strMsg = strMsg & "<tr>"
        strMsg = strMsg & "<td><img src=cid:Image2 alt='image description'></td>"
        strMsg = strMsg & "</tr>"
        strMsg = strMsg & "</table></body></html>"



Dim av1 As AlternateView = AlternateView.CreateAlternateViewFromString(strMsg, Nothing, MediaTypeNames.Text.Html)
        av1.LinkedResources.Add(img1)
        av1.LinkedResources.Add(img2)

mail.AlternateViews.Add(AV1)             mail.IsBodyHtml = True

        Dim smtp As SmtpClient = New SmtpClient()
        smtp.Host = "1.1.1.1" ' Whatever is your smtp server
        smtp.Port = "25"
        smtp.Send(mail)
        MsgBox("Email Sent !", MsgBoxStyle.Information, "Success")
    Catch ex As Exception
        MsgBox("Error in Email" & ex.Message, MsgBoxStyle.Critical, "Email Failed")
    End Try

任何人都可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

    Dim strMsg As String = Nothing
    Dim img1 As LinkedResource = Nothing

    Try
        img1 = New LinkedResource("C:\For Guidance System\header.jpg", MediaTypeNames.Image.Jpeg)
        img1.ContentId = "Image1"
    Catch ex As Exception
        MsgBox("Error: " + ex.Message + "", MsgBoxStyle.Critical, "Loading logo failed!")
        Return IDnums = "" And emailAdd = ""
        Exit Function
    End Try

    Dim mail As New MailMessage()
    mail.From = New MailAddress("myemail@gmail.com", "Description")
    mail.Bcc.Add(CType(emailAdd.ToString, String))
    mail.Subject = "Requested passcode"
    mail.Priority = MailPriority.High
    strMsg = ""
    strMsg = strMsg & "<html><body><p></p>"
    strMsg = strMsg & "<table width='70%' border='0' cellpadding='0' cellspacing='0' bgcolor='white'>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<td><h2><center><span style='color:#1C8DFF'>Header</span></center></h2></td>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<td>&nbsp;</td>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<td><img src=cid:Image1 alt=></td>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<p>The requested passcode is:    " + final.ToString + "</p>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<td>&nbsp;</td>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<p>If you can't login, please refer it to the guidance office for further assistance, else you can request again another passcode.</p>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "<tr>"
    strMsg = strMsg & "<p>Please do not reply to this email. Thank you and God bless!</p>"
    strMsg = strMsg & "</tr>"
    strMsg = strMsg & "</table></body></html>"

Dim av1 As AlternateView = AlternateView.CreateAlternateViewFromString(strMsg, Nothing, MediaTypeNames.Text.Html)
    av1.LinkedResources.Add(img1)

    mail.AlternateViews.Add(av1)
    mail.IsBodyHtml = True

Dim smtp As SmtpClient = New SmtpClient()

我通过修改和添加一些代码解决了这个问题:

    smtp.UseDefaultCredentials = False
    smtp.Credentials = New System.Net.NetworkCredential("myemail@gmail.com", "password")
    smtp.Host = "smtp.gmail.com"
    smtp.Port = 587
    smtp.EnableSsl = True
    smtp.Send(mail)