csb
csb

Reputation: 336

Xamarin iOS - How to send emails

I've tried following the tutorial: http://developer.xamarin.com/recipes/ios/shared_resources/email/send_an_email/

But when I run the code it never enters CanSendMail always hits the Else statement.

Code

public partial class ContactController : BaseController
{
    MFMailComposeViewController mailController;

    public ContactController()
        : base(null, null)
    {
    }

    public override void ViewDidLoad()
    {
        base.ViewDidLoad();
        View.BackgroundColor = UIColor.White;

        var title = new UILabel(new RectangleF(-110, 80, 320, 30));
        title.Font = UIFont.SystemFontOfSize(24.0f);
        title.TextAlignment = UITextAlignment.Center;
        title.TextColor = UIColor.Black;
        title.Text = "Contact";

        if (MFMailComposeViewController.CanSendMail)
        {

            mailController = new MFMailComposeViewController();

            // do mail operations here
            mailController.SetToRecipients(new string[] { "[email protected]" });
            mailController.SetSubject("mail test");
            mailController.SetMessageBody("this is a test", false);

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

            this.PresentViewController(mailController, true, null);
        }
        else { Console.WriteLine("Email can't be sent"); }

        var body = new UILabel(new RectangleF(50, 120, 220, 100));
        body.Font = UIFont.SystemFontOfSize(12.0f);
        body.TextAlignment = UITextAlignment.Center;
        body.Lines = 0;
        body.Text = @"This is the content view controller.";

        View.Add(title);
        View.Add(body);
    }
}

Can somebody help me with this please.

Thanks

Upvotes: 1

Views: 4180

Answers (1)

csb
csb

Reputation: 336

I just needed to enable an Email account within the devices settings.

Upvotes: 1

Related Questions