none
Failure sending attachment mail using SMTP in C# RRS feed

  • Question

  • Hi 

    We are sending mail using SMTP class in a console application. We are sending the mails with an attachment in a loop. All the mails are sent successfully except the one and getting below exception. My assumption is it might be failing due the size of the file. The file size is almost 4.5 Mb . We checked but didn't find any  option to set the file size limit.

    We have tried to increase the data size in config too but no success. Added below in app.config

    <system.web>
        <httpRuntime maxRequestLength="1048576"/>
    </system.web>
      <system.webServer>
        <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="1073741824" />
          </requestFiltering>
        </security>
      </system.webServer>

    Error Message   : Failure sending mail.Inner Exception :System.IO.IOException: Unable to write data to the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
       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)
       --- End of inner exception stack trace ---
       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)
       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.Mime.EightBitStream.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.Mime.EightBitStream.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.Close()
       at System.Net.Mime.MimePart.Send(BaseWriter writer, Boolean allowUnicode)
       at System.Net.Mime.MimeMultiPart.Send(BaseWriter writer, Boolean allowUnicode)
       at System.Net.Mail.Message.Send(BaseWriter writer, Boolean sendEnvelope, Boolean allowUnicode)
       at System.Net.Mail.MailMessage.Send(BaseWriter writer, Boolean sendEnvelope, Boolean allowUnicode)
       at System.Net.Mail.SmtpClient.Send(MailMessage message)Strack Trace :   at System.Net.Mail.SmtpClient.Send(MailMessage message)

    Below is the code snippet:

    MailMessage mail = new MailMessage();

                    using (SmtpClient SmtpServer = new SmtpClient(MailSERVER, 25))
                    {
                        mail.From = new MailAddress(DisplayEmailAddress);
                        mail.To.Add(userEmailAddress);
                        mail.Subject = MailSubject;
                        mail.Body = MailBody;
                        System.Net.Mail.Attachment attachment;
                    
                        attachment = new System.Net.Mail.Attachment(filePath);
                        attachment.Name = filename;
                        mail.Attachments.Add(attachment);
                        
                        SmtpServer.UseDefaultCredentials = false;
                        SmtpServer.Credentials = new System.Net.NetworkCredential(MailUSERNAME, MailPASSWORD);
                        SmtpServer.EnableSsl = true;
                        SmtpServer.Send(mail);
                    }

    So, what change we can do to resolve the issue. 

    Tuesday, August 27, 2019 11:46 AM

All replies

  • I think in the most case the size limit is imposed by the mail server itself. In one of my previous company I've written mailing code to send email as large as 20MB without any issue. (Internal email only, and I've forgotten whether we had any special setting(s) done, chances are there aren't)

    Try using a mail address of that receipent's side to send email of this size on normal mail clients like Outlook or their webmail first, just to verify whether the email server, or the mail server's antivirus, have mail size restriction.

    Wednesday, August 28, 2019 1:16 AM
    Answerer