AdHawkMailer - VB.NET 中的 ASP.NET 电子邮件发送组件






2.91/5 (10投票s)
AdHawkMailer 是一个用于在 VB.NET 中发送电子邮件的 ASP.NET 组件。
引言
如今,网站通过电子邮件、新闻通讯、促销优惠、状态更新等方式与用户进行沟通。这个小类可以放入任何 App_Code 文件夹或类库中,它将使您能够以编程方式发送电子邮件。我使用面向对象的设计,因为它将使实现和修改变得非常容易。
背景
这个想法是创建一个可重用的组件,在这种情况下,就是一个电子邮件发送器。我可以随时放入并使用的东西。我过去使用过 System.Net.Mail
命名空间,并认为它很棒,但大多数时候,它几乎是相同的代码。因此,我决定将其分解为面向对象编程,并构建一个可重用的类。
Message
对象具有以下属性
- 发件人地址 -
String
- 收件人地址 -
String
- 正文是否为 HTML -
Boolean
- 主题 -
String
- 正文 -
String
- 附件 - 文件路径的
ArrayList
。 - SMTP 地址 -
String
- 端口 -
Integer
- 用户名 -
String
- 密码 -
String
- 启用 SSL -
Boolean
使用代码
这是一个使用该代码的非常简单的示例。我在顶部导入命名空间,并在按钮单击事件上发送电子邮件。但是,在这里我只分配了一个收件人地址,因为我在默认构造函数中设置了所有其他内容。
Imports AdHawkMailer
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) _
Handles Button1.Click
Dim objMessage As New Message
objMessage.ToAddress = "adam.n.thompson@gmail.com"
Dim mailSent As Boolean = objMessage.Send(objMessage)
End Sub
End Class
这是类
Imports Microsoft.VisualBasic
Imports System.Net.Mail
Imports System
Namespace AdHawkMail
Public Class Message
''' <summary>
''' These are the properties needed for SMTP email communication.
''' Most of them are self explainitory.
''' ==================================================================
''' FromAddress - Sets the from address that will show on the email sent
''' ToAddress - The Address of the Recipiant
''' IsBodyHtml - True if the body is going to be html / False otherwise
''' Subject - The Subject of the email
''' Body - Email body text/html
''' Attachments = You can send a collection of attachments
''' SmtpAddress - The IP address or URL of the smtp server you will be sending from
''' Port - The port number you will be using
''' UserName - If authentication is required
''' Password - If authentication is required
''' SslEnabled - If ssl is required
''' </summary>
''' <remarks></remarks>
#Region "Private Members and Public Properties"
Private _fromAddress As String
Private _toAddress As String
Private _isBodyHtml As Boolean
Private _subject As String
Private _body As String
Private _smtpAddress As String
Private _port As Integer
Private _userName As String
Private _password As String
Private _sslEnabled As Boolean
Private _attachments As ArrayList
Public Property FromAddress() As String
Get
Return _fromAddress
End Get
Set(ByVal value As String)
_fromAddress = value
End Set
End Property
Public Property ToAddress() As String
Get
Return _toAddress
End Get
Set(ByVal value As String)
_toAddress = value
End Set
End Property
Public Property IsBodyHtml() As Boolean
Get
Return _isBodyHtml
End Get
Set(ByVal value As Boolean)
_isBodyHtml = value
End Set
End Property
Public Property Subject() As String
Get
Return _subject
End Get
Set(ByVal value As String)
_subject = value
End Set
End Property
Public Property Body() As String
Get
Return _body
End Get
Set(ByVal value As String)
_body = value
End Set
End Property
Public Property SmtpAddress() As String
Get
Return _smtpAddress
End Get
Set(ByVal value As String)
_smtpAddress = value
End Set
End Property
Public Property Port() As Integer
Get
Return _port
End Get
Set(ByVal value As Integer)
_port = value
End Set
End Property
Public Property UserName() As String
Get
_userName
End Get
Set(ByVal value As String)
_userName = value
End Set
End Property
Public Property Password() As String
Get
Return _password
End Get
Set(ByVal value As String)
_password = value
End Set
End Property
Public Property SslEnabled() As Boolean
Get
Return _sslEnabled
End Get
Set(ByVal value As Boolean)
_sslEnabled = value
End Set
End Property
#End Region
''' <summary>
''' This constructor is where you would set the SMTP configuration,
''' and anything that you know will not be changing.
''' You can override this constructor as well to allow for multiple
''' configurations
''' </summary>
''' <remarks></remarks>
#Region "Constructors"
Public Sub New()
FromAddress = "server@exadev.com"
ToAddress = "darrylfluhart@yahoo.com"
IsBodyHtml = True
Subject = ""
Body = ""
SmtpAddress = "mail.exadev.com"
Port = 25
UserName = Nothing
Password = Nothing
SslEnabled = False
End Sub
#End Region
''' <summary>
''' This is the send Method. it takes a message object.
''' </summary>
''' <param name="_emailMessage">
''' Some of the properties are optional
'''=================================================================
''' Attachments - The code will check to see if you have any attachment
''' in the arrayList
''' UserName - if authentication is not required set the value to Nothing
''' Password - if authentication is not required set the value to Nothing
''' </param>
''' <returns>True if the message was sent, and false otherwise</returns>
''' <remarks></remarks>
#Region "Methods"
Public Sub AddAttachment(ByVal attachment As String)
_attachments.Add(attachment)
End Sub
Public Sub RemoveAttachment(ByVal attachment As String)
_attachments.Remove(attachment)
End Sub
Public Function Send(ByVal _emailMessage As Message) As Boolean
Dim mailSent As Boolean
Try
Dim Mail As New System.Net.Mail.MailMessage()
With Mail
.To.Add(_emailMessage.ToAddress)
.From = New MailAddress(_emailMessage.FromAddress)
.Subject = _emailMessage.Subject
.Body = _emailMessage.Body
.IsBodyHtml = _emailMessage.IsBodyHtml
If Not _attachments.Count = 0 Then
Dim i As Integer = 0
For Each objAttachment As Object In _attachments
.Attachments.Add(_attachments(i))
i += 1
Next
End If
End With
Dim SMTP As New SmtpClient(_emailMessage.SmtpAddress)
If Not _emailMessage.UserName = Nothing _
And _emailMessage.Password = Nothing Then
SMTP.Credentials = New Net.NetworkCredential _
(_emailMessage.UserName, _emailMessage.Password)
End If
SMTP.Port = _emailMessage.Port
SMTP.Host = _emailMessage.SmtpAddress
SMTP.EnableSsl = _emailMessage.SslEnabled
SMTP.Send(Mail)
SMTP = Nothing
Mail.Dispose()
mailSent = True
Catch ex As Exception
mailSent = False
End Try
Return mailSent
End Function
#End Region
End Class
End Namespace
祝您编码愉快!