vb.net发送邮件

Module MyMail


    ''' <summary>
    ''' 通过SmtpClient类发送电子邮件
    ''' </summary>
    ''' <param name="ReceiveAddressList">收件人地址列表</param>
    ''' <param name="Subject">邮件主题</param>
    ''' <param name="Content">邮件内容</param>
    ''' <param name="AttachFile">附件列表Hastable。KEY=文件名,Value文件路径</param>
    ''' 
    Public Function SendMail(ByVal ReceiveAddressList() As 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.exmail.qq.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 UBound(ReceiveAddressList) = 0 Then Return False
        For i = 0 To UBound(ReceiveAddressList) - 1
            mail.To.Add(ReceiveAddressList(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

    Public Function SendMail_socket(ByVal ReceiveAddressList() As 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.exmail.qq.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 UBound(ReceiveAddressList) = 0 Then Return False
        For i = 0 To UBound(ReceiveAddressList)
            If ReceiveAddressList(i) <> "" Then
                mail.To.Add(ReceiveAddressList(i))
            End If
        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

    Public Function SendMail_Attach(ByVal ReceiveAddressList() As String, ByVal CCAddressList() As 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.exmail.qq.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.Length = 0 Then Return False
        For i = 0 To UBound(ReceiveAddressList)
            If ReceiveAddressList(i) <> "" Then
                mail.To.Add(ReceiveAddressList(i))
            End If
        Next

        If CCAddressList.Length = 0 Then Return False
        For i = 0 To UBound(CCAddressList)
            If CCAddressList(i) <> "" Then
                mail.CC.Add(CCAddressList(i))
            End If
        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
End Module

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值