ios正在发送无附件的电子邮件
我已经实现了将电子邮件从iPhone设备发送到服务器的模块。在实现方面,没有电子邮件文件附加。我发誓我已经添加了附件。下面是我的作品ios正在发送无附件的电子邮件,ios,objective-c,email,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]; mailCom
-(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];
}
文本文件的MIME类型应为
text/plain
,因此请更改
NSString *mimeType = @"text/html";
到
编辑
也可以在这里更改文件名
[mailComposer addAttachmentData:fileData mimeType:mimeType fileName:@"textfile.txt"];
看起来像是文件名问题,更改下面的语句以及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";
您的fileData可能无法从文件中获取数据。检查[filedata length]=某个值。已更改,但仍然有效cannot@RajuGujarati检查您是否正在获取文件数据。我已检查是否缺少文件名上的反斜杠,但接收者收到的文件内容相同,但文件名不同。为了提供相同的文件名,我应该在细节上实现什么?@RajuGujarati请参见编辑的答案,您只需在此处设置实际的文件名。
//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";