我正在使用Objective-C开发一个iPhone应用程序,我必须通过附加文本文件来发送电子邮件。有人可以帮帮我吗?
答案 0 :(得分:2)
嗨,看看这个
MFMailComposeViewController *composer = [[MFMailComposeViewController alloc] init];
composer.mailComposeDelegate = self;
[composer setSubject:@"Subject"];
NSString *fullPath =//get path of the file
NSData *fileData = [NSData dataWithContentsOfFile:fullPath];
[composer addAttachmentData:fileData
mimeType:@"text/plain"
fileName:@"File_Name"];
NSString *emailBody = [NSString stringWithString:@"Body"];
[composer setMessageBody:emailBody isHTML:YES];
[self presentModalViewController:composer animated:YES];
[composer release];
答案 1 :(得分:0)
如果您正在使用邮件编辑器视图控制器:
[mailComposerViewController addAttachmentData:fileData mimeType:@"text/plain" fileName:@"name_of_text_file"];
答案 2 :(得分:0)
这是我的应用程序中的一种工作代码。只需跟随即可。 文件(文本文件)位于应用程序的文档目录中 确保已将MessageUIframework添加到项目中 并在您的界面文件
中导入这些内容#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface Your CLass : UIViewController <MFMessageComposeViewControllerDelegate>
<强>代码强>
Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
if ([mailClass canSendMail])
{
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"Your Subject"];
NSFileManager* manager = [[NSFileManager alloc] init];
NSString *documentsDirectory = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
if (![[self NoteFile] isEqualToString:@""]) //check if note is blank
{
NSString *NoteFilePath = [documentsDirectory
stringByAppendingPathComponent:[NSString stringWithFormat:@"%@.txt",[self NoteFileName]]];
NSData *myNoteData = [NSData dataWithContentsOfFile:NoteFilePath];
[picker addAttachmentData:myNoteData mimeType:@"text/plain" fileName:@"Name you want to give your file.txt"];
}
[manager release];
// Fill out the email body text
NSString *emailBody = @"I am attaching my file!";
[picker setMessageBody:emailBody isHTML:NO];
[self presentModalViewController:picker animated:YES];
[picker release];
您可以使用其委托方法
进一步检查邮件状态- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error