使用 C# 发送带有大附件的邮件

Send Mail with Large Attachment using c#

我正在尝试使用 "smtp.gmail.com" 使用 Smtp 发送电子邮件。我们的项目使用 .net 3.5 Framweork 和 c# 来保持 XP 兼容性。但是,当附件超过 5MB 时,我总是失败。我读到 google smtp 允许发送最大为 25MB 的附件。但是,当我尝试这样做时,它总是抛出错误:

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: The write operation failed, see inner exception. ---> System.Threading.ThreadAbortException: Thread was being aborted.
   at System.Net.UnsafeNclNativeMethods.OSSOCK.send(IntPtr socketHandle, Byte* pinnedBuffer, Int32 len, SocketFlags socketFlags)
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags, SocketError& errorCode)
   at System.Net.Sockets.Socket.Send(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags)
   at System.Net.Sockets.NetworkStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.Security._SslStream.StartWriting(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   --- End of inner exception stack trace ---
   at System.Net.Security._SslStream.ProcessWrite(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)
   at System.Net.TlsStream.Write(Byte[] buffer, Int32 offset, Int32 size)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.DelegatedStream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Base64Stream.FlushInternal()
   at System.Net.Base64Stream.Write(Byte[] buffer, Int32 offset, Int32 count)
   at System.Net.Mime.MimePart.Send(BaseWriter writer)
   at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer)
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

这是我用来发送邮件的方法:

public void Send( SMTPServerInfo serverInfo, string from, string to, string subject, string body, IList<Attachment> attachments )
{
  SmtpClient client = new SmtpClient();
  client.EnableSsl = serverInfo.EnableSSL;
  MailMessage message = new MailMessage( from, to, subject, body );
  if( IsHTML )
    message.IsBodyHtml = true;
  client.Host = serverInfo.ServerName;
  if( !Text.IsEmpty( serverInfo.UserName ) )
    client.Credentials = new NetworkCredential( serverInfo.UserName, serverInfo.Password );
  if( serverInfo.Port.HasValue )
    client.Port = serverInfo.Port.Value;
  if( attachments != null )
    foreach( Attachment attachment in attachments )
      message.Attachments.Add( attachment );

  client.Send( message );
  message.Dispose();
}

我不确定为什么会这样。

您必须像这样实例化邮件:

System.Net.Mail.Attachment attachment1;
 attachment1 = new System.Net.Mail.Attachment(attachmentFilePath_Here);
 message.Attachments.Add(attachment1);

当 SMTP 在发送大附件时强制停止时引发此错误。问题不在 SMTP 内,也不在超时内。参见:

 System.Threading.ThreadAbortException: Thread was being aborted

这就是显示错误的原因。发送电子邮件的线程已中止。看到我们可能需要检查更多线程问题。