/** * 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(); }
@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(); } }
.content(new byte[]{'X', 'Z', 'X'})); session.sendMail(sentEmail);
.attachment(EmailAttachment.with().content(new byte[]{(byte) count})); session.sendMail(sentEmail);
session.open(); session.sendMail(sentEmail);
@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; } }
/** * 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(); }