簡體   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