/** * Creates EML string from given {@link Email}. * * @param email {@link Email} from which to create EML {@link String}. * @return {@link String} with EML content. */ public String compose(final Email email) { if (getSession() == null) { createSession(getProperties()); } final OutputStreamTransport ost = new OutputStreamTransport(getSession()); final SendMailSession sendMailSession = new SendMailSession(getSession(), ost); sendMailSession.sendMail(email); return ost.getEml(); }
private Message createMessage(final Email email) throws MessagingException { final SendMailSession testSendMailSession = new SendMailSession(null, null); return testSendMailSession.createMessage(email); }
/** * Adds message data and attachments. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void addBodyData(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { final List<EmailMessage> messages = emailWithData.messages(); final int totalMessages = messages.size(); // Need to use new list since filterEmbeddedAttachments(List) removes attachments from the source List final List<EmailAttachment<? extends DataSource>> attachments = new ArrayList<>(emailWithData.attachments()); if (attachments.isEmpty() && totalMessages == 1) { // special case: no attachments and just one content setContent(messages.get(0), msgToSet); } else { final MimeMultipart multipart = new MimeMultipart(); final MimeMultipart msgMultipart = new MimeMultipart(ALTERNATIVE); multipart.addBodyPart(getBaseBodyPart(msgMultipart)); for (final EmailMessage emailMessage : messages) { msgMultipart.addBodyPart(getBodyPart(emailMessage, attachments)); } addAnyAttachments(attachments, multipart); msgToSet.setContent(multipart); } }
/** * Creates new {@link MimeMessage} from an {@link Email}. * * @param email {@link Email} to be created as a {@link MimeMessage}. * @return {@link MimeMessage} created from an {@link Email}. * @throws MessagingException if there is a failure */ protected MimeMessage createMessage(final Email email) throws MessagingException { final Email clone = email.clone(); final MimeMessage newMsg = new MimeMessage(getSession()); setPeople(clone, newMsg); setSubject(clone, newMsg); setSentDate(clone, newMsg); setHeaders(clone, newMsg); addBodyData(clone, newMsg); return newMsg; }
/** * @param emailMessage {@link EmailMessage} with data. * @param attachments {@link List} of {@link EmailAttachment}s. * @return new {@link MimeBodyPart} with data from emailMessage and attachments. * @throws MessagingException if there is a failure. */ private MimeBodyPart getBodyPart(final EmailMessage emailMessage, final List<EmailAttachment<? extends DataSource>> attachments) throws MessagingException { final MimeBodyPart bodyPart = new MimeBodyPart(); // detect embedded attachments final List<EmailAttachment<? extends DataSource>> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage); if (embeddedAttachments.isEmpty()) { // no embedded attachments, just add message setContent(emailMessage, bodyPart); } else { attachments.removeAll(embeddedAttachments); // embedded attachments detected, join them as related final MimeMultipart relatedMultipart = new MimeMultipart(RELATED); final MimeBodyPart messageData = new MimeBodyPart(); setContent(emailMessage, messageData); relatedMultipart.addBodyPart(messageData); addAnyAttachments(embeddedAttachments, relatedMultipart); bodyPart.setContent(relatedMultipart); } return bodyPart; }
@Test void testSendEmailWithVariousAttachaments() { final SmtpServer smtpServer = MailServer.create() .host(HOST) .auth(USERNAME, PASSWORD) .ssl(true) .buildSmtpMailServer(); final SendMailSession session = smtpServer.createSession(); session.open(); final Email email = Email.create() .from("info@jodd.org") .to("igor.spasic@gmail.com") .subject("test-gmail") .textMessage("Hello!") .htmlMessage( "<html><META http-equiv=Content-Type content=\"text/html; charset=utf-8\">" + "<body><h1>Hey!</h1><img src='cid:jodd.png'>" + "<h2>Hay!</h2><img src='cid:jodd2.png'>" + "<h3></h3></body></html>") .embeddedAttachment(EmailAttachment.with().content(new File(PNG)).inline(false)) .embeddedAttachment(EmailAttachment.with().content(new File(PNG)).contentId("jodd2.png").inline(true)) .attachment(EmailAttachment.with().content(PNG)); session.sendMail(email); session.close(); } }
/** * Prepares message and sends it. Returns Message ID of sent email. * * @param email {@link Email} to send. * @return String representing message ID. */ public String sendMail(final Email email) { try { final MimeMessage msg = createMessage(email); getService().sendMessage(msg, msg.getAllRecipients()); return msg.getMessageID(); } catch (final MessagingException msgexc) { throw new MailException("Failed to send email: " + email, msgexc); } }
/** * {@inheritDoc} * * @return {@link SendMailSession} */ @Override public SendMailSession createSession() { final Session session = Session.getInstance(createSessionProperties(), authenticator); final Transport mailTransport; try { mailTransport = getTransport(session); } catch (final NoSuchProviderException nspex) { throw new MailException(nspex); } return new SendMailSession(session, mailTransport); }
/** * Adds {@link List} of {@link EmailAttachment}s to multipart. * * @param attachments {@link List} of {@link EmailAttachment}s to add to multipart. This can be {@code null}. * @param multipart {@link MimeMultipart} to set data into. * @throws MessagingException if there is a failure. */ private void addAnyAttachments(final List<EmailAttachment<? extends DataSource>> attachments, final MimeMultipart multipart) throws MessagingException { for (final EmailAttachment<? extends DataSource> attachment : attachments) { final MimeBodyPart bodyPart = createAttachmentBodyPart(attachment); multipart.addBodyPart(bodyPart); } }
/** * Creates new {@link MimeMessage} from an {@link Email}. * * @param email {@link Email} to be created as a {@link MimeMessage}. * @return {@link MimeMessage} created from an {@link Email}. * @throws MessagingException if there is a failure */ protected MimeMessage createMessage(final Email email) throws MessagingException { final Email clone = email.clone(); final MimeMessage newMsg = new MimeMessage(getSession()); setPeople(clone, newMsg); setSubject(clone, newMsg); setSentDate(clone, newMsg); setHeaders(clone, newMsg); addBodyData(clone, newMsg); return newMsg; }
/** * @param emailMessage {@link EmailMessage} with data. * @param attachments {@link List} of {@link EmailAttachment}s. * @return new {@link MimeBodyPart} with data from emailMessage and attachments. * @throws MessagingException if there is a failure. */ private MimeBodyPart getBodyPart(final EmailMessage emailMessage, final List<EmailAttachment<? extends DataSource>> attachments) throws MessagingException { final MimeBodyPart bodyPart = new MimeBodyPart(); // detect embedded attachments final List<EmailAttachment<? extends DataSource>> embeddedAttachments = filterEmbeddedAttachments(attachments, emailMessage); if (embeddedAttachments.isEmpty()) { // no embedded attachments, just add message setContent(emailMessage, bodyPart); } else { attachments.removeAll(embeddedAttachments); // embedded attachments detected, join them as related final MimeMultipart relatedMultipart = new MimeMultipart(RELATED); final MimeBodyPart messageData = new MimeBodyPart(); setContent(emailMessage, messageData); relatedMultipart.addBodyPart(messageData); addAnyAttachments(embeddedAttachments, relatedMultipart); bodyPart.setContent(relatedMultipart); } return bodyPart; }
/** * Prepares message and sends it. Returns Message ID of sent email. * * @param email {@link Email} to send. * @return String representing message ID. */ public String sendMail(final Email email) { try { final MimeMessage msg = createMessage(email); getService().sendMessage(msg, msg.getAllRecipients()); return msg.getMessageID(); } catch (final MessagingException msgexc) { throw new MailException("Failed to send email: " + email, msgexc); } }
/** * {@inheritDoc} * * @return {@link SendMailSession} */ @Override public SendMailSession createSession() { final Session session = Session.getInstance(createSessionProperties(), authenticator); final Transport mailTransport; try { mailTransport = getTransport(session); } catch (final NoSuchProviderException nspex) { throw new MailException(nspex); } return new SendMailSession(session, mailTransport); }
/** * Adds {@link List} of {@link EmailAttachment}s to multipart. * * @param attachments {@link List} of {@link EmailAttachment}s to add to multipart. This can be {@code null}. * @param multipart {@link MimeMultipart} to set data into. * @throws MessagingException if there is a failure. */ private void addAnyAttachments(final List<EmailAttachment<? extends DataSource>> attachments, final MimeMultipart multipart) throws MessagingException { for (final EmailAttachment<? extends DataSource> attachment : attachments) { final MimeBodyPart bodyPart = createAttachmentBodyPart(attachment); multipart.addBodyPart(bodyPart); } }
/** * Adds message data and attachments. * * @param emailWithData {@link Email} with data * @param msgToSet {@link MimeMessage} to set data into. * @throws MessagingException if there is a failure. */ private void addBodyData(final Email emailWithData, final MimeMessage msgToSet) throws MessagingException { final List<EmailMessage> messages = emailWithData.messages(); final int totalMessages = messages.size(); // Need to use new list since filterEmbeddedAttachments(List) removes attachments from the source List final List<EmailAttachment<? extends DataSource>> attachments = new ArrayList<>(emailWithData.attachments()); if (attachments.isEmpty() && totalMessages == 1) { // special case: no attachments and just one content setContent(messages.get(0), msgToSet); } else { final MimeMultipart multipart = new MimeMultipart(); final MimeMultipart msgMultipart = new MimeMultipart(ALTERNATIVE); multipart.addBodyPart(getBaseBodyPart(msgMultipart)); for (final EmailMessage emailMessage : messages) { msgMultipart.addBodyPart(getBodyPart(emailMessage, attachments)); } addAnyAttachments(attachments, multipart); msgToSet.setContent(multipart); } }
/** * Creates EML string from given {@link Email}. * * @param email {@link Email} from which to create EML {@link String}. * @return {@link String} with EML content. */ public String compose(final Email email) { if (getSession() == null) { createSession(getProperties()); } final OutputStreamTransport ost = new OutputStreamTransport(getSession()); final SendMailSession sendMailSession = new SendMailSession(getSession(), ost); sendMailSession.sendMail(email); return ost.getEml(); }
@SuppressWarnings("rawtypes") private static boolean sendMail(int messageType, String host, String user, String passwd, String subject, String message, String... to) { try { Email mail = Email.create() .from(user).to(to) .subject(subject); switch (messageType) { case HTML_TYPE: mail.addHtml(message); break; case TEXT_TYPE: default: mail.addText(message); break; } SmtpServer smtpServer = SmtpServer.create(host).authenticateWith(user, passwd); SendMailSession session = smtpServer.createSession(); session.open(); session.sendMail(mail); session.close(); return true; } catch (Exception e) { logger.warn(e.getMessage(), e); return false; } }