VB.NET发送电子邮件(通过使用SmtpClIEnt类)
以下代码已通过VS2008测试。
''' <summary> ''' 通过SmtpClIEnt类发送电子邮件 ''' </summary> ''' <param name="ReceiveAddressList">收件人地址列表</param> ''' <param name="Subject">邮件主题</param> ''' <param name="Content">邮件内容</param> ''' <param name="Attachfile">附件列表Hastable。KEY=文件名,Value文件路径</param> Private Function SendMail(ByVal ReceiveAddressList As List(Of String),ByVal Subject As String,ByVal Content As String,_ Optional ByVal Attachfile As Hashtable = nothing) As Boolean Dim i As Integer 'SMTP客户端 Dim smtp As New System.Net.Mail.SmtpClIEnt("SMTP.163.COM") 'smtp.Host = "smtp.163.com" 'SMTP服务器名称 '发件人邮箱身份验证凭证。 参数分别为 发件邮箱登录名和密码 smtp.Credentials = New System.Net.NetworkCredential("邮箱账户","邮箱密码") '创建邮件 Dim mail As New System.Net.Mail.MailMessage() '主题编码 mail.SubjectEnCoding = System.Text.EnCoding.GetEnCoding("GB2312") '正文编码 mail.BodyEnCoding = System.Text.EnCoding.GetEnCoding("GB2312") '邮件优先级 mail.Priority = System.Net.Mail.MailPriority.normal '以HTML格式发送邮件,为false则发送纯文本邮箱 mail.IsBodyHTML = True '发件人邮箱 mail.From = New System.Net.Mail.MailAddress("发件人邮箱") '添加收件人,如果有多个,可以多次添加 If ReceiveAddressList.Count = 0 Then Return False For i = 0 To ReceiveAddressList.Count - 1 mail.To.Add(ReceiveAddressList.Item(i)) Next '邮件主题和内容 mail.Subject = Subject mail.Body = Content '定义附件,参数为附件文件名,包含路径,推荐使用绝对路径 If Not Attachfile Is nothing AndAlso Attachfile.Count <> 0 Then For Each sKey As String In Attachfile.Keys Dim objfile As New System.Net.Mail.Attachment(Attachfile.Item(sKey)) '附件文件名,用于收件人收到附件时显示的名称 objfile.name = sKey '加入附件,可以多次添加 mail.Attachments.Add(objfile) Next End If '发送邮件 Try smtp.Send(mail) MessageBox.Show("邮件发送成功!") Return True Catch MessageBox.Show("邮件发送失败!") Return False Finally mail.dispose() End Try End Function总结
以上是内存溢出为你收集整理的VB.NET发送电子邮件(通过使用SmtpClient类)全部内容,希望文章能够帮你解决VB.NET发送电子邮件(通过使用SmtpClient类)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)