无法从 xamarin.android 应用程序使用 Mailkit 发送电子邮件

Unable to send email using Mailkit from a xamarin.android app

我无法使用 jstedfastMailKit 库从 xamarin.android 应用程序发送电子邮件。

我正在使用以下代码:

try
{
    //From Address
    string FromAddress = "from_sender@gmail.com";
    string FromAdressTitle = "Email Title";
    //To Address
    string ToAddress = "to_receiver@gmail.com";
    string ToAdressTitle = "Address Title";
    string Subject = "Subject of mail";
    string BodyContent = "Body of email";

    //Smtp Server
    string SmtpServer = "smtp.gmail.com";
    //Smtp Port Number
    int SmtpPortNumber = 587;

    var mimeMessage = new MimeMessage();
    mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
    mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
    mimeMessage.Subject = Subject;
    mimeMessage.Body = new TextPart("plain")
    {
        Text = BodyContent

    };

    using (var client = new SmtpClient())
    {

        client.Connect(SmtpServer, SmtpPortNumber, false);
        // Note: only needed if the SMTP server requires authentication
        // Error 5.5.1 Authentication 
        client.AuthenticationMechanisms.Remove("XOAUTH2");
        client.Authenticate("from_sender@gmail.com", "password");
        client.Send(mimeMessage);
        Console.WriteLine("The mail has been sent successfully !!");
        Console.ReadLine();
        client.Disconnect(true);

    }

}
catch (Exception ex) 
{
    string message = ex.Message;
}

当我从我的应用程序 运行 这段代码时,它抛出一个异常:

MailKit.Security.AuthenticationException

我在这段代码中遗漏了什么。谁能帮帮我!

使用 MAILMESSAGE class。

using System.Net.Mail;

MailMessage mail = new MailMessage("example@gmail.com", "example@gmail.com", "Title","Body");
                    SmtpClient client = new SmtpClient();
                    client.Host = ("smtp.gmail.com");
                    client.Port = 587; //smtp port for SSL
                    client.Credentials = new System.Net.NetworkCredential("example@gmail.com", "password");
                    client.EnableSsl = true; //for gmail SSL must be true

                    client.Send(mail);