Reputation: 1
I am developing one iPhone application using Objective-C in which I have to send e-mails by attaching a text file with that. Could anyone please help me?
Upvotes: 0
Views: 1995
Reputation: 21805
This is kind of a working code in my app..SO just follow along. The file(text file) is in my documents directory of the app Make sure you have added MessageUIframework to your project and have these import in your interface file
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface Your CLass : UIViewController <MFMessageComposeViewControllerDelegate>
Code
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if ([mailClass canSendMail])
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Your Subject"];
NSFileManager* manager = [[NSFileManager alloc] init];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
if (![[self NoteFile] isEqualToString:@""]) //check if note is blank
{
NSString *NoteFilePath = [documentsDirectory
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",[self NoteFileName]]];
NSData *myNoteData = [NSData dataWithContentsOfFile:NoteFilePath];
[picker addAttachmentData:myNoteData mimeType:@"text/plain" fileName:@"Name you want to give your file.txt"];
}
[manager release];
// Fill out the email body text
NSString *emailBody = @"I am attaching my file!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
You can further check the mail status using its delegate methods
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
Upvotes: 0
Reputation: 2256
Hi take a look at this
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:@"Subject"];
NSString *fullPath =//get path of the file
NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
[composer addAttachmentData:fileData
mimeType:@"text/plain"
fileName:@"File_Name"];
NSString *emailBody = [NSString stringWithString:@"Body"];
[composer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];
Upvotes: 2
Reputation: 11174
if you're using a mail composer view controller:
[mailComposerViewController addAttachmentData:fileData mimeType:@"text/plain" fileName:@"name_of_text_file"];
Upvotes: 0