ASP.NetWebサイトを開発しています。
ASPXページの1つにボタンが含まれています。
このボタンをクリックすると、GmailSMTPサーバーを使用してメールを送信したいと思います。
Gmailアカウントをクレデンシャルとして使用したい。
ボタンのクリックイベントハンドラは次のとおりです。
protected void m_myButton_Click(object p_sender, EventArgs p_args)
{
string l_subject = "My subject";
StringBuilder l_builder = new StringBuilder();
// ...
string l_body = l_builder.ToString();
string l_host = ConfigurationManager.AppSettings["SmtpHost"];
int l_port = int.Parse(ConfigurationManager.AppSettings["SmtpPort"]);
NameValueCollection l_smtpAccount = (NameValueCollection)ConfigurationManager.GetSection("smtpAccount");
string l_address = l_smtpAccount["SmtpAddress"];
string l_password = l_smtpAccount["SmtpPassword"];
MailMessage l_mail = new MailMessage();
l_mail.From = new MailAddress(l_address);
l_mail.To.Add(l_address);
l_mail.Subject = l_subject;
l_mail.Body = l_body;
SmtpClient l_smtp = new SmtpClient();
l_smtp.Host = l_host; // l_host = "smtp.gmail.com"
l_smtp.Port = l_port; // l_port = 587
l_smtp.EnableSsl = true;
l_smtp.UseDefaultCredentials = false;
l_smtp.Credentials = new NetworkCredential(l_address, l_password);
l_smtp.Send(l_mail);
}
コマンドラインl_smtp.Send(l_mail)が失敗します。
SMTP例外が発生し、InnerExceptionプロパティが次のように通知します(フランス語から英語への翻訳):ターゲットコンピューターが接続を拒否したため、接続が確立されていません173.194.67.108:587
私のコードは、このページにあるコードに基づいています:http: //www.codeproject.com/Questions/254743/sending-email-in-asp-net-using-smtp-gmail-server
どんな助けでも大歓迎です