简体   繁体   中英

Can't sent csv file attachment with mail

I have used the MFMailComposeViewController to send the generated report(csv).

Now mail is sent to To:email id, & but when i checked the mails i did received the mail but attachment was not there.

Then I also tried MailComposer example :

https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html

in which the png image is attached to mail demo. I also sent mail using that app, But same result image attachment is not delivered.

Help, to find what's the problem? Thanks in advance.

Here is code in that app :

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;

[picker setSubject:@"Hello from California!"];


// 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 an image to the email
NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
NSData *myData = [NSData dataWithContentsOfFile:path];
[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];

// Fill out the email body text
NSString *emailBody = @"It is raining in sunny California!";
[picker setMessageBody:emailBody isHTML:NO];

[self presentModalViewController:picker animated:YES];
[picker release];

In my situation the email was sent fine from my iPhone, but there was no attachment when sending from my wife's iPhone. Turns out that her default account was set to her Yahoo account and that was not allowing attachments. I simply switched it to her mobile Me account and the attachment was made. I use gmail so I never had a problem.

in addition, I had tried switching the MIME type from text/csv to text/plain and that did not help. It worked either way on my iPhone and not at all on my wife's.

Hope this helps!

I have tried to attach .csv file with "text/plain" & I got success. I think you should try the same. best of luck.

I ran into the same problems and I fixed the problem by doing a really hard look at two functions that I wrote:

One defines the filename from a .plist file, the other one actually sets the location of the file (including it's path AND filename).

Using the right function to retrieve the filename in the MFMailViewController fixed the issue:

    -(NSString *)dataFilePath { //this gets the ,plist file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];

return [documentsDirectory stringByAppendingPathComponent:kFilename];

//----------------------------

    -(NSString *)fileName { // this defines the fileName from the values in the .plist

NSString *patientDetails = [self dataFilePath];
NSArray *dataArray = [[NSArray alloc] initWithContentsOfFile:patientDetails];

NSString *firstName = [dataArray objectAtIndex:0];
NSString *lastName = [dataArray objectAtIndex:1];

NSString *fileName = [firstName stringByAppendingString:lastName];

return [fileName stringByAppendingString:@".csv"];

//-------------------------

     -(NSString *)setFileLocation {  //this puts name and folder together.  

NSArray *sysPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDirectory = [sysPaths objectAtIndex:0];
NSString *fileName = [self fileName];   
NSString *fullFilePath = [NSString stringWithFormat:@"%@/%@", docDirectory,fileName];
  // NSLog(@"Filepath is : %@",fullFilePath);
return fullFilePath;

}

//-------------------------

The last one is the function you want to call in your MFMailComposeViewController:

     ...
NSData *csvData = [NSData dataWithContentsOfFile:[self setFileLocation]]; 
    [picker addAttachmentData:csvData mimeType:@"text/csv" fileName:fileName];
     ...

fileName, of course, is an NSString, retrieved by calling the fileName function: NSString *fileName = [self fileName];

Hope this helps!

If your code looks okay I'll suspect that that myData didn't get loaded with data is nil . Put in an NSLog([myData description]) or use the debugger to check that myData is not nil .

I'm out of ideas. If I was stuck in this situation I would create a stand alone project with the example code you provided and see if I could get it working. Usually in these situations the code you are focusing on is probably correct and error is coming from somewhere else.

Also:

  • Check to see if the path variable is nil because that will cause the attachment to not appear.
  • Make sure there is a icon in message body the mail compose view. When you successfully add an attachment you should see the icon representation in the view.
  • And you probably want set your fileName to rainy.png , ie [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"] .

Good luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM