use of org.openntf.domino.email.IEmailAttachment.Type in project org.openntf.domino by OpenNTF.
the class DominoEmail method addAttachments.
/* (non-Javadoc)
* @see org.openntf.domino.email.IEmail#addAttachments(java.util.ArrayList, org.openntf.domino.MIMEEntity)
*/
@Override
public void addAttachments(final MIMEEntity parent) {
try {
Stream streamFile = null;
for (IEmailAttachment attach : getAttachments()) {
InputStream is = null;
EmbeddedObject eo = null;
String fileName = attach.getFileName();
// Get content type
String contentType = URLConnection.guessContentTypeFromName(fileName);
if (null == contentType) {
// $NON-NLS-1$
contentType = "application/octet-stream";
}
// $NON-NLS-1$
int idex = StringUtil.indexOfIgnoreCase(fileName, ".", fileName.length() - 6);
if (idex > -1) {
String extension = fileName.substring(idex);
if (StringUtil.equals("gif", extension)) {
// $NON-NLS-1$
// $NON-NLS-1$
contentType = "image/gif";
} else if (StringUtil.equals("jpg", extension) || StringUtil.equals("jpeg", extension)) {
// $NON-NLS-1$ //$NON-NLS-2$
// $NON-NLS-1$
contentType = "image/jpeg";
} else if (StringUtil.equals("png", extension)) {
// $NON-NLS-1$
// $NON-NLS-1$
contentType = "image/png";
}
}
// $NON-NLS-1$ //$NON-NLS-2$
contentType += "; name=\"" + fileName + "\"";
try {
Type attachmentType = attach.getAttachmentType();
if (attachmentType == Type.DOCUMENT) {
// retrieve the document containing the attachment to send from the relevant
Database dbFile = getSession().getDatabase(getSession().getServerName(), attach.getDbPath());
Document docFile = dbFile.getDocumentByUNID(attach.getUnid());
if (null != docFile) {
eo = docFile.getAttachment(attach.getFileName());
is = eo.getInputStream();
}
} else if (attachmentType == Type.FILE) {
Path path = Paths.get(attach.getPath()).resolve(attach.getFileName());
is = Files.newInputStream(path);
} else if (attachmentType == Type.STREAM) {
is = attach.getInputStream();
} else {
is = new ByteArrayInputStream(attach.getBytes());
}
if (null != is) {
MIMEEntity mimeChild = parent.createChildEntity();
// $NON-NLS-1$
MIMEHeader mimeHeader = mimeChild.createHeader("Content-Disposition");
if (attach.isInlineImage()) {
// $NON-NLS-1$ //$NON-NLS-2$
mimeHeader.setHeaderVal("inline; filename=\"" + attach.getFileName() + "\"");
} else {
// $NON-NLS-1$ //$NON-NLS-2$
mimeHeader.setHeaderVal("attachment; filename=\"" + attach.getFileName() + "\"");
}
// $NON-NLS-1$
mimeHeader = mimeChild.createHeader("Content-ID");
// $NON-NLS-1$ //$NON-NLS-2$
mimeHeader.setHeaderVal("<" + attach.getContentId() + ">");
streamFile = getSession().createStream();
streamFile.setContents(is);
mimeChild.setContentFromBytes(streamFile, contentType, MIMEEntity.ENC_IDENTITY_BINARY);
streamFile.close();
}
} catch (Exception e) {
DominoUtils.handleException(e);
} finally {
if (null != is) {
is.close();
}
}
}
} catch (Throwable t) {
DominoUtils.handleException(t);
}
}
Aggregations