發(fā)送郵件

import <MessageUI/MFMailComposeViewController.h>

MFMailComposeViewControllerDelegate

  • (void)sendEmail {
    Class mainlClass=NSClassFromString(@"MFMailComposeViewController");
    if (!mainlClass) {
    [self showMessage:@"當前系統(tǒng)版本不支持應用內(nèi)發(fā)送郵件功能"];
    return;
    }

    if (![mainlClass canSendMail]) {
    [self showMessage:@"你還沒有設(shè)置郵件賬戶"];

      return;
    

    }

    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate=self;

    //設(shè)置主題
    [mailPicker setSubject:@"Redo.Me 意見反饋"];

    //添加收件人
    NSArray *toRecipients=[NSArray arrayWithObject:@"chjwrr@163.com"];
    [mailPicker setToRecipients:toRecipients];

//添加抄送
NSArray *ccRecipients=[NSArray arrayWithObject:@""];
[mailPicker setCcRecipients:ccRecipients];

//添加密送
NSArray *bccRecipients=[NSArray arrayWithObject:@""];
[mailPicker setBccRecipients:bccRecipients];

//添加一張圖片
UIImage *image=[UIImage imageNamed:@""];
NSData *imageData=UIImagePNGRepresentation(image);
[mailPicker addAttachmentData:imageData mimeType:@"png" fileName:@"icon.png"];

//添加一個附件
NSString *filePath=@"附件路徑";
NSData *fileData=[NSData dataWithContentsOfFile:filePath];
[mailPicker addAttachmentData:fileData mimeType:@"" fileName:@"附件名字+后綴名"];

NSString *emailBody=@"<font color='red'> 一張照片,一幅場景<br />一句情話,一段回憶<br />一篇文章,一個曾經(jīng)<br />既然時間無法Undo<br />那么就 Redo.Me </font>";
NSString *msg=[NSString stringWithFormat:@"<br />手機型號:%@<br />系統(tǒng)版本:%.2f<br />應用版本:%@",[NSString phoneName],kAPP_System_Version,kAPP_Bundle_Version];


[mailPicker setMessageBody:[NSString stringWithFormat:@"%@\n%@",emailBody,msg] isHTML:YES];

[self presentViewController:mailPicker animated:YES completion:^{
    
}];

}

pragma mark - MFMailComposeViewControllerDelegate

  • (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    [self dismissViewControllerAnimated:YES completion:nil];

    switch (result) {
    case MFMailComposeResultSent:{
    [self showMessage:@"郵件已發(fā)送"];

      }
          break;
      case MFMailComposeResultCancelled:{
          [self showMessage:@"郵件已取消"];
    
      }
          break;
      case MFMailComposeResultFailed:{
          [self showMessage:@"郵件發(fā)送失敗,請重試"];
    
      }
          break;
      case MFMailComposeResultSaved:{
          [self showMessage:@"郵件保存成功"];
    
      }
          break;
    
      default:
          break;
    

    }

}

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容