如何在iOS中的单个邮件中附加多个文件

时间:2013-05-09 11:19:39

标签: iphone ios

我想以编程方式在iOS中以单个邮件发送多个附件文件。到目前为止,我已尝试过以下内容:

 // I give the file from array 
NSString *str_mail = [readingEmfReading objectAtIndex:0];
//  here I can encode the file
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
//here I can attach the file with extance of .csv
[controller addAttachmentData:myData mimeType:@".cvs" fileName:retriveEmail]  
//here I can set the body for mail 
[controller setMessageBody:@"file" isHTML:NO];
//here code for sent mail
if (controller) [self presentViewController:controller animated:YES completion:nil];

通过使用此代码,我只能发送一个附件。我想发送多个文件。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

你添加很多时间addAttachmentData并添加多个文件试试这段代码: - 添加这一行

NSString *str_mail = [readingEmfReading objectAtIndex:0];
//  here i can encoded the file
NSData *myData = [str_mail dataUsingEncoding:NSUTF8StringEncoding]

NSData *myData1 = [str_mail dataUsingEncoding:NSUTF8StringEncoding]
//here i can attach the file with extance of .csv
[controller addAttachmentData:myData mimeType:@".cvs" fileName:retriveEmail]  
//here i can set the body for mail 


// For second file 

NSData *myData1 = [str_mail dataUsingEncoding:NSUTF8StringEncoding]

[controller addAttachmentData:myData1 mimeType:@".cvs" fileName:retriveEmail] 

[controller setMessageBody:@"file" isHTML:NO];
//here code for sent mail
if (controller) [self presentViewController:controller animated:YES completion:nil];

答案 1 :(得分:0)

if([MFMailComposeViewController canSendMail])
        {
            [mailController setMailComposeDelegate:self];
            NSString* message=@"";
            NSString *filePath;
            for (int i=0; i<filesarray.count; i++)
            {
                if (i==filesarray.count-1)
                {
                    message=[message stringByAppendingFormat:@"%@ ",[filesarray objectAtIndex:i]];
                }
                else if (i==filesarray.count-2)
                {
                    message=[message stringByAppendingFormat:@"%@ and ",[filesarray objectAtIndex:i]];
                }
                else
                    message=[message stringByAppendingFormat:@"%@, ",[filesarray objectAtIndex:i]];
                NSString *datPath =[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
                datPath = [datPath stringByAppendingFormat:@"/%@.csv",[filesarray objectAtIndex:i]];
                filePath = datPath;
                [mailController addAttachmentData:[NSData dataWithContentsOfFile:filePath] mimeType:@"text/csv" fileName:[NSString stringWithFormat:@"%@.csv",[filesarray objectAtIndex:i]]];
            }
            [mailController setSubject:message];
            [mailController setMessageBody:@"" isHTML:NO];
            [self presentModalViewController:mailController animated:YES];
        }