【问题标题】:"The operations timed out" when using SmtpClient使用 SmtpClient 时“操作超时”
【发布时间】:2012-10-21 08:25:27
【问题描述】:

我正在尝试使用 C# 框架创建一个小型应用程序来发送电子邮件。但是,它不起作用。该应用程序总是给我“操作超时”。我不知道为什么。

这是我的代码:

private void button1_Click(object sender, EventArgs e)
   {
     MailAddress fromAddress = new MailAddress("from@gmail.com");
     MailAddress toAddress = new MailAddress("to@gmail.com");

     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);
        }

    }

谢谢!

【问题讨论】:

  • 此处提供的 SMTP 信息是否合适?
  • 尝试设置更高的超时限制。以毫秒为单位。
  • SmtpClient 实现 IDisposable 并且应该在 using 块中。
  • @ImreGreilich 您应该将其发布为答案。

标签: c# .net email smtp gmail


【解决方案1】:

尝试设置更高的超时限制。以毫秒为单位。

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("myusername@gmail.com");
            MailAddress toAddress = new MailAddress("myusername@gmail.com");

            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);
            }
        }
    }
}

【讨论】:

  • 它仍然给我“操作超时”。是因为我使用 c# 框架而不是 html 吗?我看到一些 cmets 说因为 smpt 服务器无法连接。这是正确的原因吗??
  • 很可能是的,它不会连接到 SMTP 服务器。尝试使用端口 465。我发现这应该用于 SSL 连接。
  • 我试过你的代码。仅将超时更改为 10000(当然还有发送电子邮件、收件人、用户名和密码)。它工作得很好。你是如何使用用户名的?只需要 @ 之前的文本(“from@gmail.com”中的“from”)。
  • 我认为您不需要更改 Gmail 中的任何设置。尝试打开一个命令窗口,然后输入:telnet smtp.gmail.com 587。它应该清理窗口并像这样写一行:220 mx.google.com ESMTP...。如果您收到一条错误消息,那么似乎有什么东西阻止了您与 gmail smtp 服务器的连接。
  • 我遇到了同样的问题。我使用的是 465 端口,当我切换到 587 端口时它工作了。我的电子邮件客户端 (Outlook) 在端口 465 上运行良好。也许这就是问题所在。我想知道使用相同端口的两个应用程序是否存在冲突。端口 25 对我不起作用。它没有超时,但说它无法连接。
猜你喜欢
  • 1970-01-01
  • 2012-05-15
  • 1970-01-01
  • 2020-08-23
  • 2012-08-30
  • 1970-01-01
  • 1970-01-01
  • 2016-11-08
  • 2013-07-14
相关资源
最近更新 更多