6

编辑我可以发送没有附件的邮件

尝试发送邮件时出现此错误:

System.Net.Mail.SmtpException: The operation has timed out.

以下是我的代码:

public static void SendMailMessage(string to, string subject, string body, List<string> attachment)
{
    MailMessage mMailMessage = new MailMessage();
    // string body; --> Compile time error, body is already defined as an argument

    mMailMessage.From = new MailAddress("abc@gmail.com");

    mMailMessage.To.Add(new MailAddress(to));

    mMailMessage.Subject = subject;

    mMailMessage.Body = body;

    foreach (string s in attachment)
    {
        var att = new Attachment(s);

        mMailMessage.Attachments.Add(att);

    }


    // Set the format of the mail message body as HTML
    mMailMessage.IsBodyHtml = true;
    // Set the priority of the mail message to normal
    mMailMessage.Priority = MailPriority.High;

    using (SmtpClient mSmtpClient = new SmtpClient())
    {
        mSmtpClient.Send(mMailMessage);
    }
}

网页配置

 <system.net>
<mailSettings>
  <smtp from="mailid">
    <network host="smtp.gmail.com" port="587" enableSsl="true" userName="username" password="pass" />
  </smtp>
</mailSettings>

注意:附件不超过其限制(低于 25 mb)

我能做些什么来解决这个问题,或者我错过了什么?

4

2 回答 2

2

所以基本上我们在聊天中发现问题的出现是因为附件的上传需要很长时间。

解决它的一种方法是增加 SmtpClient 的超时值:

mSmtpClient.Timeout = int.MaxValue;

注意:使用 int.MaxValue 进行测试,但对部署的解决方案使用更实际的值。

于 2013-03-06T15:06:08.930 回答
0

设置本地 smtp 服务器进行中继,或使用类似http://aws.amazon.com/ses/的东西。我不认为谷歌会允许你通过他们的服务器以编程方式中继。

于 2013-03-06T14:30:27.483 回答