ios 發送郵件,簡訊

來源:互聯網
上載者:User

標籤:cti   use   dma   smis   object   關閉   www.   suse   sage   

第一種方式:openURL
    #pragma mark - 使用系統郵件用戶端發送郵件       -(void)launchMailApp       {             NSMutableString *mailUrl = [[[NSMutableString alloc]init]autorelease];           //添加收件者           NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];           [mailUrl appendFormat:@"mailto:%@", [toRecipients componentsJoinedByString:@","]];           //添加抄送           NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];             [mailUrl appendFormat:@"?cc=%@", [ccRecipients componentsJoinedByString:@","]];           //添加密送           NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];             [mailUrl appendFormat:@"&bcc=%@", [bccRecipients componentsJoinedByString:@","]];           //添加主題           [mailUrl appendString:@"&subject=my email"];           //添加郵件內容           [mailUrl appendString:@"&body=<b>email</b> body!"];           NSString* email = [mailUrl stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];             [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];         }  
第二種方式:MFMailComposeViewController

讓控制器遵守<MFMailComposeViewControllerDelegate>協議

#pragma mark - 在應用內發送郵件//啟用郵件功能- (void)sendMailInApp{    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));    if (!mailClass) {        [WSYFunction alertViewWithTitle:@"提示" message:@"當前系統版本不支援應用內發送郵件功能,您可以使用mailto方法代替" buttonTitle:@"確定"];        return;    }    if (![mailClass canSendMail]) {        [WSYFunction alertViewWithTitle:@"提示" message:@"使用者沒有設定郵件賬戶" buttonTitle:@"確定"];        return;    }    [self displayMailPicker];}//調出郵件發送視窗- (void)displayMailPicker{    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];    mailPicker.mailComposeDelegate = self;        //設定主題    [mailPicker setSubject: [NSString stringWithFormat:@"%@",self.bookNameArray[ChooseBook]]];    //添加收件者    NSArray *toRecipients = [NSArray arrayWithObject: @""];    [mailPicker setToRecipients: toRecipients];    //添加抄送//    NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];//    [mailPicker setCcRecipients:ccRecipients];    //添加密送//    NSArray *bccRecipients = [NSArray arrayWithObjects:@"[email protected]", nil];//    [mailPicker setBccRecipients:bccRecipients];    //    // 添加一張圖片    UIImage *addPic = [UIImage imageNamed: @"lanch.jpeg"];    NSData *imageData = UIImagePNGRepresentation(addPic);            // png    //關於mimeType:http://www.iana.org/assignments/media-types/index.html    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"lanch.jpeg"];    //構造字串檔案的儲存路徑    // 擷取Document目錄    NSString *docPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];    NSString *strPath = [docPath stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]];    //構造字串對象    NSString *book_str = @"";    for (int i = 0; i < self.array.count; i++) {        book_str = [book_str stringByAppendingString:[NSString stringWithFormat:@"\n\n%@",self.array[i]]];    }    //通過將writeToFile:atomically:encoding:error:方法發送給字串對象完成字串儲存到檔案內的功能    [book_str writeToFile:strPath atomically:YES encoding:NSUTF8StringEncoding error:nil];        NSData *bookData = [NSData dataWithContentsOfFile:strPath];        //發送txt文本附件    [mailPicker addAttachmentData:bookData mimeType:@"text/txt" fileName:[NSString stringWithFormat:@"%@.txt",self.bookNameArray[ChooseBook]]];    NSString *emailBody = @"<font color=‘red‘>eMail</font> 本文";    [mailPicker setMessageBody:emailBody isHTML:YES];    [self presentModalViewController: mailPicker animated:YES];}#pragma mark - 實現 MFMailComposeViewControllerDelegate- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{    //關閉郵件發送視窗    [self dismissModalViewControllerAnimated:YES];    NSString *msg;    switch (result) {        case MFMailComposeResultCancelled:            msg = @"您已取消編輯郵件";            break;        case MFMailComposeResultSaved:            msg = @"您已成功儲存郵件";            break;        case MFMailComposeResultSent:            msg = @"您點擊發送,將郵件放到隊列中,還沒發送";            break;        case MFMailComposeResultFailed:            msg = @"您試圖儲存或者發送郵件失敗";            break;        default:            msg = @"";            break;    }    [WSYFunction alertViewWithTitle:@"" message:msg buttonTitle:@"確定"];}

最後使用直接調用方法:

[self sendMailInApp];

 

ios 發送郵件,簡訊

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.