Search in sources :

Example 76 with MimeMessage

use of javax.mail.internet.MimeMessage in project OpenClinica by OpenClinica.

the class QueryServiceImpl method sendEmail.

private void sendEmail(String to, String subject, String body, Boolean htmlEmail) throws Exception {
    try {
        JavaMailSenderImpl mailSender = (JavaMailSenderImpl) appContext.getBean("mailSender");
        Properties javaMailProperties = mailSender.getJavaMailProperties();
        if (null != javaMailProperties) {
            if (javaMailProperties.get("mail.smtp.localhost") == null || ((String) javaMailProperties.get("mail.smtp.localhost")).equalsIgnoreCase("")) {
                javaMailProperties.put("mail.smtp.localhost", "localhost");
            }
        }
        MimeMessage mimeMessage = mailSender.createMimeMessage();
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, htmlEmail);
        helper.setFrom(EmailEngine.getAdminEmail());
        helper.setTo(processMultipleImailAddresses(to.trim()));
        helper.setSubject(subject);
        helper.setText(body, true);
        mailSender.send(mimeMessage);
        logger.debug("Email sent successfully on {}", new Date());
    } catch (MailException me) {
        logger.error("Email could not be sent on {} due to: {}", new Date(), me.getMessage());
    }
}
Also used : JavaMailSenderImpl(org.springframework.mail.javamail.JavaMailSenderImpl) MimeMessage(javax.mail.internet.MimeMessage) MailException(org.springframework.mail.MailException) Properties(java.util.Properties) MimeMessageHelper(org.springframework.mail.javamail.MimeMessageHelper) Date(java.util.Date)

Example 77 with MimeMessage

use of javax.mail.internet.MimeMessage in project jdk8u_jdk by JetBrains.

the class MailTest method sendMail.

void sendMail() {
    try {
        Properties props = new Properties();
        props.put("mail.smtp.host", host);
        props.put("mail.smtp.auth", "true");
        Session session = Session.getInstance(props);
        session.setDebug(true);
        // Define message
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipients(Message.RecipientType.TO, to);
        message.setSubject("this is a multipart test");
        Multipart multipart = new MimeMultipart();
        BodyPart messageBodyPart1 = new MimeBodyPart();
        messageBodyPart1.setText("please send also this Content\n ciao!");
        multipart.addBodyPart(messageBodyPart1);
        BodyPart messageBodyPart2 = new MimeBodyPart();
        messageBodyPart2.setContent("<b>please</b> send also this Content <br>ciao!", "text/html; charset=UTF-8");
        multipart.addBodyPart(messageBodyPart2);
        message.setContent(multipart);
        /*
                Transport tr = session.getTransport("smtp");
                tr.connect(host,user, password);
                tr.sendMessage(message,InternetAddress.parse(to));
                tr.close();
            */
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        message.writeTo(baos);
        String output = baos.toString();
        System.out.println("output = " + output);
        if (output.contains("also this Content")) {
            System.out.println("Test PASSED.");
        } else {
            System.out.println("Test FAILED, missing content.");
            throw new IllegalStateException("Test FAILED, missing content.");
        }
    } catch (MessagingException ignored) {
    } catch (IOException ignored) {
    }
}
Also used : MimeBodyPart(javax.mail.internet.MimeBodyPart) BodyPart(javax.mail.BodyPart) InternetAddress(javax.mail.internet.InternetAddress) MimeMultipart(javax.mail.internet.MimeMultipart) Multipart(javax.mail.Multipart) MessagingException(javax.mail.MessagingException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Properties(java.util.Properties) MimeMessage(javax.mail.internet.MimeMessage) MimeMultipart(javax.mail.internet.MimeMultipart) MimeBodyPart(javax.mail.internet.MimeBodyPart) Session(javax.mail.Session)

Example 78 with MimeMessage

use of javax.mail.internet.MimeMessage in project symmetric-ds by JumpMind.

the class MailService method sendEmail.

protected String sendEmail(String subject, String text, String recipients, Properties prop, String transportType, boolean useAuth, String user, String password) {
    Session session = Session.getInstance(prop);
    ByteArrayOutputStream ba = null;
    if (log.isDebugEnabled()) {
        session.setDebug(true);
        ba = new ByteArrayOutputStream();
        PrintStream ps = new PrintStream(ba);
        session.setDebugOut(ps);
    }
    Transport transport;
    try {
        transport = session.getTransport(transportType);
    } catch (NoSuchProviderException e) {
        log.error("Failure while obtaining transport", e);
        return getNestedErrorMessage(e);
    }
    try {
        if (useAuth) {
            transport.connect(user, password);
        } else {
            transport.connect();
        }
    } catch (MessagingException e) {
        log.error("Failure while connecting to transport", e);
        return getNestedErrorMessage(e);
    }
    try {
        MimeMessage message = new MimeMessage(session);
        message.setSentDate(new Date());
        message.setRecipients(RecipientType.BCC, recipients);
        message.setSubject(subject);
        message.setText(text);
        try {
            transport.sendMessage(message, message.getAllRecipients());
        } catch (MessagingException e) {
            log.error("Failure while sending notification", e);
            return getNestedErrorMessage(e);
        }
    } catch (MessagingException e) {
        log.error("Failure while preparing notification", e);
        return e.getMessage();
    } finally {
        try {
            transport.close();
        } catch (MessagingException e) {
        }
    }
    if (log.isDebugEnabled()) {
        log.debug(ba.toString());
    }
    return null;
}
Also used : PrintStream(java.io.PrintStream) MessagingException(javax.mail.MessagingException) MimeMessage(javax.mail.internet.MimeMessage) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Transport(javax.mail.Transport) NoSuchProviderException(javax.mail.NoSuchProviderException) Date(java.util.Date) Session(javax.mail.Session)

Example 79 with MimeMessage

use of javax.mail.internet.MimeMessage in project GNS by MobilityFirst.

the class Email method simpleMail.

/**
   *
   * @param subject
   * @param recipient
   * @param text
   * @param suppressWarning
   * @return true if the mail was sent
   */
public static boolean simpleMail(String subject, String recipient, String text, boolean suppressWarning) {
    try {
        MailSSLSocketFactory sf = new MailSSLSocketFactory();
        sf.setTrustAllHosts(true);
        Properties props = new Properties();
        props.setProperty("mail.smtp.ssl.enable", "true");
        //props.put("mail.smtp.host", smtpHost);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.ssl.socketFactory", sf);
        Session session = Session.getInstance(props);
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(Config.getGlobalString(GNSConfig.GNSC.SUPPORT_EMAIL)));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient));
        message.setSubject(subject);
        message.setText(text);
        SMTPTransport t = (SMTPTransport) session.getTransport("smtp");
        try {
            t.connect(SMTP_HOST, Config.getGlobalString(GNSConfig.GNSC.ADMIN_EMAIL), Config.getGlobalString(GNSConfig.GNSC.ADMIN_PASSWORD));
            t.sendMessage(message, message.getAllRecipients());
            getLogger().log(Level.FINE, "Email response: {0}", t.getLastServerResponse());
        } finally {
            t.close();
        }
        getLogger().log(Level.FINE, "Successfully sent email to {0} with message: {1}", new Object[] { recipient, text });
        return true;
    } catch (GeneralSecurityException | MessagingException e) {
        if (!suppressWarning) {
            getLogger().log(Level.WARNING, "Unable to send email: {0}", e);
        }
        return false;
    }
}
Also used : MailSSLSocketFactory(com.sun.mail.util.MailSSLSocketFactory) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) GeneralSecurityException(java.security.GeneralSecurityException) Properties(java.util.Properties) Session(javax.mail.Session) SMTPTransport(com.sun.mail.smtp.SMTPTransport)

