2012-07-10 51 views
4

我在這裏發現了這個源代碼,並嘗試使用它發送郵件。 我成功地收到了'MFMailComposeResultSent'消息,但實際郵件未發送。 我不明白爲什麼不工作。 另外,我從NSData附加了一個圖像,但它不顯示在mailcomposeview中。 這段代碼有什麼問題? 實際上,我只需要用附帶圖片調用本地郵件應用程序,但我聽說沒有辦法通過附件調用本地應用程序。 請讓我知道我的代碼有什麼問題。 問題1:不從mailcomposeview發送郵件, 問題2:不顯示附加圖像。 最佳:使用附加圖像運行本機郵件應用程序。 我會很高興地等待你的答案。謝謝。爲什麼我的發送郵件不能從MFMailComposeViewController工作?

-(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; 
} 
} 
+0

你通過模擬器或設備上發送? – shabbirv 2012-07-10 01:21:56

+0

我通過設備測試過。 – user1471568 2012-07-10 01:53:24

+0

您的設備是否在網絡上?我想當你收到MFMailComposeResultSent時,它實際上意味着它已經成功地告訴郵件應用發送郵件,但如果你沒有網絡,它可能實際上排隊等待發送和等待。 – Stew 2012-07-10 04:11:22

回答

3
  1. 確保您在設置中配置的帳戶。
  2. 我沒有找到任何收件人?

你可以參考這個link

+0

當然,我有網絡連接。我通過鍵入在郵件視圖中編寫了收件人。不在代碼中。 – user1471568 2012-07-10 04:51:27

+1

呵呵,我在手機上發現郵件賬號沒有配置。 (實際上已配置,但iCloud帳戶有問題。)非常感謝。有沒有辦法檢查用戶配置的郵件帳戶? – user1471568 2012-07-10 05:01:16

+0

有一個+(BOOL)canSendMail; MFMailComposeViewController方法,它明確檢查是否配置了任何郵件帳戶。如果您的帳戶配置不正確,我認爲這是用戶問題,而不是代碼中可以檢測到的問題。 – Stew 2012-07-10 05:05:07

0

在迅速3,您可以使用此代碼清晰:

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

    } 
相關問題