要发送的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