2017-08-03 35 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

謝謝,它允許訪問後幫助。 –

相關問題