Example 80 with MimeMessage

use of javax.mail.internet.MimeMessage in project libresonic by Libresonic.

the class RecoverController method emailPassword.

/*
    * e-mail user new password via configured Smtp server
    */
private boolean emailPassword(String password, String username, String email) {
    /* Default to protocol smtp when SmtpEncryption is set to "None" */
    String prot = "smtp";
    if (settingsService.getSmtpServer() == null || settingsService.getSmtpServer().isEmpty()) {
        LOG.warn("Can not send email; no Smtp server configured.");
        return false;
    }
    Properties props = new Properties();
    if (settingsService.getSmtpEncryption().equals("SSL/TLS")) {
        prot = "smtps";
        props.put("mail." + prot + ".ssl.enable", "true");
    } else if (settingsService.getSmtpEncryption().equals("STARTTLS")) {
        prot = "smtp";
        props.put("mail." + prot + ".starttls.enable", "true");
    }
    props.put("mail." + prot + ".host", settingsService.getSmtpServer());
    props.put("mail." + prot + ".port", settingsService.getSmtpPort());
    /* use authentication when SmtpUser is configured */
    if (settingsService.getSmtpUser() != null && !settingsService.getSmtpUser().isEmpty()) {
        props.put("mail." + prot + ".auth", "true");
    }
    Session session = Session.getInstance(props, null);
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(settingsService.getSmtpFrom()));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
        message.setSubject("Libresonic Password");
        message.setText("Hi there!\n\n" + "You have requested to reset your Libresonic password.  Please find your new login details below.\n\n" + "Username: " + username + "\n" + "Password: " + password + "\n\n" + "--\n" + "Your Libresonic server\n" + "libresonic.org");
        message.setSentDate(new Date());
        Transport trans = session.getTransport(prot);
        try {
            if (props.get("mail." + prot + ".auth") != null && props.get("mail." + prot + ".auth").equals("true")) {
                trans.connect(settingsService.getSmtpServer(), settingsService.getSmtpUser(), settingsService.getSmtpPassword());
            } else {
                trans.connect();
            }
            trans.sendMessage(message, message.getAllRecipients());
        } finally {
            trans.close();
        }
        return true;
    } catch (Exception x) {
        LOG.warn("Failed to send email.", x);
        return false;
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Transport(javax.mail.Transport) Date(java.util.Date) Session(javax.mail.Session)

Aggregations

MimeMessage (javax.mail.internet.MimeMessage)1146 Test (org.junit.Test)374 InternetAddress (javax.mail.internet.InternetAddress)334 MessagingException (javax.mail.MessagingException)299 Session (javax.mail.Session)222 Properties (java.util.Properties)219 MimeMultipart (javax.mail.internet.MimeMultipart)208 MimeBodyPart (javax.mail.internet.MimeBodyPart)178 Date (java.util.Date)153 IOException (java.io.IOException)137 Message (javax.mail.Message)120 MimeMessageHelper (org.springframework.mail.javamail.MimeMessageHelper)107 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)97 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)83 InputStream (java.io.InputStream)82 ArrayList (java.util.ArrayList)81 Multipart (javax.mail.Multipart)75 DataHandler (javax.activation.DataHandler)73 ByteArrayOutputStream (java.io.ByteArrayOutputStream)72 BodyPart (javax.mail.BodyPart)70