Vb.net send an email with an image

there are many questions about sending email with an attachment, but I could not find what I was looking for my form contains 8 text fields and 1 PictureBox, I can send all text fields by email, but ican't send PictureBox I try a lot of code

this is my form .

and here is my code

    Dim MyMessage As New MailMessage
    Try
        MyMessage.From = New MailAddress("x@gmail.com")

        MyMessage.To.Add("x@gmail.com")

        MyMessage.Subject = "Data"

        MyMessage.Body = Label1.Text + "  =  " + IDTextBox.Text + Environment.NewLine + Label2.Text + "  =  " + ناوی_سیانی_کارمەندTextBox.Text + Environment.NewLine + Label3.Text + "  =  " + ساڵی_لە__دایک_بوونTextBox.Text + Environment.NewLine + Label4.Text + "  =  " + شوێنی_دانیشتنTextBox.Text + Environment.NewLine + Label5.Text + "  =  " + گروپی_خوێنTextBox.Text + Environment.NewLine + Label6.Text + "  =  " + ناو_نیشانی_کار_لە_کۆمپانیاTextBox.Text + Environment.NewLine + Label7.Text + "  =  " + ژمارەی_مۆبایلTextBox.Text + Environment.NewLine + Label8.Text + "  =  " + شوێنی_کارTextBox.Text

        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.Port = 587
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("x@gmail.com", "x")
        SMTP.Send(MyMessage)
        MsgBox("your message Has been sent successfully")
    Catch ex As Exception

    End Try

I will be great if you could help me :)

+4
source share
2 answers

You tried it. You can use LinkedResource and AlternatiView for this.

string path = Filelocation; // as you are using picture box,give the location from where the image is loading into picture box. 

LinkedResource imageLink = new LinkedResource(path);
imageLink.ContentId = "myImage";
AlternateView altView = AlternateView.CreateAlternateViewFromString(
  "<html><body><img src=cid:myImage/>" + 
  "<br></body></html>" + strMailContent, 
  null, MediaTypeNames.Text.Html);
altView.LinkedResources.Add(imageLink);

//now append it to the body of the mail
msg.AlternateViews.Add(altView);
msg.IsBodyHtml = true;

or you can send it using HtmlBody. link: http://vba-useful.blogspot.fr/2014/01/send-html-email-with-embedded-images.html

+2

, ...

Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment(FilePath)
MyMessage.Attachments.Add(attachment)
0

Source: https://habr.com/ru/post/1531982/


All Articles