ios发送没有附件的电子邮件

时间:2014-06-25 05:54:09

标签: ios objective-c email

我已经实施了模块,用于将电子邮件从我的iPhone设备发送到服务器。在实现方面,没有附加电子邮件文件。我发誓我添加了附件。以下是我的工作

-(IBAction)sendMail:(id)sender{


    NSArray *toRecipents = [NSArray arrayWithObjects:@"birth.lo@sdsdfsfsdf-hk.com",@"jason.li@asdasdad-hk.com",@"lo.sdad@gmail.com",nil];
    mailComposer = [[MFMailComposeViewController alloc]init];
    if ([MFMailComposeViewController canSendMail]) {
        mailComposer.mailComposeDelegate = self;

        [mailComposer setSubject:@"NOXAV testing"];
        [mailComposer setToRecipients:toRecipents] ;

        [mailComposer setMessageBody:@"Testing message for the test mail" isHTML:NO];

        NSArray *paths = NSSearchPathForDirectoriesInDomains
        (NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];

        //make a file name to write the data to using the documents directory:
        NSString *fileName = [NSString stringWithFormat:@"%@/textfile.txt",
                              documentsDirectory];
        NSData *fileData = [NSData dataWithContentsOfFile:fileName];

        NSString *mimeType = @"text/html";
        [mailComposer addAttachmentData:fileData mimeType:mimeType fileName:fileName];

        [self presentModalViewController:mailComposer animated:YES ];

    }

}


     -(void)mailComposeController:(MFMailComposeViewController *)controller
             didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
                 if (result) {
                     NSLog(@"Result : %d",result);
                 }
                 if (error) {
                     NSLog(@"Error : %@",error);
                 }
                 [self dismissModalViewControllerAnimated:YES];

             }

3 个答案:

答案 0 :(得分:1)

文本文件的MIME类型应为text/plain,因此请更改

NSString *mimeType = @"text/html";

NSString *mimeType = @"text/plain";

修改

同时更改文件名

[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:@"textfile.txt"];

答案 1 :(得分:0)

看起来像文件名问题,更改以下语句以及Yogesh建议的上述内容 -

//make a file name to write the data to using the documents directory:
NSString * fileName = [documentsDirectory stringByAppendingPathComponent:@"textfile.txt"];

NSData *fileData = [NSData dataWithContentsOfFile:fileName];
NSString *mimeType = @"text/plain";

答案 2 :(得分:0)

您的fileData可能无法从文件中获取数据。检查[filedata length]是否为某个值。