Launch “new email” window on Mac OS X with c++

2019-09-14 18:38发布

问题:

Can I trigger my Mac to open the default mail client "new message" window from my C++ program. It should work just like a mailto link does. When the program runs it should open a "new message" window with a message body, a subject line, and a recipient already filled in. I DON'T need to include an attachment. I know there are some answers already on Stack Overflow addressing that question. A mailto link with the functionality I need looks something like this:

    "mailto:bob@domain.com?subject=look at this website&body=Hi,I found this website."

If it is not possible to use mailto directly in C++, is there some other way of doing what I am looking for?

Thank you for your help!

回答1:

This simple code does what I need! It launches the default mail client window with a given recipient address, subject, and message body. "email", "subject", and "bodyMessage" are all string variables declared in my complete program code. This snippet doesn't include their declarations.

       string mailTo = "mailto:" + email + "?subject=" + subject + "\\&body=" + bodyMessage;
       string command = "open " + mailTo;
       system(command.c_str());

This post on using the "open" command helped.