kfmlx kfmlx - 1 month ago 29
C# Question

Send Mail with Large Attachment using c#

I am trying to send email using Smtp using "smtp.gmail.com". Our project use .net 3.5 Framweork and c# to keep XP Compatibility. However, I always failed when the attachment is over 5MB. I read that google smtp allow to send attachment as large as 25MB. However, when I try to do it, it always throw error:

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)


This is the method I use to send mail:

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


I am not sure why is this happening.

Answer

This error thrown when the SMTP is force to stop when sending large attachment. The problem is not within the SMTP neither the Timeout. See:

 System.Threading.ThreadAbortException: Thread was being aborted

That is the reason the error shown. The thread that sending the email is aborted. See we might need to check more on the Thread problem.

Comments