c# - 以 xamarin 形式发送电子邮件

标签 c# xamarin

我正在尝试在我的 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

关于c# - 以 xamarin 形式发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43694998/

相关文章:

c# - 自托管 WCF 服务的跨域访问?

xamarin - Apple 开发者帐户团队未出现在 VS Mac 上

c# - 标签不显示文本 - Xamarin IOS

c# - 获取WAV文件的PCM值

c# - 更改 ASP.NET MVC 项目命名空间后出现编译错误?

c# - 手机进入休眠状态后,在前台服务中保持 wifi 处于 Activity 状态

c# - Xamarin 中的 SignalR 客户端

xamarin - 无法使用 Objective Sharpie 将 Objective-C 库绑定(bind)到 C#

c# - 表单启动时在文本框中编辑位置插入符号 C#

c# - 托管WebBrowser控件时如何获取Document Interface?