Search code examples
iphoneobjective-cnsmutablearraynsdatamfmailcomposeviewcontroller

Data Attachement in E-mail?


I save my objects in NSMutablearray in NSData formate.No i am try to attached in E-mail Body.here is code.

  - (IBAction)sendEmail
     {

   if ([MFMailComposeViewController canSendMail])
  {
       NSArray *recipients = [NSArray arrayWithObject:@"[email protected]"];
       MFMailComposeViewController *controller = [[MFMailComposeViewController 
            alloc] init];
       controller.mailComposeDelegate = self;
       [controller setSubject:@"Iphone Game"];
       NSString *string = [viewArray componentsJoinedByString:@"\n"];
       NSString *emailBody = string; 
       NSLog(@"test=%@",emailBody);
       [controller setMessageBody:emailBody isHTML:YES];
       [controller setToRecipients:recipients];
       [self presentModalViewController:controller animated:YES];
       [controller release];
    }
else 
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Alert" 
            message:@"Your device is not set up for email." delegate:self 
            cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];   
    [alert release];
}

 }

I get no error,but not see any data in E-mail.in NSLog i see this..2012-05-07 15:33:22.984 Note List[273:207] test=>]please suggest any one better solution for me how i attached my NSMutableArray data in E-mail body..


Solution

  • i'm not clear with your question, try this way to set your data. and check the values you are going to set before passing it to composer,

    see this

    MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
        picker.mailComposeDelegate = self;
    
        NSString *mSubject;
    
        if(isInvite)
        {
            mSubject=@"TAPP Invitation";
        }
        else 
        {
            mSubject= @"TAPP email";
        }
    
        [picker setSubject:mSubject];
    
        NSString *mBody;
    
        if(isInvite)
        {
            NSString *pTappId=[[DataModel sharedDataModel] getValueForKey:USER_TAPP_ID];
            NSString *currentUserName=[[DataModel sharedDataModel] getValueForKey:CURRENT_USER_NAME];
             mBody=[NSString stringWithFormat:@"<HTML><BODY>Hi,<br><br>We already know one another, and I would like us to keep in touch.<br><br>Let's connect through TAPP (<a href=\"http://download.mytapp.com\">Download Here</a>) a smarter, private way to exchange and manage contact information.<br><br>Join TAPP and secure your preferred, unique ID before it is too late, and then connect with me. My TAPP ID is %@.<br><br>For more information, <a href=\"http://www.mytapp.com\">click here</a><br><br>Regards,<br><br>%@</BODY></HTML>",pTappId,currentUserName];
        }
        else 
        {
            mBody= [NSString stringWithFormat:@"<HTML><BODY><br /><br />Connected by <a href=http://www.mytapp.com>TAPP</a></BODY></HTML>"];
        }
    
        // Set up recipients
        NSArray *toRecipients = [NSArray arrayWithObject:selectedEmailId]; 
        //NSArray *toRecipients = [NSArray arrayWithObject:@""]; 
    
        [picker setToRecipients:toRecipients];
    
        // Attach an image to the email
        //NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
    
        //NSData *myData = UIImagePNGRepresentation(photo.image);
        //[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"abc.png"];
    
        // Fill out the email body text
        NSString *emailBody = mBody;
        [picker setMessageBody:emailBody isHTML:YES];
    
        [self presentModalViewController:picker animated:YES];
    
        [picker release];