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

♀尐吖头ヾ 提交于 2019-12-11 08:19:36

问题


I am not able to send email from a xamarin.android app using MailKit library of jstedfast.

I am using the following code :

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;
}

When I run this code from my app, it throws an exception:

MailKit.Security.AuthenticationException

What I am missing in this code. Can anybody help me out !


回答1:


Use 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);


来源:https://stackoverflow.com/questions/45489618/unable-to-send-email-using-mailkit-from-a-xamarin-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!