2017-08-03 87 views
0

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

我使用下面的代码:

try 
{ 
    //From Address 
    string FromAddress = "[email protected]"; 
    string FromAdressTitle = "Email Title"; 
    //To Address 
    string ToAddress = "[email protected]"; 
    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("[email protected]", "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

enter image description here

我在想什么这段代码。任何人都可以帮助我!

回答

1

使用MAILMESSAGE类。

using System.Net.Mail; 

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

        client.Send(mail); 
+0

我尝试这样做,则抛出异常:“{System.Net.Mail.SmtpException:534-5.7.14

+0

您需要允许安全性较低的应用访问您的Gmail帐户。 https://support.google.com/accounts/answer/6010255?hl=zh-CN –

+0

谢谢,它允许访问后帮助。 –

相关问题