Send Email code in VB.NET
周伟泽
2023-12-01
Dim mailserver As String
mailserver ="test"
Private Function SendEmail(ByVal reportname As String, ByVal MailAddress As String, ByVal filename As String) As Boolean
'Send Email到指定Email Address
Try
Dim mesbody As String 'MAIL主体
mesbody = " test<BR><BR>"
mesbody += Errorinfo
Dim Message As System.Web.Mail.MailMessage = New System.Web.Mail.MailMessage
Message.To = MailAddress '接收人,多个用分号;隔开
Message.From = "Admin" '发送者
Message.Subject = reportname '标题
Message.Body = mesbody
Message.BodyFormat = MailFormat.Html '编码方式
Try
If System.IO.File.Exists(filename) Then '如果指定的附加文件存在,粘贴附件
Dim myAttachment As MailAttachment = New MailAttachment(filename)
Message.Attachments.Add(myAttachment)
End If
SmtpMail.SmtpServer = mailserver '设置MAIL服务器
SmtpMail.Send(Message)
Return True
Catch ex As Exception
StatusBar1.Panels(1).Text = "Error: " & ex.Message
Return False
End Try
Catch ex As Exception
Return False
End Try
End Function