iOS 發(fā)送郵件

iOS 應(yīng)用內(nèi)調(diào)用并發(fā)送郵件

  • 目前有兩種方法 mailto: 和 MFMailComposeViewController

1. 使用 mailto:

  • 會(huì)跳轉(zhuǎn)到應(yīng)用外并打開系統(tǒng)郵件
NSURL *url = [NSURL URLWithString:@"mailto:yourEmail"];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
    [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:nil];
} else {
    NSLog(@"打開郵箱出現(xiàn)錯(cuò)誤");
}

2. MFMailComposeViewController

  • 該方式需要已設(shè)置好郵件賬戶,否則無法打開
  • 可設(shè)置主題、收件人、抄送人、發(fā)送內(nèi)容
  • 首先需要導(dǎo)入 #import <MessageUI/MFMailComposeViewController.h>
#pragma mark - 發(fā)送郵件
- (void)sendEmail {
    
    MFMailComposeViewController *mailVC = [MFMailComposeViewController new];
    if (!mailVC) {
        // 在設(shè)備還沒有添加郵件賬戶的時(shí)候,為空
        NSLog(@"暫未設(shè)置郵箱賬戶,請(qǐng)先到系統(tǒng)設(shè)置添加賬戶");
        return;
    }
    
    //代理 MFMailComposeViewControllerDelegate
    mailVC.mailComposeDelegate = self;
    //郵件主題
    [mailVC setSubject:@"反饋/建議"];
    //收件人
    [mailVC setToRecipients:@[@"yourEmail"]];
    
    [self presentViewController:mailVC animated:YES completion:nil];
}

// 實(shí)現(xiàn)代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    // MFMailComposeResultCancelled
    // MFMailComposeResultSaved
    // MFMailComposeResultSent
    // MFMailComposeResultFailed
  
    if (result == MFMailComposeResultSent) {
        NSLog(@"發(fā)送成功");
    } else if (result == MFMailComposeResultFailed) {
        NSLog(@"發(fā)送失敗");
    }
    
    [controller dismissViewControllerAnimated:YES completion:nil];
}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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