xcode 将 .txt 附加到 MFMailComposeViewController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14707645/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:43:30  来源:igfitidea点击:

Attaching .txt to MFMailComposeViewController

iosobjective-cxcodeemail-attachmentsmfmailcomposeviewcontroller

提问by Alex

I have a .txt file stored in Documents Folder and I want to send it by MFMailComposeViewController with next code in the body of -sendEmailmethod:

我有一个 .txt 文件存储在文档文件夹中,我想通过 MFMailComposeViewController 发送它,并在-sendEmail方法主体中使用下一个代码:

NSData *txtData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"dataBase" ofType:@"txt"]];
        [mail addAttachmentData:txtData mimeType:@"text/plain" fileName:[NSString stringWithFormat:@"dataBase.txt"]];

When Mail composer appears I can see attachment in the mail body but I receive this mail without attachment. Maybe it is wrong MIME-type for .txt attachment or something wrong with this code?

当邮件编辑器出现时,我可以在邮件正文中看到附件,但我收到的这封邮件没有附件。也许 .txt 附件的 MIME 类型错误或此代码有问题?

Thanks

谢谢

回答by Rajan Balana

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];        
        NSString *txtFilePath = [documentsDirectory stringByAppendingPathComponent:@"abc.txt"];
NSData *noteData = [NSData dataWithContentsOfFile:txtFilePath];
        MFMailComposeViewController *_mailController = [[MFMailComposeViewController alloc] init];
        [_mailController setSubject:[NSString stringWithFormat:@"ABC"]];
        [_mailController setMessageBody:_messageBody
                                 isHTML:NO];
        [_mailController setMailComposeDelegate:self];
        [_mailController addAttachmentData:noteData mimeType:@"text/plain" fileName:@"abc.txt"];

Hope it helps.

希望能帮助到你。

回答by superm0

In Swift 3, you can send mail with attachment like this

在 Swift 3 中,您可以像这样发送带有附件的邮件

@IBAction func emailLogs(_ sender: Any) {
    let allPaths = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)
    let documentsDirectory = allPaths.first!
    let pathForLog = documentsDirectory.appending("/application.log")

    if MFMailComposeViewController.canSendMail() {
        let mail = MFMailComposeViewController()
        mail.mailComposeDelegate = self;
        mail.setToRecipients(["[email protected]"])
        mail.setSubject("Application Logs")
        mail.setMessageBody("Please see attached", isHTML: true)

        if let fileData = NSData(contentsOfFile: pathForLog) {
            mail.addAttachmentData(fileData as Data, mimeType: "text/txt", fileName: "application.log")
        }

        self.present(mail, animated: true, completion: nil)
    }
}

And then dismiss the composer controller on a result

然后在结果上关闭作曲家控制器

func mailComposeController(_ controller: MFMailComposeViewController, didFinishWith result: MFMailComposeResult, error: Error?) {
    controller.dismiss(animated: true, completion: nil)
}

Make sure to subscribe to this delegate

确保订阅此委托

MFMailComposeViewControllerDelegate