如何在vb.net中发送附件的电子邮件?

前端之家收集整理的这篇文章主要介绍了如何在vb.net中发送附件的电子邮件?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
该怎么做?这是我目前的代码,这是一个Windows窗体:
  1. mail.From = New MailAddress(TextBox2.Text)
  2. mail.To.Add(New MailAddress(TextBox1.Text))
  3. mail.Subject = TextBox3.Text
  4. mail.Body = TextBox4.Text
  5.  
  6.  
  7.  
  8. mail.IsBodyHtml = True
  9.  
  10.  
  11. Dim client As SmtpClient = New SmtpClient("smtp.gmail.com")
  12. client.EnableSsl = True
  13. client.Credentials = New System.Net.NetworkCredential(TextBox2.Text,TextBox5.Text)
  14.  
  15.  
  16. Try
  17. client.Send(mail)
  18. Catch ex As Exception
  19. MessageBox.Show("Sending email Failed. Please Try again")
  20.  
  21.  
  22. End Try
Here就是一个很好的例子
  1. Public Sub SendMailOneAttachment(ByVal From As String,_
  2. ByVal sendTo As String,ByVal Subject As String,_
  3. ByVal Body As String,_
  4. Optional ByVal AttachmentFile As String = "",_
  5. Optional ByVal CC As String = "",_
  6. Optional ByVal BCC As String = "",_
  7. Optional ByVal SMTPServer As String = "")
  8.  
  9. Dim myMessage As MailMessage
  10.  
  11. Try
  12. myMessage = New MailMessage()
  13. With myMessage
  14. .To = sendTo
  15. .From = From
  16. .Subject = Subject
  17. .Body = Body
  18. .BodyFormat = MailFormat.Text
  19. 'CAN USER MAILFORMAT.HTML if you prefer
  20.  
  21. If CC <> "" Then .Cc = CC
  22. If BCC <> "" Then .Bcc = ""
  23.  
  24. If FileExists(AttachmentFile) Then _
  25. .Attachments.Add(AttachmentFile)
  26.  
  27. End With
  28.  
  29. If SMTPServer <> "" Then _
  30. SmtpMail.SmtpServer = SMTPServer
  31. SmtpMail.Send(myMessage)
  32.  
  33. Catch myexp As Exception
  34. Throw myexp
  35. End Try
  36.  
  37. End Sub
  38.  
  39. Public Sub SendMailMultipleAttachments(ByVal From As String,_
  40. ByVal sendTo As String,_
  41. ByVal Body As String,_
  42. Optional ByVal AttachmentFiles As ArrayList = Nothing,_
  43. Optional ByVal CC As String = "",_
  44. Optional ByVal BCC As String = "",_
  45. Optional ByVal SMTPServer As String = "")
  46.  
  47. Dim myMessage As MailMessage
  48. Dim i,iCnt As Integer
  49.  
  50. Try
  51. myMessage = New MailMessage()
  52. With myMessage
  53. .To = sendTo
  54. .From = From
  55. .Subject = Subject
  56. .Body = Body
  57. .BodyFormat = MailFormat.Text
  58. 'CAN USER MAILFORMAT.HTML if you prefer
  59.  
  60. If CC <> "" Then .Cc = CC
  61. If BCC <> "" Then .Bcc = ""
  62.  
  63. If Not AttachmentFiles Is Nothing Then
  64. iCnt = AttachmentFiles.Count - 1
  65. For i = 0 To iCnt
  66. If FileExists(AttachmentFiles(i)) Then _
  67. .Attachments.Add(AttachmentFiles(i))
  68. Next
  69.  
  70. End If
  71.  
  72. End With
  73.  
  74. If SMTPServer <> "" Then _
  75. SmtpMail.SmtpServer = SMTPServer
  76. SmtpMail.Send(myMessage)
  77.  
  78.  
  79. Catch myexp As Exception
  80. Throw myexp
  81. End Try
  82. End Sub
  83.  
  84. Private Function FileExists(ByVal FileFullPath As String) _
  85. As Boolean
  86. If Trim(FileFullPath) = "" Then Return False
  87.  
  88. Dim f As New IO.FileInfo(FileFullPath)
  89. Return f.Exists
  90.  
  91. End Function

猜你在找的VB相关文章