[Solved] How send e-mail with javaSE1.6 [closed]


You certainly can send mail from within Java SE, you just need to include two jar files with your project: mail.jar and activation.jar. Java EE contains a number of technologies that can be used outside of a Java EE container. You just need to add the relevant library files to your project and thus your class path. The code below should work on an email server using TLS and requiring authentication to send mail. An email server running on port 25 (no security) requires less code.

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("gmailUser", "*****");
                }
            });

    System.out.println(props);
    try {
        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("[email protected]"));
        message.setRecipients(Message.RecipientType.TO,
                InternetAddress.parse("[email protected]"));
        message.setSubject("Java EE7 jars deployed used on Java SE");
        message.setText("Dear Mr. Gupta,\nThank you for your work on Java EE7, but I need to send email from my desktop app.");
        Transport.send(message);
        System.out.println("Done");
    } catch (MessagingException utoh) {
        System.out.println("Error sending message:" + utoh.toString());
    }
}

solved How send e-mail with javaSE1.6 [closed]