问题
I want to send .doc file as an email attachment from my app programmatically.
回答1:
Use -[MFMailComposeViewController addAttachmentData:] with a mimeType of "application/msword". For example:
- (void)displayComposerSheet {
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"I'm attaching a word document!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"first@example.com"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"second@example.com", @"third@example.com", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"fourth@example.com"];
[picker setToRecipients:toRecipients];
[picker setCcRecipients:ccRecipients];
[picker setBccRecipients:bccRecipients];
// Attach a doc to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyDocument" ofType:@"doc"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"application/msword" fileName:@"MyDocument"];
// Fill out the email body text
NSString *emailBody = @"Please see the attached document.";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
}
来源:https://stackoverflow.com/questions/5509618/sending-doc-file-as-an-attachment-on-ipad