LoginSignup
2
4

More than 5 years have passed since last update.

C#(.NET Core)で、Gmailからメールを送信する

Posted at

SmtpClientを使います。

System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient('smtp.gmail.com', 587);
smtp.EnableSsl = true;
smtp.Credentials = new System.Net.NetworkCredential("***id***", "***password***");

System.Net.Mail.MailMessage oMsg = new System.Net.Mail.MailMessage("from", "to", "subject", "body");
smtp.Send(oMsg);

Gmailの2段階認証に注意

上記プログラムでメール送信ができますが、Gmailの2段階認証を導入している場合、パスワードにGoogleアカウントパスワードを入れても、送信できません。

別途、以下のアプリパスワード設定画面から、アプリパスワードを発行します。

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4