2

I'm trying to send an email in my xamarin forms project, I have tried both in the iPhone simulator and on an iPhone device. When I push the send email button on the iPhone, nothing happens, not even a debug error. I have also made sure i am logged in with my email on the device.

I have used serviceDependency and followed the setup at this link: https://developer.xamarin.com/recipes/ios/shared_resources/email/send_an_email/

my interface:

public interface InterfaceEmail
{
    void sendEmail();
}

iOS implementation:

[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);
    }}}}

Implementation in my shared code:

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();
    }
}

Does anyone have an idea on what could be wrong here?

ebk
  • 107
  • 1
  • 3
  • 13

3 Answers3

7

For a better way to send email without even writing platform specific code install this nuget into your solution xam.plugin.Messaging(https://www.nuget.org/packages/Xam.Plugins.Messaging/)

Then write the code below in PCL

var email = new EmailMessageBuilder()
  .To("to.plugins@xamarin.com")
  .Subject("Xamarin Messaging Plugin")
  .Body("Well hello there from Xam.Messaging.Plugin")
  .Build();

You can also add attachments. For more details please go through https://github.com/cjlotz/Xamarin.Plugins/blob/master/Messaging/Details.md

Sayo Babalola
  • 990
  • 14
  • 25
Renjith
  • 682
  • 4
  • 19
1

The iPhone simulator will always return false to CanSendMail as it can not send mail. On a physical device, you will need to configure at least on e mail account.

Also:

Typo in:

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

Should be:

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

Typo in:

mailController.Finnished += ~~~~~

Should be:

mailController.Finished += ~~~~~
SushiHangover
  • 73,120
  • 10
  • 106
  • 165
  • The typo's you pointed out is just in this post and should not be a problem in my code. When I run in simulator the value of CanSendMail is set to true, but I have also tried with a physical device to be sure, but nothing happens on the iPhone when i push the send mail button. The debugger will go through each step of the code, but with no result. – ebk Apr 29 '17 at 14:54
  • `CanSendMail` should be false in the sim., are you running a older **beta** version of iOS? as this was broken in the iOS framework. Running an old version of `Xamarin.iOS`? – SushiHangover Apr 29 '17 at 18:38
  • I am running the newest version of xamarin.iOS, and the reason that CanSendMail is true is because i am logged in with my email on the simulator. Is there anything else that could cause my problem? – ebk Apr 30 '17 at 11:21
1

Probably it is related to this bug: https://bugzilla.xamarin.com/show_bug.cgi?id=58933

Just remove DisplayActionSheet.

Or if you want to use it, then there is a temporary solution in this Xamarin forum topic
Add

await Task.Delay(100);

after DisplayActionSheet

Alexej Sommer
  • 2,677
  • 1
  • 14
  • 25