Search in sources :

Example 1 with Type

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);
    }
}
Also used : Path(java.nio.file.Path) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) EmbeddedObject(org.openntf.domino.EmbeddedObject) Document(org.openntf.domino.Document) Type(org.openntf.domino.email.IEmailAttachment.Type) SessionType(org.openntf.domino.utils.Factory.SessionType) MIMEEntity(org.openntf.domino.MIMEEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) Database(org.openntf.domino.Database) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Stream(org.openntf.domino.Stream) MIMEHeader(org.openntf.domino.MIMEHeader)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 Path (java.nio.file.Path)1 Database (org.openntf.domino.Database)1 Document (org.openntf.domino.Document)1 EmbeddedObject (org.openntf.domino.EmbeddedObject)1 MIMEEntity (org.openntf.domino.MIMEEntity)1 MIMEHeader (org.openntf.domino.MIMEHeader)1 Stream (org.openntf.domino.Stream)1 Type (org.openntf.domino.email.IEmailAttachment.Type)1 SessionType (org.openntf.domino.utils.Factory.SessionType)1