你可以用System.Net.Mail命名空间完成发送邮件的功能。
参考代码:
Module Module1
Sub Main()
'' 发送测试邮件
''
Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
"Test HTML mail with attachments", "test email <br /> yeah!", _
"smtp.magnet.ie", , , New String() {"c:\file1.txt", _
"c:\file2.txt", "c:\file3.txt"})
Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
"Test text-only mail with attachments", "test email yeah!", _
"smtp.magnet.ie", False, , New String() {"c:\file1.txt", _
"c:\file2.txt", "c:\file3.txt"})
Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
"Test HTML mail with no attachments", _
"test email <br /> yeah!", "smtp.magnet.ie")
Mailer.SendMail("jamie@plenderj.com", "jamie@plenderj.com", _
"Test text-only with no attachments and authentication", _
"test email yeah!", "smtp.magnet.ie", , , , "MyUsername", "P@$$w0rd")
Mailer.SendMail(From:="jamie@plenderj.com", _
To:="jamie@plenderj.com", _
Subject:="Test text-only with no attachments and authentication #2", _
Body:="test email yeah!", _
Attachments:=Nothing, _
IsBodyHtml:=False, _
MailPort:=125, _
MailServer:="mail.magnet.ie")
End Sub
Class Mailer
''一个共享发送邮件方法
Shared Sub SendMail(ByVal [From] As String, ByVal [To] As String, _
ByVal Subject As String, ByVal Body As String, ByVal MailServer _
As String, Optional ByVal IsBodyHtml As Boolean = True, _
Optional ByVal MailPort As Integer = 25, _
Optional ByVal Attachments() As String = Nothing, Optional _
ByVal AuthUsername As String = Nothing, Optional ByVal _
AuthPassword As String = Nothing)
''创建一个 SmtpClient
对象,是得应用程序有权发送邮件
''e-mail 是用 Simple Mail Transfer Protocol (SMTP)协议.
Dim MailClient As System.Net.Mail.SmtpClient = _
New System.Net.Mail.SmtpClient(MailServer, MailPort)
Dim MailMessage = New System.Net.Mail.MailMessage( _
[From], [To], Subject, Body)
''设置一个值,表明邮件是不是Html形式的.
MailMessage.IsBodyHtml = IsBodyHtml
''设置证书
If (AuthUsername IsNot Nothing) AndAlso (AuthPassword _
IsNot Nothing) Then
MailClient.Credentials = New _
System.Net.NetworkCredential(AuthUsername, AuthPassword)
End If
''添加附件
If (Attachments IsNot Nothing) Then
For Each FileName In Attachments
MailMessage.Attachments.Add( _
New System.Net.Mail.Attachment(FileName))
Next
End If
MailClient.Send(MailMessage)
End Sub
End Class
End Module
如果您对我们的论坛在线支持服务有任何的意见或建议,请通过
邮件告诉我们。
立刻免费下载
MSDN 论坛好帮手