如何使用vb.net在电子邮件上发送图片框

时间:2017-12-26 08:05:49

标签: vb.net email picturebox

我想使用vb.net在电子邮件上发送图片框,但电子邮件是空的。

这是我的代码

 Try
        Dim smtp = New SmtpClient("smtp.gmail.com") ' connexion au serveur SMTP Gmail
        Dim message As New MailMessage
        message.Body = PictureBox1.Container
        message.To.Add(txt_adr.Text) ' choisir l'@ du destinataire
        'message.Body = corp.Text ' le corps du mail a envoyé
        message.From = New MailAddress("*******") ' message from expéditeur
        message.Subject = Objet.Text ' le champs objet du mail
        '------------ connexion avec le SMTP ---------'
        smtp.EnableSsl = True ' activer le chiffremment SSL
        smtp.Port = "587" ' accéder au port de connexion du SMTP
        smtp.Credentials = New System.Net.NetworkCredential("*******", "*********") ' introduire les paramétres de connexion de l'expéditeur
        smtp.Send(message) 'envoyé le mail
        MsgBox("Successful")
    Catch ex As Exception
        MsgBox(ex.Message, MsgBoxStyle.Exclamation, "erreur d'envoie")
    End Try

然后,我尝试使用html体,但图像是在附件中发送而不是背景,没有文本(txt_objet.text)

这是我的HTML代码

      Dim img As LinkedResource
        img = New LinkedResource(image_path.Text, "image/jpeg")
        img.ContentId = "myimage"
        Dim htmlbody As String =
           "<html>
            <head>
            <style>
             body {
                   background-image : url(C:\Users\user\Desktop\Nouveau dossier (2)\img.jpg); 
                   background-position : center center;
                   }
            </style>
            <body>
            <h1>Picture</h1>
            <br><p>" + txt_objet.Text + "</p>
        </body>
        </html>"
        Dim altview As AlternateView = AlternateView.CreateAlternateViewFromString(htmlbody, Nothing, "Text/html")
        altview.LinkedResources.Add(img)
        '****** affecter cette format à l'email
        message.AlternateViews.Add(altview)
        message.Body = htmlbody
        message.IsBodyHtml = True

1 个答案:

答案 0 :(得分:2)

首先,将您的图片保存到文件中(如果它已经作为文件提供了它的好处)

第二个,用于在电子邮件正文中发送内嵌图像 请尝试以下代码:

Dim htmlBody As String = "<html><body><h1>Picture</h1><br><img src=""cid:filename""></body></html>"
Dim avHtml As AlternateView = AlternateView.CreateAlternateViewFromString(htmlBody, Nothing, MediaTypeNames.Text.Html)
Dim inline As LinkedResource = New LinkedResource("filename.jpg", MediaTypeNames.Image.Jpeg)
inline.ContentId = Guid.NewGuid.ToString
avHtml.LinkedResources.Add(inline)
Dim mail As MailMessage = New MailMessage
mail.AlternateViews.Add(avHtml)
Dim att As Attachment = New Attachment(filePath)
att.ContentDisposition.Inline = true
mail.From = New MailAddress("*******")
mail.To.Add(txt_adr.Text)
mail.Subject = Objet.Text
mail.Body = String.Format("<img src=""""cid:{0}"""" />", inline.ContentId)
mail.IsBodyHtml = true
mail.Attachments.Add(att)