/** * Create a {@link SmtpServer} from current data. * * @return {@link SmtpServer} from current data. * */ public SmtpServer buildSmtpMailServer() { if (ssl) { return new SmtpSslServer(this); } return new SmtpServer(this); } }
/** * {@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); }
@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; } }
@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(); } }
@Override protected Properties createSessionProperties() { final Properties props = super.createSessionProperties(); props.setProperty(MAIL_SMTP_STARTTLS_REQUIRED, startTlsRequired ? StringPool.TRUE : StringPool.FALSE); props.setProperty(MAIL_SMTP_STARTTLS_ENABLE, StringPool.TRUE); props.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, String.valueOf(port)); props.setProperty(MAIL_SMTP_PORT, String.valueOf(port)); if (!plaintextOverTLS) { props.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); } props.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, StringPool.FALSE); props.setProperty(MAIL_HOST, host); return props; }
final SendMailSession session = smtpServer.createSession();
@Override protected Properties createSessionProperties() { final Properties props = super.createSessionProperties(); props.setProperty(MAIL_SMTP_STARTTLS_REQUIRED, startTlsRequired ? StringPool.TRUE : StringPool.FALSE); props.setProperty(MAIL_SMTP_STARTTLS_ENABLE, StringPool.TRUE); props.setProperty(MAIL_SMTP_SOCKET_FACTORY_PORT, String.valueOf(port)); props.setProperty(MAIL_SMTP_PORT, String.valueOf(port)); if (!plaintextOverTLS) { props.setProperty(MAIL_SMTP_SOCKET_FACTORY_CLASS, "javax.net.ssl.SSLSocketFactory"); } props.setProperty(MAIL_SMTP_SOCKET_FACTORY_FALLBACK, StringPool.FALSE); props.setProperty(MAIL_HOST, host); return props; }
/** * {@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); }
final SendMailSession session = smtpServer.createSession();
/** * Create a {@link SmtpServer} from current data. * * @return {@link SmtpServer} from current data. * */ public SmtpServer buildSmtpMailServer() { if (ssl) { return new SmtpSslServer(this); } return new SmtpServer(this); } }
.buildSmtpMailServer(); final SendMailSession session = smtpServer.createSession(); session.open();