发邮件 iphone

form:http://www.cocoachina.com/bbs/read.php?tid-59626.html

  
  
  
  
  1. 添加MessageUI. framework
  2. #import <MessageUI/MessageUI.h>
  3. MFMailComposeViewControllerDelegate
#pragma mark -
#pragma mark MFMailComposeViewController
- (void) alertWithTitle:(NSString *)_title_ msg:(NSString *)msg {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:_title_ 
                                                    message:msg 
                                                   delegate:nil 
                                          cancelButtonTitle:@"确定" 
                                          otherButtonTitles:nil];
    [alert show];
    [alert release];
}
 
-(void)displayComposerSheet {
    MFMailComposeViewController *mailPicker = [[MFMailComposeViewController alloc] init];
    mailPicker.mailComposeDelegate = self;
 
    [mailPicker setSubject:@"eMail主题"];
 
    // 添加发送者
    NSArray *toRecipients = [NSArray arrayWithObject:@"[email][email protected][/email]"];
    //NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email][email protected][/email]", @"[email][email protected][/email]", nil];
    //NSArray *bccRecipients = [NSArray arrayWithObject:@"[email][email protected][/email]", nil];
    [mailPicker setToRecipients:toRecipients];
    //[picker setCcRecipients:ccRecipients];    
    //[picker setBccRecipients:bccRecipients];
 
    // 添加图片
    UIImage *addPic = [UIImage imageNamed:@"Icon.png"];
    NSData *imageData = UIImagePNGRepresentation(addPic);            // png
    // NSData *imageData = UIImageJPEGRepresentation(addPic, 1);    // jpeg
    [mailPicker addAttachmentData:imageData mimeType:@"" fileName:@"Icon.png"];
 
    NSString *emailBody = @"eMail 正文";
    [mailPicker setMessageBody:emailBody isHTML:YES];
 
 
 
 
    [self presentModalViewController:mailPicker animated:YES];
    [mailPicker release];
}
 
-(void)launchMailAppOnDevice {
    NSString *recipients = @"mailto:[email protected]&subject=my email!";
    //@"mailto:[email protected][email protected],[email][email protected][/email]&subject=my email!";
    NSString *body = @"&body=email body!";
 
    NSString *email = [NSString stringWithFormat:@"%@%@", recipients, body];
    email = [email stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:email]];
}
 
-(void)sendEMail {
    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
 
    if (mailClass != nil) {
        if ([mailClass canSendMail]) {
            [self displayComposerSheet];
        } else {
            [self launchMailAppOnDevice];
        }
    } else {
        [self launchMailAppOnDevice];
    }    
}
- (void)mailComposeController:(MFMailComposeViewController *)controller 
          didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
    NSString *msg;
 
    switch (result) {
        case MFMailComposeResultCancelled:
            msg = @"邮件发送取消";
            break;
        case MFMailComposeResultSaved:
            msg = @"邮件保存成功";
            [self alertWithTitle:nil msg:msg];
            break;
        case MFMailComposeResultSent:
            msg = @"邮件发送成功";
            [self alertWithTitle:nil msg:msg];
            break;
        case MFMailComposeResultFailed:
            msg = @"邮件发送失败";
            [self alertWithTitle:nil msg:msg];
            break;
        default:
            break;
    }
 
    NSLog(@"发送结果:%@", msg);
    [self dismissModalViewControllerAnimated:YES];
}
 

你可能感兴趣的:(发邮件 iphone)