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



我正在尝试使用Smtp使用"smtp.gmail.com"发送电子邮件。我们的项目使用。net 3.5框架和c#来保持XP的兼容性。但是,当附件超过5MB时,我总是失败。我读到谷歌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,也不在于Timeout。看到:

 System.Threading.ThreadAbortException: Thread was being aborted

这就是显示错误的原因。发送邮件的线程被终止。看,我们可能需要更多地检查线程问题

相关内容

  • 没有找到相关文章

最新更新