[C#.NET][VB.NET] 使用 Gmail SMTP 送信

  • 44979
  • 0
  • C#
  • 2010-01-06

[C#.NET][VB.NET] 使用 Gmail SMTP 送信

  
{
    MailMessage mail = new MailMessage();
    NetworkCredential cred = new NetworkCredential("xxx@gmail.com", "password");
    //收件者
    mail.To.Add("ggyy@hotmail.com,ooxx@hotmail.com");
    mail.Subject = "subject";
    //寄件者
    mail.From = new System.Net.Mail.MailAddress("xxx@gmail.com");
    mail.IsBodyHtml = true;
    mail.Body = "message";
    //設定SMTP
    SmtpClient smtp = new SmtpClient("smtp.gmail.com");
    smtp.UseDefaultCredentials = false;
    smtp.EnableSsl = true;
    smtp.Credentials = cred;
    smtp.Port = 587;
    //送出Mail
    smtp.Send(mail);
}
VB
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click    Dim mail As New MailMessage()
    Dim cred As New NetworkCredential("xxx@gmail.com", "password")
    '收件者
    mail.[To].Add("ggyy@hotmail.com,ooxx@hotmail.com")
    mail.Subject = "subject"
    '寄件者
    mail.From = New System.Net.Mail.MailAddress("xxxu@gmail.com")
    mail.IsBodyHtml = True
    mail.Body = "message"
    '設定SMTP
    Dim smtp As New SmtpClient("smtp.gmail.com")
    smtp.UseDefaultCredentials = False
    smtp.EnableSsl = True
    smtp.Credentials = cred
    smtp.Port = 587
    '送出Mail
    smtp.Send(mail)
End Sub

若有謬誤,煩請告知,新手發帖請多包涵


Microsoft MVP Award 2010~2017 C# 第四季
Microsoft MVP Award 2018~2022 .NET

Image result for microsoft+mvp+logo