Why doesn't my mailing list work with MFMailComposeViewController?

I found this source code here and tried to send mail using it. I successfully received the message "MFMailComposeResultSent", but the actual mail was not sent. I do not understand why it does not work. In addition, I added an image from NSData, but it does not appear in mailcomposeview. What happened to this code? In fact, I only need to call my own mail application with an attached image, but I heard that there is no way to call my own application with an attachment. Please let me know what is wrong with my code. Problem 1: does not send mail from mailcomposeview, Problem 2: the attached image is not displayed. Best: Launch your own email application with an attached image. I will be happy to wait for your answers. thanks.

-(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; } } 
+4
source share
2 answers
  • Make sure you have an account configured in the settings.
  • I did not find any recipients.

You can use the link.

+3
source

In swift 3, you can use this clean 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) } 
0
source

All Articles