[asp.net]用gmail發信(寄,smtp,mail,email,信,gmail)

  • 6112
  • 0

摘要:[asp.net]用gmail發信(寄,smtp,mail,email,信,gmail)


Dim objMail As New System.Net.Mail.MailMessage
Dim id As String = "yourAccount@gmail.com"
Dim passwd As String = "yourPassword"
Try
	With objMail
		.Body = "測試的body"""
		.Subject = "測試主題"                
		.From = New System.Net.Mail.MailAddress("ooooxxxx@gmail.com", "大帥哥")
		.IsBodyHtml = True
		.To.Add(New System.Net.Mail.MailAddress("abcdef@gmail.com", "另一個大帥哥"))
	End With
	Dim smtpMail As New System.Net.Mail.SmtpClient()
	smtpMail.Host = "smtp.gmail.com"
	smtpMail.Port = 25
	'smtpMail.Port = 587 企業的防火牆通常會擋住這種奇怪的port
	smtpMail.EnableSsl = True
	smtpMail.UseDefaultCredentials = False
	smtpMail.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network            
	smtpMail.Credentials = New System.Net.NetworkCredential(id, passwd)

	'smtpMail.SendAsync(objMail, "test")
	smtpMail.Send(objMail)
	MsgBox("mail success!!")

Catch ex As Exception
	MsgBox(ex.ToString)
End Try