为什么我的发送邮件无法从MFMailComposeViewController工作?

时间:2012-07-10 01:11:53

标签: iphone ios xcode mfmailcomposeviewcontroller

我从这里找到了这个源代码并尝试使用它发送邮件。 我成功获得了'MFMailComposeResultSent'消息,但未发送实际邮件。 我不明白为什么不工作。 另外,我附加了NSData中的图像,但它没有显示在mailcomposeview中。 这段代码有什么问题? 实际上,我只需要使用附件图像调用本机邮件应用程序,但我听说无法通过附件调用本机应用程序。 请告诉我我的代码有什么问题。 问题1:不从mailcomposeview发送邮件, 问题2:不显示附加图像。 最佳:运行附加图像的本机邮件应用程序。 我很乐意等你的回答。 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;
}
}

3 个答案:

答案 0 :(得分:3)

  1. 确保您在设置中配置了帐户。
  2. 我没找到任何收件人??
  3. 您可以参考此link

答案 1 :(得分:0)

在swift 3中,你可以使用这个清晰的代码:

 @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)

    }

答案 2 :(得分:0)

Swift 5 和 Xcode 12.5.1 ....

打开配置邮件(如果存在)或转到其他选项...

声明反馈邮件

char

点击事件

let recipientEmail = "feedback@gmail.com"

如果用户没有任何邮件配置,它会询问其他邮件选项...