问题
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];
回答1:
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!
回答2:
I have tried to attach .csv file with "text/plain" & I got success. I think you should try the same. best of luck.
回答3:
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!
回答4:
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
.
回答5:
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
torainy.png
, i.e.[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"]
.
Good luck.
来源:https://stackoverflow.com/questions/1775022/cant-sent-csv-file-attachment-with-mail