I need to make a simple e-mail sender with JavaSE 1.6. I tried to search a solution but I found only discussions about JavaEE.
Asked
Active
Viewed 133 times
-4
-
https://developers.google.com/appengine/docs/java/mail/usingjavamail – scrappedcola Jul 03 '13 at 15:44
-
really? google "java send email". Top result: http://www.tutorialspoint.com/java/java_sending_email.htm. Also related: this SO post: http://stackoverflow.com/questions/3649014/send-email-using-java – Russell Uhl Jul 03 '13 at 15:45
-
Javax.mail is not a SE library, is for the EE enviroment! – user2512014 Jul 03 '13 at 15:49
-
Not sure how simple you need, but if you are sending mail from a swing or JavaFX app, you need to send the mail from a thread OTHER than the Event dispatch thread. I used SwingWorker for that purpose. The code in my answer is much simpler. – Thorn Jul 03 '13 at 16:09
-
javax.mail can be used in SE environment if you bothered to read the page: he JavaMail API provides a platform-independent and protocol-independent framework to build mail and messaging applications. The JavaMail API is available as an optional package for use with Java SE platform and is also included in the Java EE platform. http://www.oracle.com/technetwork/java/javamail/index.html – scrappedcola Jul 03 '13 at 19:08
1 Answers
1
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("yourAddress@gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("arunGupta@oracle.com"));
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());
}
}

Thorn
- 4,015
- 4
- 23
- 42