.net 自动发送邮件
2013-12-31 09:26
232 查看
1 构建一个类,包含发送邮件的对象和参数
2 函数调用
Imports System.Net.Mail Imports System.Net.Mime Public Class clsEMail Private Shared WithEvents TheSmtp As SmtpClient Private Shared YourSmtpServerName As String = "mail.adphk.com" Private Shared mSMTPPort As Integer = 25 'Private Shared YourSmtpServerName As String = "localhost" Private Shared mUserName As String = Nothing Private Shared mPassword As String = Nothing Public Shared Property SMTPPort As Integer Get Return mSMTPPort End Get Set(value As Integer) mSMTPPort = value End Set End Property Public Shared Property UserName As String Get Return mUserName End Get Set(value As String) mUserName = value End Set End Property Public Shared Property Password As String Get Return mPassword End Get Set(value As String) mPassword = value End Set End Property Public Shared Property SMTPServerName As String Get Return YourSmtpServerName End Get Set(value As String) YourSmtpServerName = value End Set End Property Public Shared Function SendThis(ByVal SubjectText As String, ByVal BodyText As String, ByVal FromAddress As String, _ ByVal ToAddress As String, Optional ByVal FileName As Collection = Nothing, Optional strCC As String = "", _ Optional strBCC As String = "", Optional blnHTMLBody As Boolean = False, Optional blnEnableSSL As Boolean = False) As Boolean Dim blnSendResult As Boolean = False Try 'Dim systemmailadd As System.Net.Mail.MailAddress Dim email As New Net.Mail.MailMessage() Dim strSplitTo() As String Dim strSplitCC() As String Dim strSplitBCC() As String Dim strToAddress As String = Replace(ToAddress, ",", ";") Dim strCCAddress As String = Replace(strCC, ",", ";") Dim strBCCAddress As String = Replace(strBCC, ",", ";") Dim i As Integer email.From = New MailAddress(FromAddress) strSplitTo = Split(ToAddress, ";") For i = 0 To UBound(strSplitTo) If strSplitTo(i).Trim <> "" Then email.To.Add(strSplitTo(i)) End If Next i strSplitCC = Split(strCCAddress, ";") For i = 0 To UBound(strSplitCC) If strSplitCC(i).Trim <> "" Then email.CC.Add(strSplitCC(i)) End If Next i strSplitBCC = Split(strBCCAddress, ";") For i = 0 To UBound(strSplitBCC) If strSplitBCC(i).Trim <> "" Then email.Bcc.Add(strSplitBCC(i)) End If Next i email.SubjectEncoding = System.Text.Encoding.UTF8 email.BodyEncoding = System.Text.Encoding.UTF8 email.IsBodyHtml = blnHTMLBody email.Subject = SubjectText email.Body = BodyText If Not FileName Is Nothing Then For Each Name As String In FileName Dim attach As New Net.Mail.Attachment(Name) 'Includes Path email.Attachments.Add(attach) Next For Each At As Attachment In email.Attachments At.TransferEncoding() = Net.Mime.TransferEncoding.Base64 Next End If 'Dim TheSmtp As New SmtpClient(YourSmtpServerName, 25) TheSmtp = New SmtpClient(YourSmtpServerName, mSMTPPort) 'uncomment and add the correct e-mail address and password for smtp authentication If Not mUserName Is Nothing Or mPassword Is Nothing Then TheSmtp.Credentials = New Net.NetworkCredential(mUserName, mPassword) End If TheSmtp.DeliveryMethod = SmtpDeliveryMethod.Network TheSmtp.EnableSsl = blnEnableSSL TheSmtp.Send(email) email.Attachments.Clear() TheSmtp = Nothing email = Nothing blnSendResult = True Catch ex As Exception blnSendResult = False System.Console.WriteLine(ex.Message) System.Console.Write(ex) System.Console.Write(vbCrLf) End Try Return blnSendResult End Function Private Shared Sub TheSmtp_SendCompleted(sender As Object, e As System.ComponentModel.AsyncCompletedEventArgs) Handles TheSmtp.SendCompleted System.Diagnostics.Debug.Print(e.ToString) End Sub End Class
2 函数调用
clsEMail.SMTPPort = 25 clsEMail.SMTPServerName = "192.168.1.3" clsEMail.UserName = "jimmylee@adphk.com" clsEMail.Password = "jimmylee" clsEMail.SendThis("中文繁體 JIM2", "<b>中文繁體2</b>", "jimmylee@adphk.com", "hardenchen@3ytechnology.com")
相关文章推荐
- .net自动发送邮件,适用于MD5或sha1加密后的密码取
- .net 自动向用户发送邮件
- .NET中如何用Outlook自动发送邮件(C#)
- VBS 自动发送邮件
- PHP自动发送邮件
- .NET实现发送邮件功能
- Java实现自动发送带附件的邮件(网易邮箱为例)
- 自动发送邮件方法
- 自动任务发送邮件优化
- .net 发送邮件
- ASP.NET自动发送邮件功能的实现
- EmEditor中自动发送邮件备份的VBScript宏脚本
- C# 定时自动发送邮件
- 用户验证信息邮件自动发送系统代码
- .net System.Web.Mail发送邮件 (设置发件人 只显示用户名)
- SVN Hook + Python实现commit后自动发送邮件
- ThinkPHP结合PHPMailer自动发送邮件功能
- Quality Center自动发送邮件配置
- 通用邮件自动发送Winform控件开发(五)