2015-05-24 106 views
6

我想用PDF附件發送電子郵件。 我創建的PDF文件,然後我這是錯誤的下面筆者認爲:將PDF文件附加到電子郵件中 ​​- Swift

// locate folder containing pdf file    
let documentsPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, NSSearchPathDomainMask.UserDomainMask, true)[0] as! String 

let pdfFileName = documentsPath.stringByAppendingPathComponent("chart.pdf") 
let fileData = NSData(contentsOfFile: pdfFileName) 
mc.addAttachmentData(fileData, mimeType: "pdf", fileName: chart) 

前發送電子郵件,我可以看到連接chart.pdf,但是當我發送的電子郵件,這是沒有附件發送,這是因爲我沒有正確附加文件。

回答

9

您錯過了mimeTypeaddAttachmentData()。使用application/pdf而不是pdf

+0

感謝您的快速回復程序發送

,我更改爲:mc.addAttachmentData(FILEDATA,mime類型:「應用/ pdf「,fileName:pdfFileName),但仍然發送電子郵件沒有附件。 –

+3

你確定'fileData'不是零嗎?而pdf實際上是在這條路上? – NightFury

+1

你是對的,fileData是零,那麼我不得不將fileData = NSData(contentsOfFile:documentsPath)中的「documentsPath」更改爲「pdfFileName」。現在它工作得很好。非常感謝你 –

3

我們可以武官PDF文件的電子郵件,並與雨燕2.2

@IBAction func sendEmail(sender: UIButton) 
    { 
     //Check to see the device can send email. 
     if(MFMailComposeViewController.canSendMail()) 
     { 
      print("Can send email.") 

      let mailComposer = MFMailComposeViewController() 
      mailComposer.mailComposeDelegate = self 

      //Set to recipients 
      mailComposer.setToRecipients(["your email address heres"]) 

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

      //set mail body 
      mailComposer.setMessageBody("This is what they sound like.", isHTML: true) 

      if let filePath = NSBundle.mainBundle().pathForResource("All_about_tax", ofType: "pdf") 
      { 
       print("File path loaded.") 

       if let fileData = NSData(contentsOfFile: filePath) 
       { 
        print("File data loaded.") 
        mailComposer.addAttachmentData(fileData, mimeType: "application/pdf", fileName: "All_about_tax.pdf") 

       } 
      } 

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

    func mailComposeController(controller: MFMailComposeViewController, didFinishWithResult result: MFMailComposeResult, error: NSError?) 
    { 
     self.dismissViewControllerAnimated(true, completion: nil) 
    } 
+0

我有一個錯誤:使用未聲明的類型'MFMailComposeViewController'和'MFMailComposeResult' – coskukoz

+1

你是否已經導入了MessageUI框架? –