How can we add attachment to default mail Applicat

2019-07-04 04:05发布

问题:

I am using NSWorkspace to set TO, CC, BCC , SUBJECT and body text in default mail application through my application. How can i add attachments using NSWrokSpace? Is there any way to attach files in the default mail app using NSWorkSpace? I have tried using NSSharingService but problem with NSSharingService is that you cannot set CC and BCC email addresses in the default application Email Compose. Is there any way to add CC, and BCC recipients using NSSharingSerivce ?? or is there any way to add attachments in mail using NSWorkSpace?? My code to open and set TO, CC and BCC is here

 NSWorkspace.sharedWorkspace().openURL(NSURL.init(string:"mai‌​lto:(self.txtTo.‌​stringValue)"+"?subj‌​ect=YourSubject"+"&c‌​c="+self.txtCC.strin‌​gValue+"&bcc="+self.‌​txtBcc.stringValue)!‌​)

And Using NSSharingService i can set only recipients as TO

 service!.recipients = [self.txtTo.stringValue]
 service!.subject = "Subject"

thanks!.

回答1:

It looks like NSSharingService has the method performWithItems, which takes an array of the things you want to share.

It appears you can create a sharing service of type NSSharingServiceNameComposeEmail and then attach a message body and a file to attach.

I've never used NSSharingService before, so the above is based on some Googling and reading the docs. (And I mostly work in iOS these days so my Mac OS is getting a little stale.)

Check out this link:

http://cutecoder.org/featured/programmatically-sending-rich-text-mail-attachment-mac/