Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void sendEMail() {
- Display display = Display.getCurrent();
- Shell shell = new Shell(display);
- OleFrame frame = new OleFrame(shell, SWT.NONE);
- // This should start outlook if it is not running yet
- OleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");
- site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
- // now get the outlook application
- OleClientSite site2 = new OleClientSite(frame, SWT.NONE, "Outlook.Application");
- OleAutomation outlook = new OleAutomation(site2);
- //
- OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */).getAutomation();
- setProperty(mail, "To", "test@gmail.com"); /*
- * Empty but could also be predefined
- */
- setProperty(mail, "Bcc", "test@gmail.com"); /*
- * Empty but could also be predefined
- */
- setProperty(mail, "BodyFormat", 2 /* HTML */);
- setProperty(mail, "Subject", "Top News for you");
- setProperty(mail, "HtmlBody", "<html>Hello<p>, please find some infos here.</html>");
- File file = new File("c:/temp/test.txt");
- if (file.exists()) {
- OleAutomation attachments = getProperty(mail, "Attachments");
- invoke(attachments, "Add", "c:/temp/test.txt");
- }
- else {
- MessageDialog.openInformation(shell, "Info",
- "Attachment File c:/temp/test.txt not found; will send email with attachment");
- }
- invoke(mail, "Display" /* or "Send" */);
- }
- private static OleAutomation getProperty(final OleAutomation auto, final String name) {
- Variant varResult = auto.getProperty(property(auto, name));
- if ((varResult != null) && (varResult.getType() != OLE.VT_EMPTY)) {
- OleAutomation result = varResult.getAutomation();
- varResult.dispose();
- return result;
- }
- return null;
- }
- private static Variant invoke(final OleAutomation auto, final String command, final String value) {
- return auto.invoke(property(auto, command), new Variant[] { new Variant(value) });
- }
- private static Variant invoke(final OleAutomation auto, final String command) {
- return auto.invoke(property(auto, command));
- }
- private static Variant invoke(final OleAutomation auto, final String command, final int value) {
- return auto.invoke(property(auto, command), new Variant[] { new Variant(value) });
- }
- private static boolean setProperty(final OleAutomation auto, final String name, final String value) {
- return auto.setProperty(property(auto, name), new Variant(value));
- }
- private static boolean setProperty(final OleAutomation auto, final String name, final int value) {
- return auto.setProperty(property(auto, name), new Variant(value));
- }
- private static int property(final OleAutomation auto, final String name) {
- return auto.getIDsOfNames(new String[] { name })[0];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement