Showing posts with label java mail. Show all posts
Showing posts with label java mail. Show all posts
April 30, 2012
How to send email from a Java program via Gmail's SMTP server

This Java function uses your Email, Password to send a Message to other Email address via Gmail's SMTP server.
Download JavaMail Library: http://www.oracle.com/technetwork/java/javamail/index-138643.html
Extract the Zip Archive
Import mail.jar into your project
Download
Download JavaMail Library: http://www.oracle.com/technetwork/java/javamail/index-138643.html
Extract the Zip Archive
Import mail.jar into your project
- public void sendMailViaGmail(final String email, final String password, String sendTo, String subject, String body) {
- java.util.Properties properties = new java.util.Properties();
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.starttls.enable", "true");
- properties.put("mail.smtp.host", "smtp.gmail.com");
- properties.put("mail.smtp.port", "587");
- javax.mail.Session session = javax.mail.Session.getInstance(properties, new javax.mail.Authenticator() {
- protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
- return new javax.mail.PasswordAuthentication(email, password);
- }
- });
- try {
- javax.mail.Message message = new javax.mail.internet.MimeMessage(session);
- message.setFrom(new javax.mail.internet.InternetAddress(email));
- message.setRecipients(javax.mail.Message.RecipientType.TO, javax.mail.internet.InternetAddress.parse(sendTo));
- message.setSubject(subject);
- message.setText(body);
- javax.mail.Transport.send(message);
- } catch (Exception e) {
- System.out.println(e);
- }
- }
Download
Subscribe to:
Posts (Atom)