ios發送郵件

來源:互聯網
上載者:User

標籤:des   blog   http   使用   os   html   

方法一:

1.需要引入庫MessageUI.framework

#import <MessageUI/MessageUI.h>

#import<MessageUI/MFMailComposeViewController.h>

 

[email protected] ViewController : UIXXXXXViewController <..., MFMailComposeViewControllerDelegate>  

   @end  

 

3.發送執行代碼。事先驗證相關支援。 

    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));    if (!mailClass) {        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"發送郵件"                                                         message:@"當前系統版本不支援應用內發送郵件功能,您可以使用mailto方法代替"                                                        delegate:self                                               cancelButtonTitle:@"我知道啦"                                               otherButtonTitles: nil] autorelease];        [alert show];                return;    }    if (![mailClass canSendMail]) {        UIAlertView *alert = [[[UIAlertView alloc] initWithTitle:@"發送郵件"                                                         message:@"使用者沒有設定郵件賬戶"                                                        delegate:self                                               cancelButtonTitle:@"我知道啦"                                               otherButtonTitles: nil] autorelease];        [alert show];        return;    }        MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];    mc.mailComposeDelegate = self;    [mc setSubject:@"Hello, World!"];    [mc setToRecipients:[NSArray arrayWithObject:@"[email protected]"]];//    [mc setCcRecipients:[NSArray arrayWithObject:@"[email protected]"]];//    [mc setBccRecipients:[NSArray arrayWithObject:@"[email protected]"]];    [mc setMessageBody:@"Hello,slick!!!\n\nCome here, I need you!" isHTML:NO];        // 添加一張圖片    UIImage *addPic = [UIImage imageNamed: @"[email protected]"];    NSData *imageData = UIImagePNGRepresentation(addPic);            // png    [mc addAttachmentData: imageData mimeType: @"" fileName: @"Icon.png"];        //添加一個pdf附件    NSString *file = [self fullBundlePathFromRelativePath:@"高品質C++編程指南.pdf"];    NSData *pdf = [NSData dataWithContentsOfFile:file];    [mc addAttachmentData: pdf mimeType: @"" fileName: @"高品質C++編程指南.pdf"];        [self presentViewController:mc animated:YES completion:nil];    [mc release];

 

 回呼函數:

- (void)mailComposeController:(MFMailComposeViewController*)controller          didFinishWithResult:(MFMailComposeResult)result                        error:(NSError*)error {    switch (result)    {        case MFMailComposeResultCancelled:            NSLog(@"Mail send canceled...");            break;        case MFMailComposeResultSaved:            NSLog(@"Mail saved...");            break;        case MFMailComposeResultSent:            NSLog(@"Mail sent...");            break;        case MFMailComposeResultFailed:            NSLog(@"Mail send errored: %@...", [error localizedDescription]);            break;        default:            break;    }    [self dismissViewControllerAnimated:YES completion:nil];}

 

 

 

方法二:

url方式

#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]];     }  

即 [[UIApplicationsharedApplication] openURL:[NSURLURLWithString:@"mailto:[email protected][email protected]&subject=Greetings%20from%20Cupertino!&body=Wish%20you%20were%20here!"]];

 

還可使用skpsmtpmessage這樣的第三方控制項。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.