use of org.openntf.domino.MIMEHeader in project org.openntf.domino by OpenNTF.
the class DominoEmail method setSender.
/**
* Sets the sender details into the email, using senderName and senderEmail
*
* @param mimeRoot
* MIMEEntity root of the email, into which the sender details will be added as a child
* @since org.openntf.domino 4.5.0
*/
private void setSender(final MIMEEntity mimeRoot) {
if (StringUtil.isEmpty(getSenderEmail())) {
return;
}
MIMEHeader mimeHeader = null;
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("Reply-To");
mimeHeader.setHeaderVal(getSenderEmail());
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("Return-Path");
mimeHeader.setHeaderVal(getSenderEmail());
if (StringUtil.isEmpty(getSenderName())) {
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("From");
mimeHeader.setHeaderVal(getSenderEmail());
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("Sender");
mimeHeader.setHeaderVal(getSenderEmail());
} else {
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("From");
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
mimeHeader.addValText("\"" + getSenderName() + "\" <" + getSenderEmail() + ">", "UTF-8");
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("Sender");
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$
mimeHeader.addValText("\"" + getSenderName() + "\" <" + getSenderEmail() + ">", "UTF-8");
}
}
use of org.openntf.domino.MIMEHeader in project org.openntf.domino by OpenNTF.
the class DominoEmail method send.
/* (non-Javadoc)
* @see org.openntf.domino.email.IEmail#send()
*/
@Override
public Document send() {
try {
Stream stream;
MIMEEntity mimeEntity;
MIMEHeader mimeHeader;
Database currDb;
Session currSess = getSession();
// in case Khan is still in suspended animation!
currSess.setConvertMime(false);
// Create memo doc
try {
currDb = currSess.getCurrentDatabase();
if (null == currDb) {
// Will this work if we're running from DOTS or OSGi plugin??
// $NON-NLS-1$
currDb = currSess.getDatabase(currSess.getServerName(), "mail.box");
}
} catch (Throwable t) {
// $NON-NLS-1$
currDb = currSess.getDatabase(currSess.getServerName(), "mail.box");
}
Document memo = currDb.createDocument();
// no replies from out of office agents //$NON-NLS-1$ //$NON-NLS-2$
memo.put("RecNoOutOfOffice", "1");
String mimeBoundary = memo.getUniversalID().toLowerCase();
// $NON-NLS-1$
MIMEEntity mimeRoot = memo.createMIMEEntity("Body");
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("To");
// $NON-NLS-1$
mimeHeader.setHeaderVal(join(getTo(), ""));
if (cc_.size() > 0) {
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("CC");
// $NON-NLS-1$
mimeHeader.setHeaderVal(join(getCC(), ""));
// memo.replaceItemValue("cc", getCC());
}
if (bcc_.size() > 0) {
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("BCC");
// $NON-NLS-1$
mimeHeader.setHeaderVal(join(getBCC(), ""));
// memo.replaceItemValue("bcc", getBCC());
}
// set subject
// $NON-NLS-1$
mimeHeader = mimeRoot.createHeader("Subject");
// $NON-NLS-1$
mimeHeader.addValText(getSubject(), "UTF-8");
// create text/alternative directive: text/plain and text/html part will be childs of this entity
MIMEEntity mimeRootChild = mimeRoot.createChildEntity();
// $NON-NLS-1$
mimeHeader = mimeRootChild.createHeader("Content-Type");
// $NON-NLS-1$ //$NON-NLS-2$
mimeHeader.setHeaderVal("multipart/alternative; boundary=\"" + mimeBoundary + "\"");
// create plain text part
if (getText().size() > 0) {
mimeEntity = mimeRootChild.createChildEntity();
stream = currSess.createStream();
// $NON-NLS-1$
stream.writeText(join(getText(), System.getProperty("line.separator")));
// $NON-NLS-1$
mimeEntity.setContentFromText(stream, "text/plain; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
stream.close();
}
// create HTML part
if (contentsHTML_.size() > 0) {
mimeEntity = mimeRootChild.createChildEntity();
stream = currSess.createStream();
// $NON-NLS-1$
stream.writeText(join(contentsHTML_, System.getProperty("line.separator")));
// $NON-NLS-1$
mimeEntity.setContentFromText(stream, "text/html; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
stream.close();
}
// create embedded JSON part
if (!StringUtil.isEmpty(getJSON())) {
mimeEntity = mimeRootChild.createChildEntity();
stream = currSess.createStream();
// $NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
String json = "{\"url\" : \"" + getJSON() + "\"}" + System.getProperty("line.separator");
stream.writeText(json);
// $NON-NLS-1$
mimeEntity.setContentFromText(stream, "application/embed+json; charset=\"UTF-8\"", MIMEEntity.ENC_NONE);
stream.close();
}
// Add any attachments
addAttachments(mimeRoot);
// set the sender
setSender(mimeRoot);
// mime processing done.
memo.closeMIMEEntities(true);
// send the e-mail
memo.send();
return memo;
} catch (Throwable t) {
DominoUtils.handleException(t);
return null;
}
}
use of org.openntf.domino.MIMEHeader in project org.openntf.domino by OpenNTF.
the class Item method getTypeEx.
@Override
public Type getTypeEx() {
try {
if (itemType == null) {
itemType = Type.valueOf(getDelegate().getType());
}
} catch (NotesException e) {
DominoUtils.handleException(e, this);
}
if (itemType == Type.MIME_PART) {
MIMEEntity entity = getMIMEEntity();
if (entity != null) {
// $NON-NLS-1$
MIMEHeader contentType = entity.getNthHeader("Content-Type");
if (contentType != null) {
String headerval = contentType.getHeaderVal();
if (// $NON-NLS-1$
"application/x-java-serialized-object".equals(headerval) || "application/x-java-externalized-object".equals(headerval)) {
// $NON-NLS-1$
itemType = Type.MIME_BEAN;
}
}
parent.closeMIMEEntities(false, getName());
}
}
return itemType;
}
use of org.openntf.domino.MIMEHeader 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