文本文件在ios目标c的邮件正文中显示为一个小框

Posted

技术标签:

【中文标题】文本文件在ios目标c的邮件正文中显示为一个小框【英文标题】:Text file appear as a small box in mail body in ios objective c 【发布时间】:2017-08-21 08:29:05 【问题描述】:

我想在邮件中发送 2 个附件。一个是图像,另一个是文本文件。但是当我在单个邮件中发送两个附件时,文本文件显示为一个方形空白框。图像和文本文件,这两个文件都在接收方接收并且也是可读的。但唯一的问题是发件人在发送邮件之前无法看到邮件正文中的文本文件。发送邮件的代码如下。

- (void)showEmail:(NSString*)file     

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString = [dateFormatter dateFromString:abc4];
    [dateFormatter setDateFormat:@"MM/dd/yyyy"];
    NSString *titledate=[dateFormatter stringFromDate:dateFromString];
    NSString *emailTitle = [NSString stringWithFormat:@"(%@)",titledate];
    [dateFormatter setDateFormat:@"MM/dd/yyyy HH:mm:ss"];
    NSString *subjectdate=[dateFormatter stringFromDate:dateFromString];
    NSString *messageBody = [NSString stringWithFormat:@"%@ %@",abc,subjectdate];
    //NSArray *toRecipents = [NSArray arrayWithObject:self.txtfldEmail.text];


    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:emailTitle];
    [mc setMessageBody:messageBody ishtml:NO];
    //[mc setToRecipients:toRecipents];    

    // Determine the file name and extension

    NSArray *filepart = [file componentsSeparatedByString:@"."];
    NSString *filename = [filepart objectAtIndex:0];
    NSString *extension = [filepart lastObject];

    // Get the resource path and read the file using NSData
    //NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];   

    //NSString *xyz=[[NSUserDefaults standardUserDefaults]valueForKey:@"readpath"];
    NSData *fileData;
    NSData *fileData1;

    if ([title isEqualToString:@"Excel"]) 
        fileData = [NSData dataWithContentsOfFile:filePath];
        NSString *xyz=[[NSUserDefaults standardUserDefaults]valueForKey:@"commentfilelocation"];
        fileData1=[NSData dataWithContentsOfFile:xyz];
        NSLog(@"fileData1>>%@",fileData1);
    
    else if([title isEqualToString:@"Screenshot"])
    
    NSString *xyz=[[NSUserDefaults standardUserDefaults]valueForKey:@"readpath"];
    fileData = [NSData dataWithContentsOfFile:xyz];
    NSString *xyz1=[[NSUserDefaults standardUserDefaults]valueForKey:@"commentfilelocation"];
    NSLog(@"xyz1>>%@",[[NSUserDefaults standardUserDefaults]valueForKey:@"commentfilelocation"]);
    fileData1=[NSData dataWithContentsOfFile:xyz1]; 
    

    // Determine the MIME type
    NSString *mimeType;
    if ([extension isEqualToString:@"jpg"]) 
        mimeType = @"image/jpeg";
     else if ([extension isEqualToString:@"png"]) 
        mimeType = @"image/png";
     else if ([extension isEqualToString:@"doc"]) 
        mimeType = @"application/msword";
     else if ([extension isEqualToString:@"ppt"]) 
        mimeType = @"application/vnd.ms-powerpoint";
     else if ([extension isEqualToString:@"html"]) 
        mimeType = @"text/html";
     else if ([extension isEqualToString:@"txt"]) 
        mimeType = @"text/html";
     else if ([extension isEqualToString:@"pdf"]) 
        mimeType = @"application/pdf";
     else if ([extension isEqualToString:@"csv"]) 
        mimeType = @"text/csv";
    

    NSString *mimeType1;
    if ([extension isEqualToString:@"jpg"]) 
        mimeType1 = @"image/jpeg";
     else if ([extension isEqualToString:@"png"]) 
        mimeType1 = @"image/png";
     else if ([extension isEqualToString:@"doc"]) 
        mimeType1 = @"application/msword";
     else if ([extension isEqualToString:@"ppt"]) 
        mimeType1 = @"application/vnd.ms-powerpoint";
     else if ([extension isEqualToString:@"html"]) 
        mimeType1 = @"text/html";
    else if ([extension isEqualToString:@"txt"]) 
        mimeType1 = @"text/html";
    else if ([extension isEqualToString:@"pdf"]) 
        mimeType1 = @"application/pdf";
    else if ([extension isEqualToString:@"csv"]) 
        mimeType1 = @"text/csv";
    

    // Add attachment
    [mc addAttachmentData:fileData mimeType:mimeType fileName:file];  
    NSDateFormatter *dateFormatter1 = [[NSDateFormatter alloc] init];
    [dateFormatter1 setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *dateFromString1 = [dateFormatter1 dateFromString:abc4];
    [dateFormatter1 setDateFormat:@"MM.dd.yyyy"];
    NSString *titledate1=[dateFormatter1 stringFromDate:dateFromString1];
    NSString *commentfilename=[NSString stringWithFormat:@"%@ %@ comments.txt",abc,titledate1];
    [mc addAttachmentData:fileData1 mimeType:mimeType1 fileName:commentfilename];
    [![enter image description here][1]][1]
    // Present mail view controller on screen
    [self presentViewController:mc animated:YES completion:NULL];

邮件截图如问题所附。

【问题讨论】:

【参考方案1】:

您可能应该尝试更改 mime 类型。 例如,您是否尝试过使用 text/plain?而不是

if ([extension isEqualToString:@"txt"]) 
        mimeType1 = @"text/html";

【讨论】:

以上是关于文本文件在ios目标c的邮件正文中显示为一个小框的主要内容,如果未能解决你的问题,请参考以下文章

文本旋转属性在 PHP 的电子邮件正文中不起作用

通过 Outlook 插件编辑电子邮件正文中的文本

如何在 ios 的 UIactivity 视图控制器中仅显示 facebook、twitter 和电子邮件(目标 c)?

Outlook 将图像显示为在邮件的 HTML 正文中引用的附件

正文中粘贴图表和文本的 VBA 电子邮件

如何插入到Outlook电子邮件正文中的HTML文件