VB.New 自动发送邮件的方法

Asp.net 自动发送邮件的方法

今天有一个模块需要自动发送邮件的功能,就随便写了一个,记录一下作为积累。

一、首先需要配置web.config文件:

port="25" defaultCredentials="false"/>

二、然后编写发送邮件的函数:

'''/

''' 邮件发送方法(带附件)

'''

''' 收件人地址。如:[email protected]

''' 邮件标题

''' 邮件正文

''' 邮件发送人地址。如:[email protected]

''' 附件路径

'''

Public Function MySendMail(ByVal mailto As String, ByVal mailsubject As String, ByVal mailbody As String, ByVal mailFrom As String, ByVal list As ArrayList) As Boolean

Try

'邮件发送人地址

Dim from As New System.Net.Mail.MailAddress(mailFrom)

'如[email protected],初步测试,用[email protected]不行,用163的邮件服务器,就必须用163邮箱的用户名

'收件人地址

Dim [to] As New System.Net.Mail.MailAddress(mailto)

'如[email protected]

Dim mail As New System.Net.Mail.MailMessage(from, [to])

mail.Subject = mailsubject

mail.Body = mailbody

'以下设置服务器

Dim mySmth As New System.Net.Mail.SmtpClient()

'以下为增加附件

Dim count As Integer = list.Count

For i As Integer = 0 To count - 1

Dim data As New System.Net.Mail.Attachment(list(i).ToString())

mail.Attachments.Add(data)

Next

mySmth.Send(mail)

mail.Dispose()

Return True

Catch

Return False

End Try

End Function

三、最后就是对函数的调用了:

Dim mailSubject As String = "会员注册确认函"
Dim mailBody As String = "正文内容。"
Dim mailFrom As String = ConfigurationManager.AppSettings("SendMail")
Dim List As New ArrayList()
List.Add(Server.MapPath(ConfigurationManager.AppSettings("SendMailText")))

		'发送成功,进行相应处理
If MySendMail(Me.txtEmail.Text, mailSubject, mailBody, mailFrom, List) Then
Else

	'发送失败,进行相应处理
	Return
End If

你可能感兴趣的:(VB.New 自动发送邮件的方法)