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