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

相关文章

Format[$] ( expr [ , fmt ] ) format 返回变体型 format$ 强...
VB6或者ASP 格式化时间为 MM/dd/yyyy 格式,竟然没有好的办...
在项目中添加如下代码:新建窗口来显示异常信息。 Namespace...
转了这一篇文章,原来一直想用C#做k3的插件开发,vb没有C#用...
Sub 分列() ‘以空格为分隔符,连续空格只算1个。对所选...
  窗体代码 1 Private Sub Text1_OLEDragDrop(Data As Dat...