2

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

どんな助けでも大歓迎です

4

3 に答える 3

0

これを試して...

 protected void Button1_Click(object sender, EventArgs e)
{
 MailMessage mail = new MailMessage();
 mail.To.Add("Email ID where email is to be send");
 mail.To.Add("Another Email ID where you wanna send same email");
 mail.From = new MailAddress("YourGmailID@gmail.com");
 mail.Subject = "Email using Gmail";

 string Body = "Hi, this mail is to test sending mail"+ 
             "using Gmail in ASP.NET";
 mail.Body = Body;

  mail.IsBodyHtml = true;
 SmtpClient smtp = new SmtpClient();
 smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
 smtp.Credentials = new System.Net.NetworkCredential
   ("YourUserName@gmail.com","YourGmailPassword");
//Or your Smtp Email ID and Password
 smtp.EnableSsl = true;
 smtp.Send(mail);
 }
于 2013-02-19T10:50:55.353 に答える
0

Rupのアドバイスに従ってウイルス対策プログラムをオフにしましたが、例外はありません。
ASP .NetWebサイトとGmailSMTPサーバー間の通信を許可するために、ウイルス対策プログラムでカスタムルールを作成します。

于 2013-02-19T20:56:49.903 に答える
0

ターゲット コンピュータが接続を拒否したため、接続が確立されていません73.194.67.108:587

他の SMTP ポートを試します。465

于 2013-02-19T10:54:42.093 に答える