无法通过邮件发送csv文件附件
我已经使用MFMailComposeViewController发送生成的报告(csv)。无法通过邮件发送csv文件附件
现在邮件被发送到:电子邮件ID,&但是当我检查邮件我收到了邮件但附件不在那里。
然后我还试图MailComposer例如:
https://developer.apple.com/iphone/library/samplecode/MailComposer/index.html
其中PNG图像被附加到演示邮件。我还使用该应用程序发送邮件,但未传送相同的结果图像附件。
帮忙,找出有什么问题? 在此先感谢。
这里是该应用代码:
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Hello from California!"];
// Set up recipients
NSArray *toRecipients = [NSArray arrayWithObject:@"[email protected]"];
NSArray *ccRecipients = [NSArray arrayWithObjects:@"[email protected]", @"[email protected]", nil];
NSArray *bccRecipients = [NSArray arrayWithObject:@"[email protected]"];
[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];
如果你的代码看起来不错,我会怀疑是myData
没有得到加载数据nil
。输入NSLog([myData description])
或使用调试器检查myData
不是nil
。
我出来的想法。如果我被困在这种情况下,我会用你提供的示例代码创建一个独立的项目,看看我是否能够正常工作。通常在这些情况下,您关注的代码可能是正确的,错误来自其他地方。
另外:
- 检查是否路径变量是零,因为这将导致附件无法显示。
- 确保邮件正文中有邮件撰写视图的图标。当您成功添加附件时,您应该在视图中看到图标表示。
- 而且您可能希望将您的
fileName
设置为rainy.png
,即[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"]
。
祝你好运。
谢谢..我检查了路径,它是真实的和邮件camposerview显示附件图标表示.... – Rambo 2009-11-27 09:55:16
我试图用“text/plain”附加.csv文件&我成功了。我想你应该尝试一样的。祝你好运。
在我的情况下,我的iPhone发送的邮件很好,但从我妻子的iPhone发送时没有任何附件。原来,她的默认帐户设置为她的雅虎帐户,并且不允许附件。我只是将它切换到她的移动我的帐户,并附上了。我使用Gmail,所以我从来没有遇到过问题。
此外,我曾尝试将MIME类型从text/csv切换到text/plain,但没有任何帮助。它在我的iPhone上以任何方式工作,而根本不在我妻子的身上。
希望这会有所帮助!
我遇到同样的问题,我通过 解决了这一问题做的,我写了两个函数的真的很难看:
One从定义文件名。plist文件,另一个实际上设置文件的位置(包括它的路径和文件名) 。
使用正确的函数来检索文件名中MFMailViewController 固定的问题:
-(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;
}
// -------------------------
最后一个是你想在你MFMailComposeViewController调用函数:
...
NSData *csvData = [NSData dataWithContentsOfFile:[self setFileLocation]];
[picker addAttachmentData:csvData mimeType:@"text/csv" fileName:fileName];
...
文件名,当然是一个NSString,通过调用文件名功能检索到: 的NSString *文件名= [自文件名];
希望这会有所帮助!
谢谢兄弟... 我检查了mydata,它不是零.. !!!! 但我仍无法找到附件。 – Nic 2009-11-21 10:38:38