简体   繁体   中英

why is my sending mail not working from MFMailComposeViewController?

I found this source code from here and tried sending mail using it. I successfully got the 'MFMailComposeResultSent' Message, but actual mail is not sent. I don't understand why is not working. Also, I attached an image from NSData, but it doesn't display in mailcomposeview. What is the wrong with this code? Actually, I only need calling native mail app with an attachement image, but I heard there is no way calling the native app with attachement. Please let me know what is wrong with my code. Problem 1: doesn't send a mail from mailcomposeview, Problem 2: doesn't display attached image. Best : running native mail app with attached image. I'll be happily waiting your answers. Thx.

-(void) sendMail
{
NSLog(@"Mail");
MFMailComposeViewController *mfMailView = [[MFMailComposeViewController alloc]init];
NSData *imgData = UIImagePNGRepresentation(imgPreview.image);
mfMailView.mailComposeDelegate = self;

[mfMailView addAttachmentData:imgData mimeType:@"image/png" fileName:@"Me.png"];

//also tried this
//[mfMailView addAttachmentData:imgData mimeType:@"image/png" fileName:@"Me"];

[mfMailView setSubject:@"TE~st"];

[mfMailView setMessageBody:@"Download Me~!" isHTML:YES];
[self presentModalViewController:mfMailView animated:YES];
}

-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result) {
    case MFMailComposeResultCancelled:
        NSLog(@"cancel?");
        break;
    case MFMailComposeResultSaved:
        NSLog(@"saved?");
        break;
    case MFMailComposeResultSent:
        NSLog(@"Sent succed");
        [controller dismissModalViewControllerAnimated:YES];
        break;
    case MFMailComposeResultFailed:
        NSLog(@"sent failue");
        NSLog(@"%@",error);
        break;
    default:
        break;
}
}
  1. Make sure that you have an account configured in the settings.
  2. I didn't find any recipient??

You can take a reference from this link .

In swift 3, you can use this clear code:

 @IBAction func sendMail(_ sender: Any) {

        print(MFMailComposeViewController.canSendMail())
        if MFMailComposeViewController.canSendMail() {
            let mail = MFMailComposeViewController()
            mail.mailComposeDelegate = self
            mail.setToRecipients(["test@test.com"])
            mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true)

            present(mail, animated: true)
        } else {
             let email = "test@test.com"
             if let url = URL(string: "mailto:\(email)") {
             UIApplication.shared.open(url)
             }

        }


    }

    func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
        controller.dismiss(animated: true)
        switch result {
        case .cancelled:
            print("Mail cancelled")
        case .saved:
            print("Mail saved")
        case .sent:
            self.allertInfo(_title: "Mail Info", _message: "Mail is sent successfuly", _actionTitle: "OK")
            print("Mail sent")
        case .failed:
            self.allertInfo(_title: "Mail Info", _message: "Mail isn't sent.",
_actionTitle: "OK")
            print("Mail sent failure: \(error?.localizedDescription)")
        default:
            break
        }

    }

    func allertInfo(_title:String, _message:String, _actionTitle:String) {

        let alert = UIAlertController(title: _title, message: _message, preferredStyle: UIAlertControllerStyle.alert)
        alert.addAction(UIAlertAction(title: _actionTitle, style: UIAlertActionStyle.default, handler: nil))
        self.present(alert, animated: true, completion: nil)

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM