How to add file attachments to email
Adding file attachments to email
Here is a code snippet which adds file attachments to emails.
For this we use the EmailFileAttachement object and add it to the EmailOptions when sending.
EmailTemplate emailTemplate = context.getObject( EmailTemplate.class, "some email template" );
EmailOptions emailOptions = new EmailOptions();
// This fictional method would return a String to
// set the fileName of the attachment.
String fileName = getFileName();
// This fictional method would return a byte[] to
// set the data of the attachment.
byte[] fileData = generateCsvFile();
EmailFileAttachment attachment = new EmailFileAttachment( fileName, EmailFileAttachment.MimeType.MIME_CSV, fileData );
emailOptions.addAttachment( attachment );
context.sendEmailNotification( emailTemplate, emailOptions );
Note: This was written for IdentityIQ 6.2. Other versions may vary.
Supported MimeTypes
EmailFileAttachement MimeType |
Standard MIME Type |
---|---|
MIME_CSV | text/csv |
MIME_JAR | application/java-archive |
MIME_HTML | text/html |
MIME_OCTET | application/octet-stream |
MIME_PDF | application/pdf |
MIME_PLAIN | text/plain |
MIME_PS | application/postscript |
MIME_RICH | text/richtext |
MIME_WORD | application/msword |
MIME_ZIP | application/zip |
For more information, see:
Internet Assigned Numbers Authority - Wikipedia, the free encyclopedia
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
Any good examples of how to convert the data to byte[] ? I'm hoping to do this without having to add on any third-party libraries.
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
I found a working solution to my own question:
import java.io.File;
import java.io.FileInputStream;
public static byte[] readFiletoByteArray(File file)
{
FileInputStream fileInputStream = null;
byte[] byteFile = new byte[(int) file.length()];
try
{
fileInputStream = new FileInputStream(file);
fileInputStream.read(byteFile);
fileInputStream.close();
for (int i = 0; i < byteFile.length; i++)
{
System.out.print((char) byteFile[i]);
}
}
catch (Exception e)
{
e.printStackTrace();
}
return byteFile;
}
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
Why oh why are image Mime types not supported??
Would be super handy for embedding images in email :smileyhappy:
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
I have a custom workflow successfully generating PDF files as attachments. However, when I try to open them with Adobe Reader, I get a message to the effect that the file cannot be opened "because it is either not a supported file type or it has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded)." They clearly show as a PDF but won't open with Adobe Reader. However, they do open as text files in NotePad++.
This is the code I am using:
EmailTemplate emailTemplate = context.getObject( EmailTemplate.class, "EmailTemplateName" );
EmailOptions emailOptions = new EmailOptions();
String fileName = "FileName" + ".pdf";
String fileString = "Data goes here.";
byte[] fileData = fileString.getBytes("UTF-8"); //Does this need to be a different charset?
EmailFileAttachment attachment = new EmailFileAttachment( fileName, EmailFileAttachment.MimeType.MIME_PDF, fileData );
emailOptions.addAttachment( attachment );
context.sendEmailNotification( emailTemplate, emailOptions );
I tried changing the code to specify CSVs and TXT files, and they worked without any problems. I have tried various other charsets in the getBytes() method without any luck either. Any thoughts on additional steps to get the PDFs to open properly in Adobe Reader?
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
In place of
byte[] fileData = generateCsvFile();
this should work:
byte[] fileData = java.nio.file.Files.readAllBytes(file.toPath())
or
byte[] fileData = java.nio.file.Files.readAllBytes(java.nio.file.Paths.get(filePathStr));
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
This was very useful. I was able to use the example to add attachments in my email notification. For the namin gof the attachment, I am currently renaming based on the lastName, firstName variables. Is there a way to automatically add a new character to each attachment? For example, I am sending 3 attachments in the email. I would like the attachments to be renamed and show up in the email as:
lastName, firstName_1.pdf
lastName, firstName_2.pdf
lastName, firstName_3.pdf
- Mark as Read
- Mark as New
- Bookmark
- Permalink
- Report Content to Moderator
Hi Experts,
Currently I have a certification campaign running and it will send an email reminder to the manager every 7 days using default email templete by SailPoint "Certification Reminder". I am trying to enhance the email template so the email can send with a PDF attachment. The PDF attachment is an existing PDF in SailPoint server. Any good example to achieve this?