Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java / JavaSE

Save Email With Attachment in Outlook's Draft Folder

5.00/5 (2 votes)
3 Jun 2016CPOL 17.6K   170  
This small tip will help you to save email with attachment in Outlook's draft folder using Java programming language.

Introduction

In this small tip, I will explain how we can save emails with attachment in Outlook's draft folder using Java programming language.

Using the Code

I am using Eclipse to write this small application, following are the packages we need for this application:

Java
import java.util.*;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.*;
import javax.mail.internet.*;

In the main function, you can specify MailFrom, SMTP host and MailTo in the following variables:

Java
String to =   "emailto@yourcompany.com";
String from = "emailfrom@yourcompany.com";
String host = "yourcompanyhoust.com";

Next, we need to create Mail properites, e.g., SMTP host and store in session variable:

Java
Properties properties = System.getProperties();

properties.setProperty("mail.smtp.host", host);

 Session session = Session.getDefaultInstance(properties);

After that, following is a simple self-explanatory code to create new email message, add the message subject, load the attachment from local drive and attach it with email:

Java
Message message = new MimeMessage(session);

            message.addRecipient(
                Message.RecipientType.TO,
                new InternetAddress(to));

            message.setSubject("This is the Subject Line!");

            BodyPart messageBodyPart = new MimeBodyPart();

            messageBodyPart.setText("This is message body");

            // Create a multipar message
            Multipart multipart = new MimeMultipart();

            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment
            messageBodyPart = new MimeBodyPart();

            //Add the attachment file path
            String filename = "C:/temp/bb.log";
            FileDataSource source = new FileDataSource(filename);
            messageBodyPart.setDataHandler(new DataHandler(source));
            messageBodyPart.setFileName(filename);
            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            message.setContent(multipart);

Next, we need to add IMAP host properties and specify the IMAP host along port 993:

Java
properties.setProperty("mail.store.protocol", "imaps");
properties.setProperty("mail.imaps.port", "993");

session = Session.getDefaultInstance(properties, null);

 javax.mail.Store store = session.getStore("imaps");

// I need to add my system login and password to access IMAP host, you can check with your admin

store.connect("outlook.your_org.com", "XXXXX", "XXXXXX");

Once we are able to connect to store, we can easily browse through all Outlook folders, e.g., Inbox, Sent Items, Drafts, etc. In the following code, we are accessing the Drafts folder, opening it with Read & Write access and finally appending the above created message.

Java
Folder draft = store.getFolder("drafts");

draft.open(Folder.READ_WRITE);

draft.appendMessages(new Message[] { message });
System.out.println(
        draft.getFullName()
        + " has : "
        + draft.getMessageCount()
        + " Message(s)");

After executing the above application, you should see a new message in your Outlook draft folder.

History

  • 3/11/2016: Initial post

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)