web-dev-qa-db-ja.com

SmtpClient使用時の「操作がタイムアウトしました」

私はC#フレームワークを使用してメールを送信する小さなアプリを作成しようとしています。ただし、機能しません。アプリは常に「操作がタイムアウトしました」と表示します。理由はありません。

これが私のコードです:

private void button1_Click(object sender, EventArgs e)
   {
     MailAddress fromAddress = new MailAddress("[email protected]");
     MailAddress toAddress = new MailAddress("[email protected]");

     MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);
     mail.Subject = "Testing";
     mail.Body = "contents.";

     SmtpClient client = new SmtpClient();
     client.Host = "smtp.gmail.com";
     client.Port = 587;
     client.EnableSsl = true;
     client.Timeout = 100;
     client.UseDefaultCredentials = false;
     client.Credentials = new NetworkCredential("username", "password");

     try
        {
            client.Send(mail);
            MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
        }

    }

ありがとう!

11
YU FENG

タイムアウト制限を高く設定してみてください。ミリ秒単位です。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace GmailSendTest
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MailAddress fromAddress = new MailAddress("[email protected]");
            MailAddress toAddress = new MailAddress("[email protected]");

            MailMessage mail = new MailMessage(fromAddress.Address, toAddress.Address);
            mail.Subject = "Testing";
            mail.Body = "contents.";

            SmtpClient client = new SmtpClient();
            client.Host = "smtp.gmail.com";
            client.Port = 587;
            client.EnableSsl = true;
            client.Timeout = 10000;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("myusername", "mypassword"); 

            try
            {
                client.Send(mail);
                MessageBox.Show("Mail Sent!", "Success", MessageBoxButtons.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK);
            }
        }
    }
}
10
Imre Greilich