要發送的iPhone應用程序,你需要做下面的任務列表中的郵件。
第1步:導入#import <MessageUI/MessageUI.h>
在您想要發送電子郵件的控制器類中。
第2步:委託添加到您的控制器像圖所示
@interface <yourControllerName> : UIViewController <MFMessageComposeViewControllerDelegate, MFMailComposeViewControllerDelegate>
3步:添加以下方法發送電子郵件。
- (void) sendEmail {
// Check if your app support the email.
if ([MFMailComposeViewController canSendMail]) {
// Create an object of mail composer.
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
// Add delegate to your self.
mailComposer.mailComposeDelegate = self;
// Add recipients to mail if you do not want to add default recipient then remove below line.
[mailComposer setToRecipients:@[<add here your recipient objects>]];
// Write email subject.
[mailComposer setSubject:@「<Your Subject Here>」];
// Set your email body and if body contains HTML then Pass 「YES」 in isHTML.
[mailComposer setMessageBody:@「<Your Message Body>」 isHTML:NO];
// Show your mail composer.
[self presentViewController:mailComposer animated:YES completion:NULL];
}
else {
// Here you can show toast to user about not support to sending email.
}
}
第4步:實施MFMailComposeViewController代表
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(nullable NSError *)error {
[controller dismissViewControllerAnimated:TRUE completion:nil];
switch (result) {
case MFMailComposeResultSaved: {
// Add code on save mail to draft.
break;
}
case MFMailComposeResultSent: {
// Add code on sent a mail.
break;
}
case MFMailComposeResultCancelled: {
// Add code on cancel a mail.
break;
}
case MFMailComposeResultFailed: {
// Add code on failed to send a mail.
break;
}
default:
break;
}
}
Mugunth的真棒帖子。去哥們的路! – Jordan 2009-08-20 23:46:00
它真的很棒。謝謝。 我設計了一個專門用於接受來自用戶的電子郵件和主題的視圖。通過執行相同的代碼,它再次顯示了相似的觀點。我可以從我的按鈕按下事件在視圖控制器類調用的委託方法 感謝您的幫助, 世斌 – smakstr 2010-04-29 12:07:54
我已經下載了相同的示例代碼,但它不發送任何郵件。它只提示郵件發送成功,但沒有收到郵件。我試過添加默認顯示爲紅色的MessageUI框架,但應用程序仍然沒有發送郵件。任何在這方面的幫助將不勝感激。我正在模擬器中測試該應用程序。 – 2010-12-10 14:07:23