无法用邮件发送csv文件附件

时间:2009-11-21 09:26:14

标签: iphone attachment mfmailcomposeviewcontroller

我使用MFMailComposeViewController发送生成的报告(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:@"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];

5 个答案:

答案 0 :(得分:3)

在我的情况下,电子邮件从我的iPhone发送得很好,但是从我妻子的iPhone发送时没有附件。事实证明她的默认帐户设置为她的雅虎帐户,并且不允许附件。我只是将其切换到她的移动我帐户并附件已经完成。我使用gmail所以我从来没有遇到过问题。

此外,我曾尝试将MIME类型从text / csv切换到text / plain,但没有帮助。它可以在我的iPhone上运行,而不是在我妻子身上。

希望这有帮助!

答案 1 :(得分:1)

我尝试将.csv文件附加到“text / plain”&我成功了。我想你应该尝试一样。祝你好运。

答案 2 :(得分:1)

我遇到了同样的问题,我解决了问题 我真的很难看到我写的两个函数:

一个定义.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];
     ...

fileName当然是一个NSString,通过调用fileName函数来检索:         NSString * fileName = [self fileName];

希望这有帮助!

答案 3 :(得分:0)

如果您的代码看起来不错,我会怀疑myData没有加载数据nil。添加NSLog([myData description])或使用调试程序检查myData是否为nil

答案 4 :(得分:0)

我没有想法。如果我遇到这种情况,我会用你提供的示例代码创建一个独立的项目,看看我是否可以使它工作。通常在这些情况下,您关注的代码可能是正确的,并且错误来自其他地方。

此外:

  • 检查路径变量是否为零,因为这会导致附件不显示。
  • 确保邮件撰写视图的邮件正文中有一个图标。成功添加附件后,您应该在视图中看到图标表示。
  • 您可能希望将fileName设置为rainy.png,即[picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"]
祝你好运。

相关问题