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

时间:2022-02-25 18:15:22

I want to send picturebox on e-mail using vb.net, but the e-mail is sended empty.

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

Here is my code

这是我的代码

 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

then , i try to use a htmlbody , but the image is sended in attachement not a background , and without the text (txt_objet.text)

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

Here is my html code

这是我的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 个解决方案

#1


2  

First, save your image in to a file, (if its already available as a file its good)

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

Second, For sending inline images in the email body.
Try the below code:

其次,用于在电子邮件正文中发送内联图像。请尝试以下代码:

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)

#1


2  

First, save your image in to a file, (if its already available as a file its good)

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

Second, For sending inline images in the email body.
Try the below code:

其次,用于在电子邮件正文中发送内联图像。请尝试以下代码:

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)