why is my sending mail not working from MFMailComposeViewController?
Asked Answered
A

3

7

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;
}
}
Anarchism answered 10/7, 2012 at 1:11 Comment(3)
are you sending via simulator or on your device?Whitening
Is your device on the network? I think when you get MFMailComposeResultSent, it actually means it has successfully told the mail app to send the email, but it might actually be queued to be sent and waiting if you don't have network.Pacifically
@shabzco. Is it possible to send using simulator?Salify
D
4
  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.

Dias answered 10/7, 2012 at 4:5 Comment(4)
Ofcourse, I have network connection. I wrote recipient in the mail view with typing. Not in code.Anarchism
Oh shit, I found on my phone, mail account was not configured. (actually is configured, but iCloud account had a problem.) Thank you very much. Is there any way to check a user configured mail account?Anarchism
There is a + (BOOL)canSendMail; method on MFMailComposeViewController which definitely checks to see if any mail accounts are configured. If you have incorrectly configured accounts, I think that's a user problem, not something you could detect in code.Pacifically
just open up the iphone mail app, it will send the pending mails in outbox from MFMailComposeViewControllerSuperabound
F
0

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(["[email protected]"])
            mail.setMessageBody("<p>This is test Mail!</p>", isHTML: true)

            present(mail, animated: true)
        } else {
             let email = "[email protected]"
             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)

    }
Flute answered 10/3, 2017 at 8:41 Comment(0)
F
0

Swift 5 and Xcode 12.5.1 ....

Open config mail if exist or go to other options ...

Declare the feedback mail

let recipientEmail = "[email protected]"

On Click Event

      @IBAction func openEmail(_ sender: Any) {
        
        if MFMailComposeViewController.canSendMail() {
            self.sendEmail(title: "Subject")
        } else {
            if let emailUrl = createEmailUrl(to: recipientEmail,
                                             subject: "Subject", body: "Send from my iPhone") {
                UIApplication.shared.open(emailUrl)
            }
        }
    }


func sendEmail(title : String){
    let EmialVC = configuredMailForWriteWithUs(title: title)
    self.present(EmialVC, animated: true, completion: nil)
}

       private func createEmailUrl(to: String, subject: String, body: String) -> URL? {
           let subjectEncoded = subject.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
           let bodyEncoded = body.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!

           let gmailUrl = URL(string: "googlegmail://co?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
           let outlookUrl = URL(string: "ms-outlook://compose?to=\(to)&subject=\(subjectEncoded)")
           let yahooMail = URL(string: "ymail://mail/compose?to=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
           let sparkUrl = URL(string: "readdle-spark://compose?recipient=\(to)&subject=\(subjectEncoded)&body=\(bodyEncoded)")
           let defaultUrl = URL(string: "mailto:\(to)?subject=\(subjectEncoded)&body=\(bodyEncoded)")

           if let gmailUrl = gmailUrl, UIApplication.shared.canOpenURL(gmailUrl) {
               return gmailUrl
           } else if let outlookUrl = outlookUrl, UIApplication.shared.canOpenURL(outlookUrl) {
               return outlookUrl
           } else if let yahooMail = yahooMail, UIApplication.shared.canOpenURL(yahooMail) {
               return yahooMail
           } else if let sparkUrl = sparkUrl, UIApplication.shared.canOpenURL(sparkUrl) {
               return sparkUrl
           }

           return defaultUrl
       }

If the user doesn't have any mail config it's gonna ask for others mail options...

Freezing answered 7/7, 2021 at 10:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.