1 构建一个类,包含发送邮件的对象和参数
Imports System.Net.Mail
Imports System.Net.Mime
Public Class clsEMail
Private Shared WithEvents TheSmtp As SmtpClient
Private Shared YourSmtpServerName As String = "mail.adphk.com"
Private Shared mSMTPPort As Integer = 25
'Private Shared YourSmtpServerName As String = "localhost"
Private Shared mUserName As String = Nothing
Private Shared mPassword As String = Nothing
Public Shared Property SMTPPort As Integer
Get
Return mSMTPPort
End Get
Set(value As Integer)
mSMTPPort = value
End Set
End Property
Public Shared Property UserName As String
Get
Return mUserName
End Get
Set(value As String)
mUserName = value
End Set
End Property
Public Shared Property Password As String
Get
Return mPassword
End Get
Set(value As String)
mPassword = value
End Set
End Property
Public Shared Property SMTPServerName As String
Get
Return YourSmtpServerName
End Get
Set(value As String)
YourSmtpServerName = value
End Set
End Property
Public Shared Function SendThis(ByVal SubjectText As String, ByVal BodyText As String, ByVal FromAddress As String, _
ByVal ToAddress As String, Optional ByVal FileName As Collection = Nothing, Optional strCC As String = "", _
Optional strBCC As String = "", Optional blnHTMLBody As Boolean = False, Optional blnEnableSSL As Boolean = False) As Boolean
Dim blnSendResult As Boolean = False
Try
'Dim systemmailadd As System.Net.Mail.MailAddress
Dim email As New Net.Mail.MailMessage()
Dim strSplitTo() As String
Dim strSplitCC() As String
Dim strSplitBCC() As String
Dim strToAddress As String = Replace(ToAddress, ",", ";")
Dim strCCAddress As String = Replace(strCC, ",", ";")
Dim strBCCAddress As String = Replace(strBCC, ",", ";")
Dim i As Integer
email.From = New MailAddress(FromAddress)
strSplitTo = Split(ToAddress, ";")
For i = 0 To UBound(strSplitTo)
If strSplitTo(i).Trim <> "" Then
email.To.Add(strSplitTo(i))
End If
Next i
strSplitCC = Split(strCCAddress, ";")
For i = 0 To UBound(strSplitCC)
If strSplitCC(i).Trim <> "" Then
email.CC.Add(strSplitCC(i))
End If
Next i
strSplitBCC = Split(strBCCAddress, ";")
For i = 0 To UBound(strSplitBCC)
If strSplitBCC(i).Trim <> "" Then
email.Bcc.Add(strSplitBCC(i))
End If
Next i
email.SubjectEncoding = System.Text.Encoding.UTF8
email.BodyEncoding = System.Text.Encoding.UTF8
email.IsBodyHtml = blnHTMLBody
email.Subject = SubjectText
email.Body = BodyText
If Not FileName Is Nothing Then
For Each Name As String In FileName
Dim attach As New Net.Mail.Attachment(Name) 'Includes Path
email.Attachments.Add(attach)
Next
For Each At As Attachment In email.Attachments
At.TransferEncoding() = Net.Mime.TransferEncoding.Base64
Next
End If
'Dim TheSmtp As New SmtpClient(YourSmtpServerName, 25)
TheSmtp = New SmtpClient(YourSmtpServerName, mSMTPPort)
'uncomment and add the correct e-mail address and password for smtp authentication
If Not mUserName Is Nothing Or mPassword Is Nothing Then
TheSmtp.Credentials = New Net.NetworkCredential(mUserName, mPassword)
End If
TheSmtp.DeliveryMethod = SmtpDeliveryMethod.Network
TheSmtp.EnableSsl = blnEnableSSL
TheSmtp.Send(email)
email.Attachments.Clear()
TheSmtp = Nothing
email = Nothing
blnSendResult = True
Catch ex As Exception
blnSendResult = False
System.Console.WriteLine(ex.Message)
System.Console.Write(ex)
System.Console.Write(vbCrLf)
End Try
Return blnSendResult
End Function
Private Shared Sub TheSmtp_SendCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles TheSmtp.SendCompleted
System.Diagnostics.Debug.Print(e.ToString)
End Sub
End Class
2 函数调用
clsEMail.SMTPPort = 25
clsEMail.SMTPServerName = "192.168.1.3"
clsEMail.UserName = "jimmylee@adphk.com"
clsEMail.Password = "jimmylee"
clsEMail.SendThis("中文繁體 JIM2", "<b>中文繁體2</b>", "jimmylee@adphk.com", "hardenchen@3ytechnology.com")