2016-12-01 103 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地址。

此外,我使用了谷歌的安全性較低的應用程序,沒有收到證書錯誤,我認爲它可以連接到帳戶就好了。

回答

0

終於想出它自己!

首先,我使用Xamarin的Android Player,它顯然不支持網絡連接。

所以我的修復非常簡單:在Visual Studio Community 2015中使用了Android Emulator(它的任何版本),並使用James Montemagno的插件(NuGet上的Xam.Plugin.Connectivity)測試了網絡連接。

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帳戶,也是一個雅虎之一,仍然得到上面發佈相同的錯誤.... –

相關問題