Open mail from a java program and attach a file to mail from a directory

I need to implement email functions in my Java application, which will open the microsoft look and add a file from my directory. Is everything implemented?

+4
source share
5 answers

According to these documents you need a team

"path/to/Outlook.exe /c ipm.note /a \"path/to/attachment\"" 

Collect it and run it through ProcessBuilder

(Or listen to MarcoS, which gives a very good example of why sometimes it’s better not to literally answer questions :-))

+8
source

You can open the system mail client using the class.

 Desktop.getDesktop().mail( new URI( "mailto: address@somewhere.com " ) ) 
+5
source

If you want to implement email features in Java, consider JavaMail . In addition, if your application has email functions, you do not need to open another email client (for example, Outlook).

+4
source

I was able to open MS Outlook 2007 using HTML email. I did this using the SWT OLE API. Here's a Vogela tutorial: http://www.vogella.com/articles/EclipseMicrosoftIntegration/article.html

The tutorial says that it also works for non-RCP Java.

 public void sendEMail() { OleFrame frame = new OleFrame(getShell(), 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, "BodyFormat", 2 /* HTML */); setProperty(mail, "Subject", subject); setProperty(mail, "HtmlBody", content); if (null != attachmentPaths) { for (String attachmentPath : attachmentPaths) { File file = new File(attachmentPath); if (file.exists()) { OleAutomation attachments = getProperty(mail, "Attachments"); invoke(attachments, "Add", attachmentPath); } } } invoke(mail, "Display" /* or "Send" */); } 
+2
source

Here is the exact command you want: -

 new ProcessBuilder("C:\\Program Files\\Microsoft Office\\Office14\\OUTLOOK.exe","/a","C:\\Desktop\\stackoverflow.txt").start(); 

The first argument is the path to Outlook.

The second command is Argument-Outlook.

The third argument is the binding path.

+2
source

All Articles