无法通过邮件发送 csv 文件附件

发布于 2024-08-12 04:47:27 字数 1980 浏览 10 评论 0原文

我已使用 MFMailComposeViewController 发送生成的报告(csv)。

现在邮件已发送至 To:email 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];

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:@"[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];

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

感情旳空白 2024-08-19 04:47:27

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

另外,我尝试将 MIME 类型从 text/csv 切换到 text/plain,但这没有帮助。无论哪种方式,它在我的 iPhone 上都有效,但在我妻子的 iPhone 上根本不起作用。

希望这有帮助!

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!

蹲墙角沉默 2024-08-19 04:47:27

我尝试附加带有“text/plain”和“text/plain”的 .csv 文件。我成功了。我想你也应该尝试一下。祝你好运。

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

朦胧时间 2024-08-19 04:47:27

我遇到了同样的问题,我通过以下方式解决了问题
仔细查看我编写的两个函数:

一个从 .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 *文件名 = [自身文件名];

希望这有帮助!

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!

二手情话 2024-08-19 04:47:27

如果您的代码看起来没问题,我会怀疑 myData 没有加载数据 nil。放入 NSLog([myData description]) 或使用调试器检查 myData 是否不 nil

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.

时光倒影 2024-08-19 04:47:27

我没主意了。如果我陷入这种情况,我会使用您提供的示例代码创建一个独立的项目,看看是否可以让它工作。通常在这些情况下,您关注的代码可能是正确的,而错误来自其他地方。

另外:

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

祝你好运。

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, i.e. [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy.png"].

Good luck.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文