如何在 vb.net 中发送带有附件的电子邮件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1962511/
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
how to send an email with attachment in vb.net?
提问by user225269
how to do that?here is my current code, this is a windows form:
怎么做?这是我当前的代码,这是一个 Windows 窗体:
mail.From = New MailAddress(TextBox2.Text)
mail.To.Add(New MailAddress(TextBox1.Text))
mail.Subject = TextBox3.Text
mail.Body = TextBox4.Text
mail.IsBodyHtml = True
Dim client As SmtpClient = New SmtpClient("smtp.gmail.com")
client.EnableSsl = True
client.Credentials = New System.Net.NetworkCredential(TextBox2.Text, TextBox5.Text)
Try
client.Send(mail)
Catch ex As Exception
MessageBox.Show("Sending email failed. Please Try again")
End Try
回答by Gabe
Hereis a great example
这是一个很好的例子
Public Sub SendMailOneAttachment(ByVal From As String, _
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFile As String = "", _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "")
Dim myMessage As MailMessage
Try
myMessage = New MailMessage()
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer
If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""
If FileExists(AttachmentFile) Then _
.Attachments.Add(AttachmentFile)
End With
If SMTPServer <> "" Then _
SmtpMail.SmtpServer = SMTPServer
SmtpMail.Send(myMessage)
Catch myexp As Exception
Throw myexp
End Try
End Sub
Public Sub SendMailMultipleAttachments(ByVal From As String,_
ByVal sendTo As String, ByVal Subject As String, _
ByVal Body As String, _
Optional ByVal AttachmentFiles As ArrayList = Nothing, _
Optional ByVal CC As String = "", _
Optional ByVal BCC As String = "", _
Optional ByVal SMTPServer As String = "")
Dim myMessage As MailMessage
Dim i, iCnt As Integer
Try
myMessage = New MailMessage()
With myMessage
.To = sendTo
.From = From
.Subject = Subject
.Body = Body
.BodyFormat = MailFormat.Text
'CAN USER MAILFORMAT.HTML if you prefer
If CC <> "" Then .Cc = CC
If BCC <> "" Then .Bcc = ""
If Not AttachmentFiles Is Nothing Then
iCnt = AttachmentFiles.Count - 1
For i = 0 To iCnt
If FileExists(AttachmentFiles(i)) Then _
.Attachments.Add(AttachmentFiles(i))
Next
End If
End With
If SMTPServer <> "" Then _
SmtpMail.SmtpServer = SMTPServer
SmtpMail.Send(myMessage)
Catch myexp As Exception
Throw myexp
End Try
End Sub
Private Function FileExists(ByVal FileFullPath As String) _
As Boolean
If Trim(FileFullPath) = "" Then Return False
Dim f As New IO.FileInfo(FileFullPath)
Return f.Exists
End Function
回答by bendewey
See this sample:
请参阅此示例:
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx
http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx
Essentially, you'll need to add something like this:
本质上,您需要添加如下内容:
Dim file as string = "c:\Documents\MyFile.txt"
''// Create the file attachment for this e-mail message.
Attachment data = new Attachment(file, MediaTypeNames.Application.Octet);
''// Add time stamp information for the file.
ContentDisposition disposition = data.ContentDisposition;
disposition.CreationDate = System.IO.File.GetCreationTime(file);
disposition.ModificationDate = System.IO.File.GetLastWriteTime(file);
disposition.ReadDate = System.IO.File.GetLastAccessTime(file);
''// Add the file attachment to this e-mail message.
message.Attachments.Add(data);
回答by Voltiom
Important dispose File!
重要处置文件!
Dim smtp As New System.Net.Mail.SmtpClient
smtp.Host = "host"
smtp.Port = 25
smtp.EnableSsl = False
Dim mMail As New System.Net.Mail.MailMessage()
mMail.[To].Add("[email protected]")
mMail.From = New System.Net.Mail.MailAddress("[email protected]", "", System.Text.Encoding.UTF8)
mMail.Subject = mailSubject
mMail.Body = mailBody
Dim fileTXT as String = "c:\file1234.txt"
Dim data As Net.Mail.Attachment = New Net.Mail.Attachment(fileTXT)
data.Name = "file.txt"
mMail.Attachments.Add(data)
Try
smtp.Send(mMail)
data.Dispose()
Return True
Catch ex As System.Net.Mail.SmtpException
Return False
End Try