繁体   English   中英

发送带文件附件的邮件

[英]Send mail with file attachment

我搜索解决方案发送带附件的邮件。 我有这个代码,但文件没有附加...

if let url = URL(string: "mailto:\(email)?subject=report&body=see_attachment&attachment=/Users/myname/Desktop/report.txt") {
    NSWorkspace.shared().open(url)
}

我看到它可能与MessageUI一起工作,但我无法导入这个框架,我不知道为什么。 我收到此错误消息:没有这样的模块'MessageUI'我在General> Linked Frameworks and Libraries中检查过,但是没有MessageUI ...

有人有解决方案在邮件中添加文件吗? 谢谢

似乎mailto:不支持mailto: URL中的attachment (并不总是至少......细节似乎粗略取决于你在互联网上的位置:))

您可以使用我从这篇博文NSSharingService 内容,这里NSSharingService一个实例

是一个演示如何使用它的示例。

在你的情况下,你可以做类似的事情:

let email = "your email here"
let path = "/Users/myname/Desktop/report.txt"
let fileURL = URL(fileURLWithPath: path)

let sharingService = NSSharingService(named: NSSharingServiceNameComposeEmail)
sharingService?.recipients = [email] //could be more than one
sharingService?.subject = "subject"
let items: [Any] = ["see attachment", fileURL] //the interesting part, here you add body text as well as URL for the document you'd like to share

sharingService?.perform(withItems: items)

更新

所以@Spire在下面的评论中提到,这不会附加文件。

似乎有一点需要注意。

为此,您需要查看您的应用程序功能。

你可以:

  • 禁用App Sandbox
  • 为您要获取内容的文件夹启用读取访问权限。

我附上了几张截图。

如果我在功能下禁用了App Sandbox,这就是这个看法

App Sandbox已停用

这是一个图像,我已启用App Sandbox并允许我的应用程序读取我的Downloads文件夹中的内容

应用沙盒已启用

如果我执行上述操作,我可以使用此URL访问位于“我的下载”文件夹中的名为document.txt文件

let path = "/Users/thatsme/Downloads/document.txt"
let fileURL = URL(fileURLWithPath: path)

并将其附在邮件上

希望对你有所帮助。

import MessageUI
class ViewController: UIViewController,MFMailComposeViewControllerDelegate {

func sendMail() {
  if( MFMailComposeViewController.canSendMail()){
        print("Can send email.")

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        //Set to recipients
        mailComposer.setToRecipients(["yakupad@yandex.com"])

        //Set the subject
        mailComposer.setSubject("email with document pdf")

        //set mail body
        mailComposer.setMessageBody("This is what they sound like.", isHTML: true)
        let pathPDF = "\(NSTemporaryDirectory())contract.pdf"
            if let fileData = NSData(contentsOfFile: pathPDF) 
            {
                print("File data loaded.")
                mailComposer.addAttachmentData(fileData as Data, mimeType: "application/pdf", fileName: "contract.pdf")
            }

        //this will compose and present mail to user
        self.present(mailComposer, animated: true, completion: nil)
    }
    else
    {
        print("email is not supported")
    }

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

首先,您应该导入import MessageUI 为此项目添加框架。

例:

在此输入图像描述

调查MFMailComposeViewControllerDelegate以了解何时结束发送电子邮件。

创建电子邮件的示例:

if( MFMailComposeViewController.canSendMail() ) {
        println("Can send email.")

        let mailComposer = MFMailComposeViewController()
        mailComposer.mailComposeDelegate = self

        //Set the subject and message of the email
        mailComposer.setSubject("Have you heard a swift?")
        mailComposer.setMessageBody("This is what they sound like.", isHTML: false)

        if let filePath = NSBundle.mainBundle().pathForResource("swifts", ofType: "wav") {
            println("File path loaded.")

            if let fileData = NSData(contentsOfFile: filePath) {
                println("File data loaded.")
                mailComposer.addAttachmentData(fileData, mimeType: "audio/wav", fileName: "swifts")
            }
        }
        self.presentViewController(mailComposer, animated: true, completion: nil)
    }

链接提供了工作示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM