相信有很多朋友都使用過SmtpClient這個類別,最近正好有利用到,就順便記錄一下使用方式;利用這個類別來發送email是很輕鬆的,幾行程式碼就可以搞定,什麼?不相信..!? 好,就直接來看看程式碼吧
.net framework 2.0
相信有很多朋友都使用過SmtpClient這個類別,最近正好有利用到,就順便記錄一下使用方式;利用這個類別來發送email是很輕鬆的,幾行程式碼就可以搞定,什麼?不相信..!? 好,就直接來看看程式碼吧
Public Class Form1
Private _cMail As System.Net.Mail.SmtpClient
Private _MailCredentials As System.Net.NetworkCredential
Private _MailMessage As System.Net.Mail.MailMessage
Private _Attachment As System.Net.Mail.Attachment
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
''設定SMTP Server位置
_cMail = New System.Net.Mail.SmtpClient("Smtp server path")
''設定使用者名稱、密碼
_MailCredentials = New System.Net.NetworkCredential("userName", "Password")
_cMail.Credentials = _MailCredentials
End Sub
''直接發送mail
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
_cMail.Send("from@xxx.com.tw", "ToSomeOne@xxx.com.tw", "Subject", "Mail Body")
End Sub
''如果需要夾帶附件檔案
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
_MailMessage = New System.Net.Mail.MailMessage("from@xxx.com.tw", "ToSomeOne@xxx.com.tw", "Subject", "Mail Body")
_Attachment = New System.Net.Mail.Attachment("D:\txtfile.txt")
_MailMessage.Attachments.Add(_Attachment)
_cMail.Send(_MailMessage)
End Sub
End Class
是不是很簡單呢?快快樂樂的寫程式去吧~
PS:不要拿來亂發廣告信..orz