Send email from Xamarin.Forms app using Dependency Service

我与影子孤独终老i 提交于 2019-12-12 03:53:15

问题


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 ? 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.


回答1:


Please take a look at this answer:

Sending e-mail from Gmail in Xamarin.Forms app

besides that you can also do it with this NuGet package:

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



来源:https://stackoverflow.com/questions/41063172/send-email-from-xamarin-forms-app-using-dependency-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!