vb.net 发送带有图像的电子邮件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22431987/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
vb.net send email with image
提问by user3424662
there is a lot of topic about sending email with attachment but i couldn't find what i was looking for my form contain 8 textbox and 1 PictureBox i can send all textbox via email but ican't send PictureBox i try many code
有很多关于发送带附件的电子邮件的主题,但我找不到我正在寻找的表单包含 8 个文本框和 1 个图片框我可以通过电子邮件发送所有文本框但我无法发送图片框我尝试了很多代码
this is my form.
这是我的表格。
and here is my code
这是我的代码
Dim MyMessage As New MailMessage
Try
MyMessage.From = New MailAddress("[email protected]")
MyMessage.To.Add("[email protected]")
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("[email protected]", "x")
SMTP.Send(MyMessage)
MsgBox("your message Has been sent successfully")
Catch ex As Exception
End Try
i will be greatfull if you could help me :)
如果你能帮助我,我会很高兴的:)
采纳答案by NullReferenceException
Have you tried this.You can use the LinkedResource and AlternatiView for the same.
你试过这个吗。你可以使用 LinkedResource 和 AlternatiView。
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 with HtmlBody here is the reference : http://vba-useful.blogspot.fr/2014/01/send-html-email-with-embedded-images.html
或者你可以用 HtmlBody 发送它,这里是参考:http://vba-useful.blogspot.fr/2014/01/send-html-email-with-embedded-images.html
回答by chris_techno25
You need to add the directory of the picture file or any file you have and add it as attachment to your mail like this...
您需要添加图片文件的目录或您拥有的任何文件,并将其作为附件添加到您的邮件中,如下所示...
Dim attachment As System.Net.Mail.Attachment
attachment = New System.Net.Mail.Attachment(FilePath)
MyMessage.Attachments.Add(attachment)

