What caused the Socket Exception while sending email from Console application?

前端 未结 5 2051
走了就别回头了
走了就别回头了 2021-01-17 17:41

I\'m trying to write a basic console app that will send an email. The problem is that I keep getting the Socket exception:

An attempt was made to acc

5条回答
  •  走了就别回头了
    2021-01-17 18:17

    I changed to MailKit.Net.Smtp.SmtpClient. It gave better errors and I was able to get the emails to work.

    using MimeKit;
    using MailKit.Net.Smtp;
    using System.Net;
    
    using (var client = new SmtpClient())
        {
            client.Connect
                (
                    Context.Settings.SMTPServer,
                    Convert.ToInt32(Context.Settings.SMTPPort),
                    MailKit.Security.SecureSocketOptions.None
                );
    
            //ignore ssl errors
            client.ServerCertificateValidationCallback = (sender, certificate, chain, sslPolicyErrors) => true;
    
            // Note: only needed if the SMTP server requires authentication
            if (Context.Settings.SMTPAuthenticate)
            {
    
                client.Authenticate
                    (
                            new NetworkCredential
                            (
                                Context.Settings.SMTPUserName,
                                Context.Settings.SMTPPassword,
                                Context.Settings.SMTPDomain
                            )
                    );
            }
    
    
            await client.SendAsync(message);
            client.Disconnect(true);
    
            }
    

提交回复
热议问题