简体   繁体   English

使用依赖关系服务从Xamarin.Forms应用发送电子邮件

[英]Send email from Xamarin.Forms app using Dependency Service

In my app, for debugging purposes and testing purposes I need to send an email. 在我的应用程序中,出于调试目的和测试目的,我需要发送电子邮件。

How do I present the mail controller, when the class that sends it does not contain a definition for PresentViewController ? 当发送邮件的类未包含PresentViewController的定义时,如何显示邮件控制器? The class needs to fire off the email app after the user clicks "yes" from the alert which fires. 用户在触发的警报中单击“是”后,该类需要触发电子邮件应用程序。

    public async Task<bool> SendEmail(Exception ex)
    {
        var result = await SendNotificationToRequestSendingEmail();

        if (result)
        {

            if (MFMailComposeViewController.CanSendMail)
            {
                mailController = new MFMailComposeViewController();

                mailController.SetToRecipients(new string[] { "test@one.com", "test@two.com" });
                mailController.SetSubject("Details");
                mailController.SetMessageBody($"Message: {ex.Message}" +
                                              $"Exception: {ex.ToString()}"
                                              , false);

                mailController.Finished += (object s, MFComposeResultEventArgs args) =>
                {
                    Console.WriteLine(args.Result.ToString());
                    args.Controller.DismissViewController(true, null);
                };

                this.PresentViewController(mailController, true, null); //this line causes the error
            }
        }

        return true;
    }

How can I fix this problem or get around it? 我该如何解决或解决这个问题? This is called from a Xamarin Forms page. 这是从Xamarin表单页面调用的。

Please take a look at this answer: 请看一下这个答案:

Sending e-mail from Gmail in Xamarin.Forms app 在Xamarin.Forms应用中从Gmail发送电子邮件

besides that you can also do it with this NuGet package: 除此之外,您还可以使用此NuGet包来做到这一点:

https://www.nuget.org/packages/Xam.Plugins.Messaging/ https://www.nuget.org/packages/Xam.Plugins.Messaging/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果调用方未使用 Xamarin.Forms,Xamarin 依赖项服务是否可以工作? - Can the Xamarin Dependency service work if the caller is not using Xamarin.Forms? Xamarin.Forms依赖服务UWP:methodNotFoundException - Xamarin.Forms Dependency Service UWP: methodNotFoundException Xamarin.Forms:Android<entry> 和依赖服务?</entry> - Xamarin.Forms: Android <Entry> and dependency service? 使用async / await返回Xamarin.Forms依赖项服务回调的结果? - Using async/await to return the result of a Xamarin.Forms dependency service callback? 如何使用媒体插件在 Xamarin.Forms 中实现相机叠加的依赖服务? - How to Implement Dependency Service for Camera Overlay in Xamarin.Forms Using the Media Plugin? 使用没有依赖服务的 xamarin.forms 将图像大小压缩到 250kb - Compress size of image to 250kb using xamarin.forms without dependency service C#-Xamarin.Forms-依赖服务和事件 - C# - Xamarin.Forms - Dependency Service and event Xamarin.Forms依赖关系服务非静态字段/属性 - Xamarin.Forms Dependency Service non-static fields/properties 触觉反馈在 Xamarin.Forms iOS 依赖服务中崩溃 - Haptic Feedback crashes in Xamarin.Forms iOS Dependency Service 我如何通过 email ( xamarin.forms ) 从 ListView 发送项目 - How can i send the items from a ListView by email ( xamarin.forms )
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM