0

We have a desktop application that uses a dll to send email. The dll uses .net framework 4.0 smtp client. After enabled 2F authentication on gmail account and created an app password, we are no longer able to send email. The error reported in application's log is:​

"System.Net.Mail.SmtpException: Error sending mail. ---> System.IO.IOException: Unable to read data from transport connection: net_io_connectionclosed".

Do we need any other configurations to allow sending email using app password? Regards

EDIT

This is the Client class.

public Client()
    {
        _credentials = new NetworkCredential();
        _client = new SmtpClient()
        {
            UseDefaultCredentials = false,
            Credentials = _credentials,
            DeliveryMethod = SmtpDeliveryMethod.Network
        };
        Initialize();
        RefreshPort();
    }

    private SmtpClient _client;
    private NetworkCredential _credentials;
    private int? _port;

    public bool Ssl
    {
        get { return _client.EnableSsl; }
        set
        {
            _client.EnableSsl = value;
            RefreshPort();
        }
    }
    public string Host
    {
        get { return _client.Host; }
        set { _client.Host = value; }
    }
    public int? Port
    {
        get { return _port ?? _client.Port; }
        set
        {
            _port = value;
            RefreshPort();
        }
    }
    public string Username
    {
        get { return _credentials.UserName; }
        set { _credentials.UserName = value; }
    }
    public string Password
    {
        get { return _credentials.Password; }
        set { _credentials.Password = value; }
    }
    public TimeSpan Timeout
    {
        get { return TimeSpan.FromMilliseconds(_client.Timeout); }
        set { _client.Timeout = (int)value.TotalMilliseconds; }
    }
    public string DisplayName { get; set; }

    private void RefreshPort()
    {
        _client.Port = _port ?? (Ssl ? 587 : 25);
    }

And this is the method to send the email

public void Send(Message message)
    {
        using (MailMessage mail = new MailMessage()
        {
            From = new MailAddress(Username, DisplayName),
            Subject = message.Subject,
            Body = message.Body,
            IsBodyHtml = message.Html
        })
        {
            message.To?.ForEach(to => mail.To.Add(new MailAddress(to)));
            message.CC?.ForEach(cc => mail.CC.Add(new MailAddress(cc)));
            message.BCC?.ForEach(bcc => mail.Bcc.Add(new MailAddress(bcc)));
            message.Attachments?.ForEach(attachment => mail.Attachments.Add(new Attachment(attachment)));
            if (message.Encoding != null)
            {
                mail.SubjectEncoding = message.Encoding;
                mail.BodyEncoding = message.Encoding;
            }
            _client.Send(mail);
        }
    }
Devend
  • 1
  • 1

1 Answers1

0

If you are currently using the actual password for the gmail account you are connecting to. The the best thing to do would be to try creating an apps password and using that.

Sign in with App Passwords

update

I just tested it and this appears to work fine.

using (var client = new SmtpClient())
    {
        client.Connect("smtp.gmail.com", 465, true);
        client.Authenticate("xxxx@gmail.com", "appspassword");
        client.Send(message.GetMessage());
        client.Disconnect(true);
    }

Another option would be xoauth2

Linda Lawton - DaImTo
  • 106,405
  • 32
  • 180
  • 449