Asp.net 使用Gmail smtp 寄信
(Imports System.Net.Mail)
黃底字是可修改的地方 Dim message As New MailMessage()
With message
.IsBodyHtml = True
.BodyEncoding = System.Text.Encoding.UTF8 'body編碼方式
.Body = "信件內容"
.Subject = "這是主旨" '主旨
.SubjectEncoding = System.Text.Encoding.UTF8 '主旨編碼方式
.From = New MailAddress("xxx@xxx.com.tw", "xx工作室") '寄件者
.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess
.Priority = MailPriority.Normal '優先權
.To.Add("abc@yahoo.com.tw") '收件者
End With
Dim emailClient As New SmtpClient
emailClient.DeliveryMethod = SmtpDeliveryMethod.Network
'Gmail 帳號 參數1:gmail帳號(信箱地址) 參數2:密碼
emailClient.Credentials = New System.Net.NetworkCredential("xxx@gmail.com", "xxx")
emailClient.Host = "smtp.gmail.com"
emailClient.Port = 587
emailClient.EnableSsl = True
Try
emailClient.Send(message)
Catch smtpExc As SmtpException
'發生寄信錯誤
ClientScript.RegisterStartupScript(Me.GetType(), "OhCrap", String.Format("alert('There was a problem in sending the email: {0}');", smtpExc.Message.Replace("'", "\'")), True)
Catch generalExc As Exception
'其他問題
ClientScript.RegisterStartupScript(Me.GetType(), "OhCrap", String.Format("alert('There was a general problem: {0}');", generalExc.Message.Replace("'", "\'")), True)
End Try
Recommend to Front page
未分類(1)
Comment Permissions: Allow commenting