Как я могу исправить ошибку "Инициализатор типа для '?"бросил исключение ". при отправке электронной почты через C # с EASendMail? - PullRequest
0 голосов
/ 01 октября 2019

Я работал над этим проектом, чтобы отправить электронное письмо пользователю, и у меня постоянно возникала ошибка. (Я использую пакет EASendMail NuGet) Это "инициализатор типа для '?"бросил исключение ". (это в консоли для системы ошибок, которая есть в пакете) Я действительно не знаю, что это значит, мой код такой:

using EASendMail;

namespace Email
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                SmtpMail oMail = new SmtpMail("TryIt");

                // Set sender email address, please change it to yours
                oMail.From = "Splat2ooner@gmail.com";
                // Set recipient email address, please change it to yours
                oMail.To = "Splat2ooner@gmail.com";

                // Set email subject
                oMail.Subject = "test email from c# project";
                // Set email body
                oMail.TextBody = "this is a test email sent from c# project, do not reply";

                // SMTP server address
                SmtpServer oServer = new SmtpServer("smtp.gmail.com");

                // User and password for ESMTP authentication
                oServer.User = "Splat2ooner@gmail.com";
                oServer.Password = "password (not my passoword)";

                // Most mordern SMTP servers require SSL/TLS connection now.
                // ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
                oServer.ConnectType = SmtpConnectType.ConnectTryTLS;

                // If your SMTP server uses 587 port
                //oServer.Port = 587;

                // If your SMTP server requires SSL/TLS connection on 25/587/465 port
                //oServer.Port = 25; // 25 or 587 or 465
                oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;

                Console.WriteLine("start to send email ...");

                SmtpClient oSmtp = new SmtpClient();
                oSmtp.SendMail(oServer, oMail);

                Console.WriteLine("email was sent successfully!");
            }
            catch (Exception ep)
            {
                Console.WriteLine("failed to send email with the following error:");
                Console.WriteLine(ep.Message);
            }
        }
    }
}

Спасибо.

...