开发者

MFMailComposeViewController csv attachment not being attached, but showing inline instead

开发者 https://www.devze.com 2022-12-10 14:46 出处:网络
I am having a problem with sending csv attachments via MFMailComposeViewController. Sometimes they come through just fine, but for other users they don\'t come through as attachments, but rather as te

I am having a problem with sending csv attachments via MFMailComposeViewController. Sometimes they come through just fine, but for other users they don't come through as attachments, but rather as text inline in the email (with <br/> instead of line returns.) It's very strange. Anybody know what I'm doing wrong? Here is a snippet of my code:

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

NSString *csv = @"foo,bar,blah,hello";
NSData *csvData = [csv dataUsingEncoding:NSUTF8StringEncoding];
[mailComposeViewController addAttachmentData:csvData mimeType:@"text/csv" fileName:@"testing.csv"];

[mailComposeViewController setSubject:@"testing sending csv attachment"];
[mailCompose开发者_运维知识库ViewController setMessageBody:@"csv file should be attached" isHTML:NO];
[self presentModalViewController:mailComposeViewController animated:YES];


-(IBAction)btnPressed:(id)sender {
    NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    NSString *docDir = [arrayPaths objectAtIndex:0];
    NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"];
    NSData *csvData = [NSData dataWithContentsOfFile:Path]; 

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

    [controller setSubject:@"For csv file..."];
    [controller setMessageBody:@"...csv file is hear.." isHTML:NO];
    [controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"];
    [self presentModalViewController:controller animated:YES];
    [controller release];
}


Hi I put sample code for Creating CSV file and attach it with mail but make sure you have to add MessageUI.Framework and import its related header "MessageUI/MessageUI.h" "MessageUI/MFMailComposeViewController.h" and deligate "MFMailComposeViewControllerDelegate"...I hope this wl useful for others

- (void)viewDidLoad {

arrCsv=[[NSArray alloc]initWithObjects:@"Hello",@"Hi",@"traun",@"fine",nil];
NSArray *paths = NSSearchPathForDirectoriesInDomains

(NSDocumentDirectory, NSUserDomainMask, YES);

NSString *documentsDirectory = [paths objectAtIndex:0];

NSString *fileName = [NSString stringWithFormat:@"%@/try.csv", documentsDirectory];

[[arrCsv componentsJoinedByString:@","] writeToFile:fileName atomically:YES encoding:NSUTF8StringEncoding error:NULL];

 }



-(ibAction)btnMail   {

 NSArray *arrayPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *docDir = [arrayPaths objectAtIndex:0];
NSString *Path = [docDir stringByAppendingString:@"/CSVFile.csv"];
NSData *csvData = [NSData dataWithContentsOfFile:Path]; 
MFMailComposeViewController *controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = self;
[controller setSubject:@"For csv file..."];
[controller setMessageBody:@"...csv file is hear.." isHTML:NO];
[controller addAttachmentData:csvData mimeType:@"text/csv" fileName:@"CSVFile.csv"];
[self presentModalViewController:controller animated:YES];
[controller release];

}


- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{   message.hidden = NO;
switch (result)
{
    case MFMailComposeResultCancelled:
        message.text = @"Result: canceled";
        break;
    case MFMailComposeResultSaved:
        message.text = @"Result: saved";
        break;
    case MFMailComposeResultSent:
        message.text = @"Result: sent";
        break;
    case MFMailComposeResultFailed:
        message.text = @"Result: failed";
        break;
    default:
        message.text = @"Result: not sent";
        break;
}
[self dismissModalViewControllerAnimated:YES];
}


set the mime type as "application/octet-stream" and that should do the trick to remove inline attachments (I still named the extension of my file i.e. pdf)


I believe the second parameter to setMessageBody:isHTML: must be YES for attachments to not show up inline.


Even if you set isHTML param to YES, your message body can be sent as plain/text if the message body can be represented as such. And attachments in plain/text messages are not always recognized correctly by some email clients (Outlook).

In my case adding a link in the message body helped. Formatting text as bold with HTML tags works too. Tricky!

Tested on iPod 1G 3.1.3.


This may not be the case here, but one thing to watch out for is that:

[NSString dataUsingEncoding:] 

returns a valid but empty NSData object if the conversion to the specified encoding is not possible. Better to use the full version:

[NSString dataUsingEncoding: s allowLossyConversion: YES]

Or check the length of the returned data. It appears that zero-length data attachments are trimmed somewhere in the mail process.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号