@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(); } }
session.close();
session.close();
session.close();
@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; } }