发送带文件附件的邮件

时间:2017-03-10 09:51:05

标签: swift macos swift3

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

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

我看到它可能与MessageUI一起使用,但是我无法导入这个框架我不知道为什么。我收到此错误消息:没有这样的模块' MessageUI' 我检查了一般>链接的框架和库,但没有MessageUI ...

任何人都有解决方案在邮件中添加文件? 感谢

3 个答案:

答案 0 :(得分:7)

似乎attachment网址中的mailto:不支持macOS(并不总是至少......详细信息似乎粗略取决于您在互联网上的位置:))

您可以使用的内容我发现from this blog post,是NSSharingService documented here

的一个实例

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

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

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)

希望对你有所帮助。

答案 1 :(得分:6)

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)
  }
}

答案 2 :(得分:1)

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

示例:

enter image description here

调查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)
    }

工作示例由此link提供。