[英]sending email in xamarin forms
我正在嘗試在我的xamarin表單項目中發送一封電子郵件,我已經在iPhone模擬器和iPhone設備上試過了。 當我按下iPhone上的發送電子郵件按鈕時,沒有任何反應,甚至沒有調試錯誤。 我還確保我使用設備上的電子郵件登錄。
我使用了serviceDependency並按照以下鏈接進行了設置: https : //developer.xamarin.com/recipes/ios/shared_resources/email/send_an_email/
我的界面:
public interface InterfaceEmail
{
void sendEmail();
}
iOS實現:
[assembly: Xamarin.Forms.Dependency(typeof(SendEmail))]
namespace myProject.iOS
{
public partial class SendEmail : InterfaceEmail
{
MFMailComposeViewController mailController;
public SendEmail() {}
public void sendEmail()
{
if (MFMailComposeViewController.CanSendMail)
{
mailController = new MFMailComposeViewController();
mailController.SetToRecipients (new string[] {"my@email.com"});
mailController.SetSubject ("test mail");
mailController.SetMessageBody ("This is a test", false);
mailController.Finished += (object sender, MFComposeResultEventArgs e) =>
{
Console.WriteLine(e.Result.ToString());
e.Controller.DismissViewController(true, null);
};
UIApplication.SharedApplication.KeyWindow.RootViewController.PresentViewController(mailController, true, null);
}}}}
在我的共享代碼中實現:
async void Handle_ToolbarButton(object sender, System.EventArgs e)
{
var action = await DisplayActionSheet("What do you want to do?", "Abort", null, "Send email");
if(action == "Send email")
{
DependencyService.Get<InterfaceEmail>().sendEmail();
}
}
有沒有人知道這里可能出現什么問題?
為了更好地發送電子郵件而不編寫特定於平台的代碼,請將此nuget安裝到您的解決方案xam.plugin.Messaging( https://www.nuget.org/packages/Xam.Plugins.Messaging/ )
然后在PCL中編寫下面的代碼
var email = new EmailMessageBuilder()
.To("to.plugins@xamarin.com")
.Subject("Xamarin Messaging Plugin")
.Body("Well hello there from Xam.Messaging.Plugin")
.Build();
您還可以添加附件。 有關詳細信息,請訪問https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md
iPhone模擬器將始終向CanSendMail
返回false,因為它無法發送郵件。 在物理設備上,您至少需要配置電子郵件帳戶。
也:
錯字:
[assembly: Xamarin.Forms.Dependency(typeof(sendEmail))]
應該:
[assembly: Xamarin.Forms.Dependency(typeof(SendEmail))]
錯字:
mailController.Finnished += ~~~~~
應該:
mailController.Finished += ~~~~~
可能與此錯誤有關: https : //bugzilla.xamarin.com/show_bug.cgi?id = 58933
只需刪除DisplayActionSheet。
或者如果你想使用它,那么在這個Xamarin論壇主題中有一個臨時解決方案
加
await Task.Delay(100);
在DisplayActionSheet之后
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.