vb.net发送邮件

来源:互联网 发布:布雷德索数据 编辑:程序博客网 时间:2024/04/30 16:25
  1. ''' <summary>  
  2.         ''' 通过SmtpClient类发送电子邮件  
  3.         ''' </summary>  
  4.         ''' <param name="ReceiveAddressList">收件人地址列表</param>  
  5.         ''' <param name="Subject">邮件主题</param>  
  6.         ''' <param name="Content">邮件内容</param>  
  7.         ''' <param name="AttachFile">附件列表Hastable。KEY=文件名,Value文件路径</param>  
  8.         Private Function SendMail(ByVal ReceiveAddressList As List(Of String), ByVal Subject As StringByVal Content As String, _  
  9.                                   Optional ByVal AttachFile As Hashtable = NothingAs Boolean  
  10.             Dim i As Integer  
  11.             'SMTP客户端  
  12.             Dim smtp As New System.Net.Mail.SmtpClient("SMTP.163.COM")  
  13.             'smtp.Host = "smtp.163.com"       'SMTP服务器名称  
  14.             '发件人邮箱身份验证凭证。 参数分别为 发件邮箱登录名和密码  
  15.             smtp.Credentials = New System.Net.NetworkCredential("邮箱账户""邮箱密码")  
  16.             '创建邮件  
  17.             Dim mail As New System.Net.Mail.MailMessage()  
  18.             '主题编码  
  19.             mail.SubjectEncoding = System.Text.Encoding.GetEncoding("GB2312")  
  20.             '正文编码  
  21.             mail.BodyEncoding = System.Text.Encoding.GetEncoding("GB2312")  
  22.             '邮件优先级  
  23.             mail.Priority = System.Net.Mail.MailPriority.Normal  
  24.             '以HTML格式发送邮件,为false则发送纯文本邮箱  
  25.             mail.IsBodyHtml = True  
  26.             '发件人邮箱  
  27.             mail.From = New System.Net.Mail.MailAddress("发件人邮箱")  
  28.   
  29.             '添加收件人,如果有多个,可以多次添加  
  30.             If ReceiveAddressList.Count = 0 Then Return False  
  31.             For i = 0 To ReceiveAddressList.Count - 1  
  32.                 mail.To.Add(ReceiveAddressList.Item(i))  
  33.             Next  
  34.   
  35.             '邮件主题和内容  
  36.             mail.Subject = Subject  
  37.             mail.Body = Content  
  38.   
  39.             '定义附件,参数为附件文件名,包含路径,推荐使用绝对路径  
  40.             If Not AttachFile Is Nothing AndAlso AttachFile.Count <> 0 Then  
  41.                 For Each sKey As String In AttachFile.Keys  
  42.                     Dim objFile As New System.Net.Mail.Attachment(AttachFile.Item(sKey))  
  43.                     '附件文件名,用于收件人收到附件时显示的名称  
  44.                     objFile.Name = sKey  
  45.                     '加入附件,可以多次添加  
  46.                     mail.Attachments.Add(objFile)  
  47.                 Next  
  48.             End If  
  49.   
  50.             '发送邮件  
  51.             Try  
  52.                 smtp.Send(mail)  
  53.                 MessageBox.Show("邮件发送成功!")  
  54.                 Return True  
  55.             Catch  
  56.                 MessageBox.Show("邮件发送失败!")  
  57.                 Return False  
  58.             Finally  
  59.                 mail.Dispose()  
  60.             End Try  
  61.         End Function  
转自:http://blog.csdn.net/pashine/article/details/18048487
0 0
原创粉丝点击