Java SMTP mail sending code

        final String username = "xxx";
        final String password = "xxx";

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "HOSTNAME");
        props.put("mail.smtp.port", "587");

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

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress("abc@yahoo.de"));
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse("rec1@yahoo.de, rec2@yahoo.de"));
            message.setSubject("YOUR SUBJEC", "utf-8");
            String content = "YOUR HTML CONTENT GOES HERE";
            message.setContent(x, "text/html; charset=utf-8");

            Transport.send(message);

            System.out.println("Done");

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

Leave a Reply

Your email address will not be published. Required fields are marked *