2016-12-01 22 views
0

Gmailを使用してXamarin Formsアプリ内から電子メールを送信しようとしています。Xamarin.FormsアプリでGmailから電子メールを送信

私は1つのメソッドだけでインターフェイスを作成しました:SendEmail();

次に、Droidプロジェクトでは、上記のインターフェイスを実装するクラスを追加しました。依存関係の属性を使用すると、メインプロジェクトでメソッドの実装を取得し、すべてが次のエラーを除いて、細かいです:

Could not resolve host 'smtp.gmail.com' 

これは、メソッドの実際の実装である:

string subject = "subject here "; 
    string body= "body here "; 
    try 
    { 
     var mail = new MailMessage(); 
     var smtpServer = new SmtpClient("smtp.gmail.com", 587); 
     mail.From = new MailAddress("[email protected]"); 
     mail.To.Add("[email protected]"); 
     mail.Subject = subject; 
     mail.Body = body; 
     smtpServer.Credentials = new NetworkCredential("username", "pass"); 
     smtpServer.UseDefaultCredentials = false; 
     smtpServer.EnableSsl = true; 
     smtpServer.Send(mail); 
    } 
    catch (Exception ex) 
    { 
     System.Diagnostics.Debug.WriteLine(ex); 

    } 

周りの検索私は実際のsmtpアドレス以外のそれについての詳細を見つけることができませんでした。

また、私はGoogleより安全性の低いアプリケーションの手順を使用しましたが、資格情報のエラーを受け取っていないため、アカウントに正常に接続できると想定しています。

答えて

0

最終的にそれが自分のものです!

まず、XamarinのAndroid Playerを使用していましたが、これは明らかにネットワーク接続をサポートしていません。

Visual Studio Community 2015に組み込まれているAndroidエミュレータ(そのバージョンのもの)を使用し、James Montemagno(Xu.Plugin.Connectivity on NuGet)のプラグインを使用してネットワーク接続をテストしました。

2

こんにちは、私は私はこの方法を使用する依存関係サービスを使用して、電子メールにファイルを添付しているにも、以下のコードを使用してこれを実現している:

アンドロイド:

public static string ICSPath 
{ 
    get 
    { 
     var path = Path.Combine(Android.OS.Environment.ExternalStorageDirectory.AbsolutePath, StaticData.CalendarFolderName); 
     if (!Directory.Exists(path)) 
      Directory.CreateDirectory(path); 
     return Path.Combine(path, StaticData.CalendarFileName); 
    } 
} 

public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList) 
{ 
    Intent choserIntent = new Intent(Intent.ActionSend); 

    //Create the calendar file to attach to the email 
    var str = await GlobalMethods.CreateCalendarStringFile(segmentList); 

    if (File.Exists(ICSPath)) 
    { 
     File.Delete(ICSPath); 
    } 

    File.WriteAllText(ICSPath, str); 

    Java.IO.File filelocation = new Java.IO.File(ICSPath); 
    var path = Android.Net.Uri.FromFile(filelocation); 

    // set the type to 'email' 
    choserIntent.SetType("vnd.android.cursor.dir/email"); 
    //String to[] = { "[email protected]" }; 
    //emailIntent.putExtra(Intent.EXTRA_EMAIL, to); 
    // the attachment 
    choserIntent.PutExtra(Intent.ExtraStream, path); 
    // the mail subject 
    choserIntent.PutExtra(Intent.ExtraSubject, "Calendar event"); 
    Forms.Context.StartActivity(Intent.CreateChooser(choserIntent, "Send Email")); 

    return true; 

} 

のiOS:

public static string ICSPath 
{ 
    get 
    { 
     var path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Personal), StaticData.CalendarFolderName); 
     if (!Directory.Exists(path)) 
      Directory.CreateDirectory(path); 
     return Path.Combine(path, StaticData.CalendarFileName); 
    } 
} 


public async Task<bool> ShareCalendarEvent(List<ISegment> segmentList) 
{ 
    //Create the calendar file to attach to the email 
    var str = await GlobalMethods.CreateCalendarStringFile(segmentList); 

    if (File.Exists(ICSPath)) 
    { 
     File.Delete(ICSPath); 
    } 

    File.WriteAllText(ICSPath, str); 

    MFMailComposeViewController mail; 
    if (MFMailComposeViewController.CanSendMail) 
    { 
     mail = new MFMailComposeViewController(); 
     mail.SetSubject("Calendar Event"); 
     //mail.SetMessageBody("this is a test", false); 
     NSData t_dat = NSData.FromFile(ICSPath); 
     string t_fname = Path.GetFileName(ICSPath); 
     mail.AddAttachmentData(t_dat, @"text/v-calendar", t_fname); 

     mail.Finished += (object s, MFComposeResultEventArgs args) => 
     { 
      //Handle action once the email has been sent. 
      args.Controller.DismissViewController(true, null); 
     }; 

     Device.BeginInvokeOnMainThread(() => 
     { 
      UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mail, true, null); 
     }); 

    } 
    else 
    { 
     //Handle not being able to send email 
     await App.BasePageReference.DisplayAlert("Mail not supported", 
               StaticData.ServiceUnavailble, StaticData.OK); 
    } 

    return true; 
} 

こちらがお役に立てば幸いです。

+0

こんにちはマリオ、返信いただきありがとうございます!あなたのアプローチはうまくいくと思いますが、私はメールの送信者だけでなくメールボックスのログインも制御できる必要があります。安全性の低いGmailアカウントとYahooのアカウントを使って試してみましたが、上記のエラーメッセージが表示されます.... –

関連する問題