Send email with java [closed]

-1

Good afternoon. I'm using java, jpa, wildfly and primefaces. I need to send a notification email after the user clicks the send button. How do I send emails through java?

    
asked by anonymous 05.09.2016 / 20:47

1 answer

1

The best API for this is JavaMail . To use it you will need an email provider (for the example I cite it would be one from gmail). For example, the email sending code using SSL would look like this:

public class SendMailSSL {
public static void main(String[] args) {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class",
            "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session = Session.getDefaultInstance(props,
        new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("username","password");
            }
        });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Testing Subject");
        message.setText("Dear Mail Crawler," +
                "\n\n No spam to my email, please!");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
}
}

It's just an example, but it can vary depending on the form of authentication and email provider used. Take a look at this article , which is where I took it the above example using SSL.

    
05.09.2016 / 21:11