发送带附件的邮件

Siz*_*z S 6 c# asp.net

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

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

System.Net.Mail.SmtpException: The operation has timed out.
Run Code Online (Sandbox Code Playgroud)

以下是我的代码:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Web配置

 <system.net>
<mailSettings>
  <smtp from="mailid">
    <network host="smtp.gmail.com" port="587" enableSsl="true" userName="username" password="pass" />
  </smtp>
</mailSettings>
Run Code Online (Sandbox Code Playgroud)

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

我该怎么做才能解决这个问题,或者我错过了什么?

Tim*_*hyP 2

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

解决该问题的一种方法是增加 SmtpClient 的超时值:

mSmtpClient.Timeout = int.MaxValue;
Run Code Online (Sandbox Code Playgroud)

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