iOS 上的系统邮件发送相当简单,瞬间让你搞懂邮件发送。
APP站内发生邮件
//调出邮件发送窗口
- (void) displayMFMailComposeVC
{
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
mailCompose.mailComposeDelegate = self;
//设置主题
[mailCompose setSubject: @"eMail主题"];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObjects:@"收件人邮件1", @"收件人邮件2", nil];
[mailCompose setToRecipients: toRecipients];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"抄送人邮件1", @"抄送人邮件2", nil];
[mailCompose setCcRecipients:ccRecipients];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"密送人邮件1", nil];
[mailCompose setBccRecipients:bccRecipients];
//添加一张图片
UIImage *addPic = [UIImage imageNamed: @"图片"];
NSData *imageData = UIImagePNGRepresentation(addPic);// png
//关于mimeType:http://www.iana.org/assignments/media-types/
[mailCompose addAttachmentData:imageData mimeType:@"image" fileName:@"Icon.png"];
// application
// audio
// font
// example
// image
// message
// model
// multipart
// text
// video
//添加一个附件
NSData *pdf = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"附加链接.pdf"]];
//关于mimeType:http://www.iana.org/assignments/media-types/
[mailCompose addAttachmentData:pdf mimeType:@"application" fileName:@"附加名称.pdf"];
//富文本为 isHTML:YES 字符串isHTML:NO
NSString *emailBody = @"eMail 我的邮件";
[mailCompose setMessageBody:emailBody isHTML:YES];
[self presentViewController:mailCompose animated:NO completion:^{
}];
}
#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//关闭邮件发送窗口
[self dismissViewControllerAnimated:YES completion:nil];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
//用户取消编辑邮件
msg = @"取消发送邮件";
break;
case MFMailComposeResultSaved:
//用户成功保存邮件
msg = @"保存邮件成功";
break;
case MFMailComposeResultSent:
//用户点击发送,将邮件放到队列中,还没发送
msg = @"邮件发送中";
break;
case MFMailComposeResultFailed:
//用户试图保存或者发送邮件失败
msg = @"保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
if (msg.length) {
//[self toastWith:msg];
}
}
跳转到系统邮件页面发生邮件
- (void)sendEmil {
NSMutableString *mailUrl = [[NSMutableString alloc]init];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObjects:@"收件人邮件1", @"收件人邮件2", nil];
[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"抄送人邮件1", @"抄送人邮件2", nil];
[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"密送人邮件1", nil];
[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
//添加主题
[mailUrl appendString:@"&subject=这是我的邮件主题"];
//添加邮件内容
[mailUrl appendString:@"&body=我的邮件主题,富文本格式 body!"];
NSString* email = [mailUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
}
参考代码
- (void)sendEmil {
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if (!mailClass) {//不支持APP内发生邮件
NSMutableString *mailUrl = [[NSMutableString alloc]init];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObjects:@"收件人邮件1", @"收件人邮件2", nil];
[mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"抄送人邮件1", @"抄送人邮件2", nil];
[mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"密送人邮件1", nil];
[mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];
//添加主题
[mailUrl appendString:@"&subject=这是我的邮件主题"];
//添加邮件内容
[mailUrl appendString:@"&body=我的邮件主题,富文本格式 body!"];
NSString* email = [mailUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
[[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];
return;
}
if (![mailClass canSendMail]) {
//[self toastWith:@"用户没有设置邮件账户"];
return;
}
[self displayMFMailComposeVC];
}
//调出邮件发送窗口
- (void) displayMFMailComposeVC
{
MFMailComposeViewController *mailCompose = [[MFMailComposeViewController alloc] init];
mailCompose.mailComposeDelegate = self;
//设置主题
[mailCompose setSubject: @"eMail主题"];
//添加收件人
NSArray *toRecipients = [NSArray arrayWithObjects:@"收件人邮件1", @"收件人邮件2", nil];
[mailCompose setToRecipients: toRecipients];
//添加抄送
NSArray *ccRecipients = [NSArray arrayWithObjects:@"抄送人邮件1", @"抄送人邮件2", nil];
[mailCompose setCcRecipients:ccRecipients];
//添加密送
NSArray *bccRecipients = [NSArray arrayWithObjects:@"密送人邮件1", nil];
[mailCompose setBccRecipients:bccRecipients];
//添加一张图片
UIImage *addPic = [UIImage imageNamed: @"图片"];
NSData *imageData = UIImagePNGRepresentation(addPic);// png
//关于mimeType:http://www.iana.org/assignments/media-types/
[mailCompose addAttachmentData:imageData mimeType:@"image" fileName:@"Icon.png"];
// application
// audio
// font
// example
// image
// message
// model
// multipart
// text
// video
//添加一个附件
NSData *pdf = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"附加链接.pdf"]];
//关于mimeType:http://www.iana.org/assignments/media-types/
[mailCompose addAttachmentData:pdf mimeType:@"application" fileName:@"附加名称.pdf"];
//富文本为 isHTML:YES 字符串isHTML:NO
NSString *emailBody = @"eMail 我的邮件";
[mailCompose setMessageBody:emailBody isHTML:YES];
[self presentViewController:mailCompose animated:NO completion:^{
}];
}
#pragma mark - 实现 MFMailComposeViewControllerDelegate
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
//关闭邮件发送窗口
[self dismissViewControllerAnimated:YES completion:nil];
NSString *msg;
switch (result) {
case MFMailComposeResultCancelled:
//用户取消编辑邮件
msg = @"取消发送邮件";
break;
case MFMailComposeResultSaved:
//用户成功保存邮件
msg = @"保存邮件成功";
break;
case MFMailComposeResultSent:
//用户点击发送,将邮件放到队列中,还没发送
msg = @"邮件发送中";
break;
case MFMailComposeResultFailed:
//用户试图保存或者发送邮件失败
msg = @"保存或者发送邮件失败";
break;
default:
msg = @"";
break;
}
if (msg.length) {
//[self toastWith:msg];
}
}