2011年3月3日 星期四

發mail

'這裡需要一個Button

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim newMail As New System.Net.Mail.MailMessage
Dim ToAddress(,) As String = {{"to@yahoo.com.tw", "to"}, {"to@msa.hinet.net", "to"}}
Dim CCAddress(,) As String = {{"cc@yahoo.com.tw", "cc"}, {"cc@msa.hinet.net", "cc"}}
Dim BccAddress(,) As String = {{"bcc@yahoo.com.tw", "bcc"}, {"bcc@msa.hinet.net", "bcc"}}
Dim AttachFile() As String = {"C:\temp\123.xls", "C:\temp\456.xls"}
Dim smtpMail As New System.Net.Mail.SmtpClient

With newMail
.From = New System.Net.Mail.MailAddress("from@msa.hinet.net", "from") '寄件者
.Body = "Hello Every Body!!" '內文
.Subject = "測試資料!!" '主旨
.BodyEncoding = System.Text.Encoding.GetEncoding("BIG5") '編碼方式

For i As Int32 = 0 To ToAddress.GetUpperBound(1) '收信人
.To.Add(New System.Net.Mail.MailAddress(ToAddress(i, 0), ToAddress(i, 1)))
Next
For i As Int32 = 0 To CCAddress.GetUpperBound(1) '副本
.CC.Add(New System.Net.Mail.MailAddress(CCAddress(i, 0), CCAddress(i, 1)))
Next
For i As Int32 = 0 To BccAddress.GetUpperBound(1) '密件副本
.Bcc.Add(New System.Net.Mail.MailAddress(BccAddress(i, 0), BccAddress(i, 1)))
Next
For i As Int32 = 0 To BccAddress.GetUpperBound(1) '夾檔
.Attachments.Add(New System.Net.Mail.Attachment(AttachFile(i)))
Next
.IsBodyHtml = True '是否為HTML格式
.Priority = Net.Mail.MailPriority.Normal '優先權
End With
Try
smtpMail.Host = "msa.hinet.net"
smtpMail.SendAsync(newMail, "TEST")
Catch ex As Exception
MsgBox(ex.InnerException)
End Try
End Sub

沒有留言:

張貼留言