您将需要 MessageUI.framework 对您的项目的引用。
将以下内容添加到您的 .h 文件中
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
添加委托人<MFMailComposeViewControllerDelegate>
在 .m 文件中创建几个类似于以下内容的方法。
-(IBAction)checkCanSendMail:(id)sender{
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (mailClass != nil) {
if ([mailClass canSendMail]) {
[self displayComposerSheet];
}
else {
//Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
}
}
else {
//Display alert for not compatible. Need iPhone OS 3.0 or greater. Or implement alternative method of sending email.
}
}
-(void)displayComposerSheet {
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Email Subject"];
//Set our to address, cc and bcc
NSArray *toRecipients = [NSArray arrayWithObject:@"primary@domain.com"];
//NSArray *ccRecipients = [NSArray arrayWithObjects:@"first@domain.com",@"second@domain.com",nil];
//NSArray *bccRecipients = [NSArray arrayWithObjects:@"first@domain.com",@"second@domain.com",nil];
[mailer setToRecipients:toRecipients];
//[mailer setCcRecipients:ccRecipients];
//[mailer setBccRecipients:bccRecipients];
NSString *emailBody = @"\
<html><head>\
</head><body>\
This is some HTML text\
</body></html>";
[mailer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:mailer animated:YES];
[mailer release];
}
Apple 示例代码以及更多说明,请访问:http: //developer.apple.com/iphone/library/samplecode/MailComposer/
我知道这不使用 webView,但它确实允许您从应用程序中创建 HTML 电子邮件。