7

我从这里找到了这个源代码并尝试使用它发送邮件。我成功收到了“MFMailComposeResultSent”消息,但没有发送实际邮件。我不明白为什么不工作。另外,我附上了一张来自 NSData 的图片,但它没有显示在 mailcomposeview 中。这段代码有什么问题?实际上,我只需要使用附件图像调用本机邮件应用程序,但我听说无法使用附件调用本机应用程序。请让我知道我的代码有什么问题。问题 1:不从 mailcomposeview 发送邮件,问题 2:不显示附加图像。最佳:运行带有附加图像的本机邮件应用程序。我会很高兴地等待你的答复。谢谢。

-(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

3 回答 3

4
  1. 确保您在设置中配置了一个帐户。
  2. 我没有找到任何收件人??

您可以从此链接中获取参考。

于 2012-07-10T04:05:59.070 回答
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)

    }
于 2017-03-10T08:41:35.207 回答
0

斯威夫特 5 和 Xcode 12.5.1 ....

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

声明反馈邮件

let recipientEmail = "feedback@gmail.com"

点击事件

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

如果用户没有任何邮件配置,它将要求其他邮件选项...

于 2021-07-07T10:59:24.883 回答