添加附件时SMTP邮件失败错误

时间:2014-10-21 06:51:23

标签: c# asp.net email smtp smtpclient

我无法发送带附件的邮件,但如果没有附件就可以正常工作。我想附加一个CSV文件。我继续获得“未能发送邮件”的异常

System.Net.NetworkCredential SMTPUserInfo = new System.Net.NetworkCredential(username, password);        
  emailClient.EnableSsl = true;
  emailClient.Credentials = SMTPUserInfo;
  emailClient.Port = Convert.ToInt32(port);
  MemoryStream ms = new MemoryStream();
  StreamWriter st = new StreamWriter(ms);
  st.Write("dsadasdasd,dasdasdas");
  st.Flush();
  st.Dispose();
  System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(sender, receiver,subject,emailBody);    
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(ms, "text/CSV"); 
 message.Attachments.Add(attach);
  emailClient.Send(message);

任何指针

1 个答案:

答案 0 :(得分:0)

检查附件文件大小...基本上IIS默认只允许4MB ... 有时可能导致邮件发送..... 首先检查文件大小是否小于4MB ..如果是成功发送,则问题是文件大小.. 要解决,请在webconfig中添加

<httpRuntime maxRequestLength="524288" executionTimeout="120000" />

和里面

<system.webServer>
<security>
            <requestFiltering>
                <requestLimits maxAllowedContentLength="524288000" />
            </requestFiltering>
        </security>
...........................................................................
......................................
</system.webServer>