Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2015
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. public static void sendEMail() {
  2. Display display = Display.getCurrent();
  3. Shell shell = new Shell(display);
  4. OleFrame frame = new OleFrame(shell, SWT.NONE);
  5. // This should start outlook if it is not running yet
  6. OleClientSite site = new OleClientSite(frame, SWT.NONE, "OVCtl.OVCtl");
  7. site.doVerb(OLE.OLEIVERB_INPLACEACTIVATE);
  8. // now get the outlook application
  9. OleClientSite site2 = new OleClientSite(frame, SWT.NONE, "Outlook.Application");
  10. OleAutomation outlook = new OleAutomation(site2);
  11. //
  12. OleAutomation mail = invoke(outlook, "CreateItem", 0 /* Mail item */).getAutomation();
  13. setProperty(mail, "To", "test@gmail.com"); /*
  14. * Empty but could also be predefined
  15. */
  16. setProperty(mail, "Bcc", "test@gmail.com"); /*
  17. * Empty but could also be predefined
  18. */
  19. setProperty(mail, "BodyFormat", 2 /* HTML */);
  20. setProperty(mail, "Subject", "Top News for you");
  21. setProperty(mail, "HtmlBody", "<html>Hello<p>, please find some infos here.</html>");
  22. File file = new File("c:/temp/test.txt");
  23. if (file.exists()) {
  24. OleAutomation attachments = getProperty(mail, "Attachments");
  25. invoke(attachments, "Add", "c:/temp/test.txt");
  26. }
  27. else {
  28. MessageDialog.openInformation(shell, "Info",
  29. "Attachment File c:/temp/test.txt not found; will send email with attachment");
  30. }
  31. invoke(mail, "Display" /* or "Send" */);
  32.  
  33. }
  34.  
  35. private static OleAutomation getProperty(final OleAutomation auto, final String name) {
  36. Variant varResult = auto.getProperty(property(auto, name));
  37. if ((varResult != null) && (varResult.getType() != OLE.VT_EMPTY)) {
  38. OleAutomation result = varResult.getAutomation();
  39. varResult.dispose();
  40. return result;
  41. }
  42. return null;
  43. }
  44.  
  45. private static Variant invoke(final OleAutomation auto, final String command, final String value) {
  46. return auto.invoke(property(auto, command), new Variant[] { new Variant(value) });
  47. }
  48.  
  49. private static Variant invoke(final OleAutomation auto, final String command) {
  50. return auto.invoke(property(auto, command));
  51. }
  52.  
  53. private static Variant invoke(final OleAutomation auto, final String command, final int value) {
  54. return auto.invoke(property(auto, command), new Variant[] { new Variant(value) });
  55. }
  56.  
  57. private static boolean setProperty(final OleAutomation auto, final String name, final String value) {
  58. return auto.setProperty(property(auto, name), new Variant(value));
  59. }
  60.  
  61. private static boolean setProperty(final OleAutomation auto, final String name, final int value) {
  62. return auto.setProperty(property(auto, name), new Variant(value));
  63. }
  64.  
  65. private static int property(final OleAutomation auto, final String name) {
  66. return auto.getIDsOfNames(new String[] { name })[0];
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement