[英]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.