Click here to Skip to main content
15,886,362 members
Articles / Programming Languages / Java / Java SE
Tip/Trick

Save Email With Attachment in Outlook's Draft Folder

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
3 Jun 2016CPOL 17.3K   170   1  
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)


Written By
Architect
United States United States
A Solutions Architect with more than fourteen years of experience in application development. I mostly work in .NET, Angular, MEAN stack technologies and love to share what I do and learn during my day to day job. Please check my tutorials and blog:
https://fullstackhub.io
https://fullstackhubblog.com

Comments and Discussions

 
-- There are no messages in this forum --