When will [MFMailComposeViewController canSendMail] return NO

IphoneObjective C

Iphone Problem Overview


My iPhone app is using the MFMailComposeViewController class to send an in-app email with an attachment. The app will only attempt to display the mail composer dialog if the "canSendMail" method of class MFMailComposeViewController returns true (YES). Specifically, if the following method returns YES, it shows the mail composer, otherwise the user is presented with an error alert dialog stating that there are no email accounts set up on the device:

- (BOOL)canDeviceSendEmail
{
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
    return mailClass != nil && [mailClass canSendMail];
}

A team of testers have reported that they get this error alert dialog, even when email accounts are set up on the device. The tester used an iPhone 3G with OS 3.1.3. Therefore the MFMailComposeViewController class must have existed, and the "canSendMail" method must have returned NO.

My question is therefore: apart from the case when there are no email accounts set up on the device, in what other circumstances can the "canSendMail" method return NO?

~ Thanks

Iphone Solutions


Solution 1 - Iphone

If at least one email account is enabled on the device, the following call should return YES:

[MFMailComposeViewController canSendMail]

Conversely, if all accounts are disabled/removed, it will return NO.

Solution 2 - Iphone

For +canSendMail to return YES the default account must be set-up for sending emails.

(Internally, +[MFMailComposeViewController canSendMail] calls +[MailAccountProxy defaultMailAccountForDelivery], which finds the first mail account being -isDefaultDeliveryAccount.)

Solution 3 - Iphone

In addition to an email account not being setup on a device, canSendMail also returns NO when an MDM profile is installed and configured to not allow third party apps to send mail. In this case you must use openURL: with a mailto: URL in order to launch Mail.app and optionally fill in the to, cc, bcc, subject, and body field.

mailto: RFC

Solution 4 - Iphone

This worked for me.

In Device Go setting->Mail,Contacts,Calendar->Accounts

Here you can see no account is added.Now add account now go back to your app and you can find its returning yes this time and you are able to send E-mail. Thanks

Solution 5 - Iphone

[MFMailComposeViewController canSendMail] will return NO when you don't have any mail account on device. In this case you can open mail app by the following code:

- (void)openMailApp {
    NSString *recipients = @"mailto:?cc=&subject=";
    NSString *body = @"&body=";
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}

Solution 6 - Iphone

In case of iOS 10.1, you can enable mail from iCloud first (in Settings). Then sign in to your iCloud account. All the contacts and email should be available in your simulator after those changes.

With the change, I can get true from:

MFMailComposeViewController.canSendMail()

I am using Xcode 8.1 with Swift 3.

Solution 7 - Iphone

Vladimir's answer without deprecated functions:

    NSString *recipients = @"mailto:[email protected]?subject=emailsubject";
    
    NSString *body = @"&body=body:";
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLFragmentAllowedCharacterSet]];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email] options:@{} completionHandler:^(BOOL success) {
    }];

Solution 8 - Iphone

You can't be certain that MFMailComposeViewController must have existed because your method doesn't discriminate between MFMailComposeViewController not existing and [MFMailComposeViewController canSendMail] returning NO.

What iPhone OS you're testing on is not relevant; what version of the iPhone SDK your application links against is what determines whether MFMailComposeViewController will be available at runtime. If you want that class, you need to build with SDK 3.0 or later.

Solution 9 - Iphone

Did you include the Messaging Framework? I had a similar issue once and it was only because I forgot to add the correct framework to the XCode project.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionpythonquickView Question on Stackoverflow
Solution 1 - IphonebyneriView Answer on Stackoverflow
Solution 2 - IphonekennytmView Answer on Stackoverflow
Solution 3 - IphoneSteve MoserView Answer on Stackoverflow
Solution 4 - IphoneTunvir Rahman TusherView Answer on Stackoverflow
Solution 5 - IphoneVladimirView Answer on Stackoverflow
Solution 6 - IphoneDavid.Chu.caView Answer on Stackoverflow
Solution 7 - IphoneTheJeffView Answer on Stackoverflow
Solution 8 - IphoneTomView Answer on Stackoverflow
Solution 9 - IphoneAlfonsoView Answer on Stackoverflow