【问题标题】:sending email in xamarin forms以 xamarin 形式发送电子邮件
【发布时间】:2019-04-11 04:52:58
【问题描述】:

我正在尝试在我的 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();
    }
}

有人知道这里可能出了什么问题吗?

【问题讨论】:

标签: c# xamarin


【解决方案1】:

为了更好地发送电子邮件,甚至无需编写特定于平台的代码,请将此 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

【讨论】:

  • 超级方便。感谢分享。
【解决方案2】:

可能与此错误有关: https://bugzilla.xamarin.com/show_bug.cgi?id=58933

只需删除 DisplayActionSheet。

或者如果你想使用它,那么这个Xamarin论坛有一个临时解决方案topic
添加

await Task.Delay(100);

DisplayActionSheet 之后

【讨论】:

    【解决方案3】:

    iPhone 模拟器将始终返回 false 到 CanSendMail,因为它无法发送邮件。在物理设备上,您至少需要配置一个电子邮件帐户。

    还有:

    输入错误:

    [assembly: Xamarin.Forms.Dependency(typeof(sendEmail))]
    

    应该是:

    [assembly: Xamarin.Forms.Dependency(typeof(SendEmail))]
    

    输入错误:

    mailController.Finnished += ~~~~~
    

    应该是:

    mailController.Finished += ~~~~~
    

    【讨论】:

    • 您指出的错字只是在这篇文章中,不应该是我的代码中的问题。当我在模拟器中运行时,CanSendMail 的值设置为 true,但我也尝试过使用物理设备来确定,但是当我按下发送邮件按钮时,iPhone 上没有任何反应。调试器会遍历代码的每一步,但没有结果。
    • CanSendMail 在 sim 卡中应该为 false。您运行的是较旧的 beta 版本的 iOS 吗?因为这在 iOS 框架中被破坏了。运行旧版本的Xamarin.iOS?
    • 我正在运行最新版本的 xamarin.iOS,CanSendMail 为 true 的原因是我在模拟器上使用电子邮件登录。还有什么可能导致我的问题吗?
    猜你喜欢
    • 1970-01-01
    • 2013-04-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2021-04-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多