2012年5月8日 星期二

用程式寄信 (gmail 發信)


Imports System.Text
Imports System.Net
Imports System.Net.Mail
Imports System.ComponentModel

public sub Send_mail()
' Mail Message Setting
Dim fromEmail As String = "xxxxx@gmail.com"
Dim fromName As String = "name"
Dim from As New MailAddress(fromEmail, fromName, Encoding.UTF8)

Dim toEmail As String = "xxxxx@hotmail.com"

Dim mail As New MailMessage(from, New MailAddress(toEmail))

Dim subject As String = "Test Subject"
mail.Subject = subject
mail.SubjectEncoding = Encoding.UTF8

Dim body As String = "Test Body"
mail.Body = body
mail.BodyEncoding = Encoding.UTF8
mail.IsBodyHtml = False
mail.Priority = MailPriority.High
mail.Attachments.Add(New Mail.Attachment("c:\temp.jpg"))
' SMTP Setting
Dim client As New SmtpClient()
client.Host = "smtp.gmail.com"
client.Port = 587
client.Credentials = New NetworkCredential("xxxxxx@gmail.com", "*******")
client.EnableSsl = True

' Send Mail
client.SendAsync(mail, mail)
AddHandler client.SendCompleted, AddressOf client_SendCompleted

' Sent Compeleted Eevet
end sub

Private Sub client_SendCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
If e.[Error] IsNot Nothing Then
MessageBox.Show(e.[Error].ToString())
Else
MessageBox.Show("Message sent.")
End If
End Sub

沒有留言:

張貼留言