发送邮件---iphone开发

1、导入MessageUI.framework包

2、引入头文件#import <MessageUI/MessageUI.h> 

3、实现代理MFMailComposeViewControllerDelegate

代码如下:

1、监测手机是否遇有首发邮件功能

Class mailClass = (NSClassFromString(@"MFMailComposeViewController")); 
                    if (mailClass != nil)  
                    {  
                        if ([mailClass canSendMail])  
                        {  
                            [self displayComposerSheet];  
                        }   
                        else   
                        {  
                            [self launchMailAppOnDevice];  
                        }  
                    }   
                    else   
                    {  
                        [self launchMailAppOnDevice];  
                    }      

2、可以发送邮件

-(void)displayComposerSheet   
{  
    NSLog(@"可以发送邮件~~~~~~~~~~~~");
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];  
    
    mailPicker.mailComposeDelegate = self;  
    
    //设置主题  
    [mailPicker setSubject: @"eMail主题"];  
    
    // 添加发送者  
    NSArray *toRecipients = [NSArray arrayWithObject: @"[email protected]"];  
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];  
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]", nil];  
    [mailPicker setToRecipients: toRecipients];  
    //[picker setCcRecipients:ccRecipients];      
    //[picker setBccRecipients:bccRecipients];  
    
    // 添加图片  
//    UIImage *addPic = [UIImage imageNamed: @"123.jpg"];  
//    NSData *imageData = UIImagePNGRepresentation(addPic);            // png  
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg  
//    [mailPicker addAttachmentData: imageData mimeType: @"" fileName: @"123.jpg"];  
    
    NSString *emailBody = @"您好:您订阅的杂志,请点击一下连接进行支付!";  
    [mailPicker setMessageBody:emailBody isHTML:YES];  
    
    [self presentModalViewController: mailPicker animated:YES];  
    [mailPicker release];  
}  

3、不能发送邮件
-(void)launchMailAppOnDevice  
{  
    NSLog(@"不能发送邮件~~~~~~~~~~~~");
    NSString *recipients = @"mailto:[email protected]&subject=my email!";  
    //@"mailto:[email protected][email protected],[email protected]&subject=my email!";  
    NSString *body = @"&body=email body!";  
    
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];  
    email = [email stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding];  
    
    [[UIApplication sharedApplication] openURL: [NSURL URLWithString:email]];  
}

4、代理方法
- (void)mailComposeController:(MFMailComposeViewController *)controller   
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error   
{  
    NSString *msg;  
    
    switch (result)   
    {  
        case MFMailComposeResultCancelled:  
            msg = @"邮件发送取消";  
            break;  
        case MFMailComposeResultSaved:  
            msg = @"邮件保存成功";  
            [Utils alertWithTitle:nil message:msg];  
            break;  
        case MFMailComposeResultSent:  
            msg = @"邮件发送成功";  
            [Utils alertWithTitle:nil message:msg]; 
            break;  
        case MFMailComposeResultFailed:  
            msg = @"邮件发送失败";  
            [Utils alertWithTitle:nil message:msg];  
            break;  
        default:  
            break;  
    }  
    
    [self dismissModalViewControllerAnimated:YES];  
}  
 

猜你喜欢

转载自lijinfengjava.iteye.com/blog/1687337