5

I am new to Mac OS X application development, My question is simple, i am able set recipients and body text in default mail application through my application, but i cannot set CC and BCC recipients in the mail application. Is there any way to set CC and BCC through code, i am using Swift. My Code for settings recipients and Body is here

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

Thanks

Shuja
  • 113
  • 7

2 Answers2

5

It's generally much easier to just use a mailto URL for this case - all mail applications support them (and its required as a URL scheme for an app to be registered as an email application).

Here's the schema.

An example would be:

mailto:a@b.com?subject=blah&cc=b@c.com,c@d.com&bcc=d@e.com,e@f.com

You can generate and open this URL using

[[NSWorkspace sharedWorkspace] openURL:[NSURL URLWithString:@"mailto:a@b.com?subject=blah&cc=b@c.com,c@d.com&bcc=d@e.com,e@f.com"]]
Dev Sanghani
  • 1,145
  • 11
  • 19
  • 2
    in swift it will be like this NSWorkspace.sharedWorkspace().openURL(NSURL.init(string:"mailto:\(self.txtTo.stringValue)"+"?subject=YourSubject"+"&cc="+self.txtCC.stringValue+"&bcc="+self.txtBcc.stringValue)!) – Shuja Sep 26 '16 at 11:31
  • I have one more question regarding this, can we add attachments using NSWorkspace? @Dev – Shuja Sep 29 '16 at 07:58
1

I had to tinker with it a bit to get it to work in Swift 5. This is what I came up with:

let url = URL.init(string: "mailto:\a@b.com?subject=\(subject)&cc=b@c.com,c@d.com&bcc=d@e.com,e@f.com")
NSWorkspace.shared.open(url!)
KPDover
  • 31
  • 4