2

I've tried multiple thing like:

  • Use different variations, ports, SSL on or off, ...
  • A different mail service provider
  • Setting the "Allow less secure apps" in my gmail account settings

Question:

  • How will I fix this error and send attachments successfully?
  • What causes this error?
  • Is it because the port may be blocked?

Error:

Exception thrown: 'System.IO.IOException' in System.dll
Exception thrown: 'System.Net.Mail.SmtpException' in System.dll
System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
   at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine)
   at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller)
   at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at TreeView.TreeView.<Email>d__28.MoveNext() in C:\Users\Alexander\Documents\Visual Studio 2015\Projects\TreeView\TreeView\TreeView\TreeView.cs:line 365

Code:

public void Email()
{
    try
    {
        SmtpClient client = new SmtpClient()
        {
            Host = @"smtp.gmail.com",
            Port = 465,
            Credentials = new NetworkCredential(@"mailid@gmail.com", @"Password"),
            UseDefaultCredentials = false,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            EnableSsl = true
        };

        MailMessage message = new MailMessage()
        {
            From = new MailAddress(@"mailid@gmail.com"),
            Subject = @"Test",
            Body = @"Test",
            Priority = MailPriority.Normal
        };

        message.To.Add(@"mailid@example.com");
        Attachment file = new Attachment(@"C:\Users\Alexander\Documents\File.txt");
        message.Attachments.Add(file);

        client.Send(message);
    }
    catch { }
}
HovyTech
  • 299
  • 5
  • 19

2 Answers2

2

For gmail I have

SmtpClient client = new SmtpClient();
client.Host = "smtp.gmail.com";

client.Port = 587;

client.EnableSsl = true;
client.Credentials = new NetworkCredential("mygmailusername@gmail.com", "mygmailpassword");
                ////email.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network ;

PS You may need to enable this:

https://support.google.com/accounts/answer/6010255?hl=en

APPEND:

Download this tool:

https://www.microsoft.com/en-us/download/details.aspx?id=24009

And see if you can query that port/address

I get the below results:

=============================================

 *Starting portqry.exe -n smtp.gmail.com -e 587 -p TCP ...
Querying target system called:
 smtp.gmail.com
Attempting to resolve name to IP address...
Name resolved to 74.125.29.109
querying...
TCP port 587 (unknown service): LISTENING
portqry.exe -n smtp.gmail.com -e 587 -p TCP exits with return code 0x00000000.*
granadaCoder
  • 26,328
  • 10
  • 113
  • 146
  • Try sending WITHOUT the attachment first...to verify the settings are correct. Then debug with the attachments. – granadaCoder Apr 04 '16 at 18:55
  • He already tried to enable the "Less secure apps", it's stated in the third bullet point in his question. :) – Visual Vincent Apr 04 '16 at 18:59
  • That is weird. I just retested my code and it works. – granadaCoder Apr 04 '16 at 19:02
  • I made an APPEND to test the port. – granadaCoder Apr 04 '16 at 19:04
  • OK, so there is a new error saying `The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required.`? – HovyTech Apr 04 '16 at 19:15
  • Is portqry(ui) is giving you that response .. or is the c#/smtp code is giving you that response? What is the portqry response? – granadaCoder Apr 04 '16 at 19:27
  • Zam. Well, the portqryui will usually give you the best error phrase to search on. While nit-picky, and hindsight 20/20...my code is setting values "one on one" instead of the "inline" properties....thus why my code was working for me. – granadaCoder Apr 04 '16 at 19:34
1

Setting the UseDefaultCredentials will always set the Credentials to null.

So you have to set the UseDefaultCredentials = true/false before you set the Credentials.

Reference: https://stackoverflow.com/a/27896975/5075227

Community
  • 1
  • 1
HovyTech
  • 299
  • 5
  • 19