Jeff Bootsholz
Jeff Bootsholz

Reputation: 3078

ios sending email with no attachment

I have implemented the module for sending the email from my iPhone device to server. When it comes to the implementation, there is no email file attach instead. I sweared I have added the attACHMENT . THE below is my working

-(IBAction)sendMail:(id)sender{


    NSArray *toRecipents = [NSArray arrayWithObjects:@"[email protected]",@"[email protected]",@"[email protected]",nil];
    mailComposer = [[MFMailComposeViewController alloc]init];
    if ([MFMailComposeViewController canSendMail]) {
        mailComposer.mailComposeDelegate = self;

        [mailComposer setSubject:@"NOXAV testing"];
        [mailComposer setToRecipients:toRecipents] ;

        [mailComposer setMessageBody:@"Testing message for the test mail" isHTML:NO];

        NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        //make a file name to write the data to using the documents directory:
        NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
                              documentsDirectory];
        NSData *fileData = [NSData dataWithContentsOfFile:fileName];

        NSString *mimeType = @"text/html";
        [mailComposer addAttachmentData:fileData mimeType:mimeType fileName:fileName];

        [self presentModalViewController:mailComposer animated:YES ];

    }

}


     -(void)mailComposeController:(MFMailComposeViewController *)controller
             didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
                 if (result) {
                     NSLog(@"Result : %d",result);
                 }
                 if (error) {
                     NSLog(@"Error : %@",error);
                 }
                 [self dismissModalViewControllerAnimated:YES];

             }

Upvotes: 1

Views: 510

Answers (3)

Yogesh Suthar
Yogesh Suthar

Reputation: 30488

The MIME type for text file should be text/plain,So change

NSString *mimeType = @"text/html";

to

NSString *mimeType = @"text/plain";

Edit

Also change the fileName here

[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:@"textfile.txt"];

Upvotes: 1

isarathg
isarathg

Reputation: 878

Your fileData may not be getting the data from file. Check if [filedata length] = some value.

Upvotes: 0

rishi
rishi

Reputation: 11839

Looks like file name problem, change following statement along with above suggested by Yogesh -

//make a file name to write the data to using the documents directory:
NSString * fileName = [documentsDirectory stringByAppendingPathComponent:@"textfile.txt"];

NSData *fileData = [NSData dataWithContentsOfFile:fileName];
NSString *mimeType = @"text/plain";

Upvotes: 0

Related Questions