微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

Asp.net 自动发送邮件方法

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

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

<system.net>

<mailSettings>

<smtp from="Emailname">

<network host="smtp.163.com" userName="Emailname" password="Emailpassword"

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

</smtp>

</mailSettings>

</system.net>

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

'''/ <summary>

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

''' </summary>

''' <param name="mailto">收件人地址。如:receiver@163.com</param>

''' <param name="mailsubject">邮件标题</param>

''' <param name="mailbody">邮件正文</param>

''' <param name="mailFrom">邮件发送人地址。如:sender@163.com</param>

''' <param name="list">附件路径</param>

''' <returns></returns>

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)

'如test@163.com,初步测试,用test@sina.com不行,用163的邮件服务器,就必须用163邮箱的用户名

'收件人地址

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

'如test@tom.com

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

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