Adding Attachments to a New Email Message using Aspose.Email for Java
[Java]
//Create an instance of MailMessage class
MailMessage message = new MailMessage();
//From
message.setFrom(new MailAddress("[email protected]"));
//to whom
message.getTo().add(new MailAddress("[email protected]"));
//Adding 1st attachment
//Create an instance of Attachment class
Attachment attachment;
//Load an attachment
attachment = new Attachment("1.txt");
//Add attachment in instance of MailMessage class
message.getAttachments().add(attachment);
//Add 2nd Attachment
message.getAttachments().add(new Attachment("1.jpg"));
//Add 3rd Attachment
message.getAttachments().add(new Attachment("1.doc"));
//Add 4th Attachment
message.getAttachments().add(new Attachment("1.rar"));
//Add 5th Attachment
message.getAttachments().add(new Attachment("1.pdf"));
//Save message to disc
message.save("output.msg",MessageFormat.getMsg());
Extract Attachments from an existing Email Message
public static void main(String[] args)
{
// Base folder for reading and writing files
String strBaseFolder = "D:\\Data\\Aspose\\resources\\";
//Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage msg = MailMessage.load(strBaseFolder + "AnEmail.eml", MessageFormat.getEml());
//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = msg.getAttachments();
//Iterate over the AttachmentCollection
for(int index = 0; index < attachments.size(); index++)
{
//Initialize Attachment object and Get the indexed Attachment reference
Attachment attachment = (Attachment) attachments.get(index);
//Display Attachment Name
System.out.println(attachment.getName());
//Save Attachment to disk
attachment.save(strBaseFolder + "attachment_"+ attachment.getName());
}
}
Add or Remove Attachments from an existing Email Message
public static void main(String[] args)
{
// Base folder for reading and writing files
String strBaseFolder = "D:\\Data\\Aspose\\resources\\";
//Initialize and Load an existing EML file by specifying the MessageFormat
MailMessage message = MailMessage.load(strBaseFolder + "AnEmail.eml", MessageFormat.getEml());
//Initialize AttachmentCollection object with MailMessage Attachments
AttachmentCollection attachments = message.getAttachments();
System.out.println("Attachment Count: " + attachments.size());
//Check if AttachmentCollection size is greater than 0
if(attachments.size() > 0)
{
//Remove Attachment from index location 0
attachments.remove(0);
System.out.println("Attachment Count: " + attachments.size());
}
//Add a PDF file as Attachment to the message
message.addAttachment(new Attachment(strBaseFolder + "Blank.PDF"));
System.out.println("Attachment Count: " + attachments.size());
//Save the Email message to disk by specifying the EML MailMessageSaveType
message.save(strBaseFolder + "message.eml", MailMessageSaveType.getEmlFormat());
Overview: Aspose.Email for Java
Aspose.Email for Java is a Non-Graphical Java component that enables Java applications to read and write MS Outlook MSG files from within a Java application without using MS Outlook. It enables developers to create new MSG file from scratch, update an existing MSG file, read Outlook MSG file & get its properties like subject, body, recipients in to, cc and bcc, adding or removing attachment, sender information & MAPI properties. Aspose.Email can be used with Web as well as Desktop Application.
More about Aspose.Email for Java
Contact Information
// close InputStream object
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
// close Document object
pdfDocument.dispose();
Overview: Aspose.Pdf for Java
Aspose.Pdf is a Java PDF component to create PDF documents without using Adobe Acrobat. It supports Floating box, PDF form field, PDF attachments, security, Foot note & end note, Multiple columns document, Table of Contents, List of Tables, Nested tables, Rich text format, images, hyperlinks, JavaScript, annotation, bookmarks, headers, footers and many more. Now you can create PDF by API, XML and XSL-FO files. It also enables you to converting HTML, XSL-FO and Excel files into PDF.
More about Aspose.Pdf for Java