[英]ios sending email with no attachment
我已经实现了用于将电子邮件从我的iPhone设备发送到服务器的模块。 在实现方面,没有电子邮件文件附件。 我发誓我已经添加了attACHMENT。 以下是我的工作
-(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";
至
NSString *mimeType = @"text/plain";
在这里也更改文件名
[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]是否为某个值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.