2013-03-06 114 views
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("[email protected]"); 

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

Web配置

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

注:附件不超过(低于25 MB)的限制

我能做些什么来解决这个问题,还是我缺少什么?

+0

超时可能表示无法连接到邮件服务器。 – 2013-03-06 14:26:32

+2

gmail smtp不太可能离线,因此请尝试将enableSsl设置为false并进行测试。我相信他们使用TLS代替SSL发送 – TimothyP 2013-03-06 14:27:18

+0

好吧,显然你应该确实将ssl设置为true,你是否曾尝试从正在运行该软件的PC打开一个到SMTP的原始会话? – TimothyP 2013-03-06 14:30:50

回答

2

因此,基本上我们在聊天期间发现问题发生在 ,因为附件的上传需要很长时间。

一个解决它的办法就是增加SmtpClient的超时值:

mSmtpClient.Timeout = int.MaxValue; 

注意:使用int.MaxValue测试,但使用更现实的价值,为部署的解决方案。

0

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

+0

他们这样做,你从解析发送的域名您从中发送的计算机。事实上,我只是开了一个原始的腻子会话到587,他们让我连接没有任何问题。SMTP协议不提供谷歌的手段来查看你使用哪个程序连接到它,真正的SMTP服务器只是另一个程序。我们一直这样做 – TimothyP 2013-03-06 14:32:36