2017-08-03 37 views
0

MailKitライブラリjstedfastを使用してxamarin.androidアプリからメールを送信できません。xamarin.androidアプリからMailkitを使用してメールを送信できません

私は次のコードを使用しています:私は私のアプリから、このコードを実行すると、それが例外をスロー

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=ja –

+0

ありがとうございました。アクセスを許可した後は助かりました。 –

関連する問題