VB.NET如何实现发送mail 给多个收件者 并带附件

前端之家收集整理的这篇文章主要介绍了VB.NET如何实现发送mail 给多个收件者 并带附件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

熟悉了下用VB.NET如何发送邮件并带附件,

经个人测试OK

界面如下:

后台代码

Imports System.Net
Imports System.Net.Mail
Public Class Form1

    Dim mms As New System.Net.Mail.MailMessage
    Dim smt As New System.Net.Mail.SmtpClient() 'SMTP Server
    Dim maillist As String
    Dim mSplit As Array
    Dim mlist As New ArrayList
    Dim mMailAddr As String
    Private Sub Button1_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button1.Click

        send_mail("test Mail  " & Now,"Hello<br>" & Now & "<br>Comput Name :" & My.Computer.Name & "<br> Mail server name :" & txt_server.Text.Trim)
        Close()
    End Sub
    Private Sub send_mail(ByVal strSubject As String,ByVal mFile As String)
        Dim madFrom As New System.Net.Mail.MailAddress(TextBox1.Text.Trim) '寄件人
        Dim i,j As Integer
        smt.Host = txt_server.Text.Trim '發信主機名稱
        mms.From = madFrom
        ' mms.To.Add(txt_mail.Text.Trim)
        maillist = txt_mail.Text.Trim
        mSplit = Trim(maillist.ToLower).Split(",")
        If mSplit.Length > 0 Then
            For j = 0 To mSplit.Length - 1
                If mSplit(j).ToString.Trim <> "" Then
                    If InStr(mSplit(j).ToString.Trim,"@") = 0 Then '用户输入@前面的就可以了,系统会自动加上下面的邮箱后缀
                        mSplit(j) &= "@hotmail.com"    '自己设定如果是公司内部用,就写公司邮箱后缀
                    End If
                    mlist.Add(mSplit(j).ToString.Trim)
                End If
            Next
        End If
        If mlist.Count <> 0 Then
            For i = 0 To mlist.Count - 1
                mMailAddr = Trim(mlist.Item(i))
                If mMailAddr.Trim <> "" Then
                    If InStr(mMailAddr,"@") = 0 Then
                        mMailAddr &= "@hotmail.com"
                    End If
                    mms.To.Add(mMailAddr)
                End If
            Next
        End If

        mms.Subject = strSubject.Replace(Chr(10),"").Replace(Chr(13),"") '信件主旨  'charycheng 20120510 +  
        Dim attach As Net.Mail.Attachment = New Net.Mail.Attachment(TextBox2.Text)
        mms.Attachments.Add(attach)

        mms.Body = "<BR> <table>Test Mail If you have any questions,please contact Chary <br> Thanks! " '信件內容
        mms.IsBodyHtml = True

        Try
            smt.Send(mms)
            Label4.Text = "MSG:Send Mail OK!"
        Catch ex As Exception
            MsgBox(ex.Message)
        Finally
            mms.Dispose()

        End Try
    End Sub





    Private Sub Button2_Click(ByVal sender As System.Object,ByVal e As System.EventArgs) Handles Button2.Click
        If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
            TextBox2.Text = OpenFileDialog1.FileName
        End If

    End Sub
End Class
原文链接:https://www.f2er.com/vb/260010.html

猜你在找的VB相关文章