Source Code : Send email using java

Send email using java

import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;

public class SendEmail {

  public static void main(String [] args) {

  // Recipient's email ID needs to be mentioned.
  String to = "abcd@gmail.com";

  // Sender's email ID needs to be mentioned
  String from = "web@gmail.com";

  // Assuming you are sending email from localhost
  String host = "localhost";

  // Get system properties
  Properties properties = System.getProperties();

  // Setup mail server
  properties.setProperty("mail.smtp.host", host);

  // Get the default Session object.
  Session session = Session.getDefaultInstance(properties);

  try{
  // Create a default MimeMessage object.
  MimeMessage message = new MimeMessage(session);

  // Set From: header field of the header.
  message.setFrom(new InternetAddress(from));

  // Set To: header field of the header.
  message.addRecipient(Message.RecipientType.TO,
  new InternetAddress(to));

  // Set Subject: header field
  message.setSubject("This is the Subject Line!");

  // Now set the actual message
  message.setText("This is actual message");

  // Send message
  Transport.send(message);
  System.out.println("Sent message successfully....");
  }catch (MessagingException mex) {
  mex.printStackTrace();
  }
  }
}

I am getting the error :

javax.mail.MessagingException: Could not connect to SMTP host: localhost, port: 25;
  nested exception is:java.net.ConnectException: Connection refused: connect
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1706)
  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:525)Will this code work to send email?


 

The following code works very well with Google SMTP server. You need to supply your Google username and password
 

import com.sun.mail.smtp.SMTPTransport;
import java.security.Security;
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author doraemon
 */
public class GoogleMail {
  private GoogleMail() {
  }

  /**
  * Send email using GMail SMTP server.
  *
  * @param username GMail username
  * @param password GMail password
  * @param recipientEmail TO recipient
  * @param title title of the message
  * @param message message to be sent
  * @throws AddressException if the email address parse failed
  * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
  */
  public static void Send(final String username, final String password, String recipientEmail, String title, String message) throws AddressException, MessagingException {
  GoogleMail.Send(username, password, recipientEmail, "", title, message);
  }

  /**
  * Send email using GMail SMTP server.
  *
  * @param username GMail username
  * @param password GMail password
  * @param recipientEmail TO recipient
  * @param ccEmail CC recipient. Can be empty if there is no CC recipient
  * @param title title of the message
  * @param message message to be sent
  * @throws AddressException if the email address parse failed
  * @throws MessagingException if the connection is dead or not in the connected state or if the message is not a MimeMessage
  */
  public static void Send(final String username, final String password, String recipientEmail, String ccEmail, String title, String message) throws AddressException, MessagingException {
  Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
  final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

  // Get a Properties object
  Properties props = System.getProperties();
  props.setProperty("mail.smtps.host", "smtp.gmail.com");
  props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
  props.setProperty("mail.smtp.socketFactory.fallback", "false");
  props.setProperty("mail.smtp.port", "465");
  props.setProperty("mail.smtp.socketFactory.port", "465");
  props.setProperty("mail.smtps.auth", "true");

  /*
  If set to false, the QUIT command is sent and the connection is immediately closed. If set
  to true (the default), causes the transport to wait for the response to the QUIT command.

  ref :  http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/package-summary.html
  http://forum.java.sun.com/thread.jspa?threadID=5205249
  smtpsend.java - demo program from javamail
  */
  props.put("mail.smtps.quitwait", "false");

  Session session = Session.getInstance(props, null);

  // -- Create a new message --
  final MimeMessage msg = new MimeMessage(session);

  // -- Set the FROM and TO fields --
  msg.setFrom(new InternetAddress(username + "@gmail.com"));
  msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipientEmail, false));

  if (ccEmail.length() > 0) {
  msg.setRecipients(Message.RecipientType.CC, InternetAddress.parse(ccEmail, false));
  }

  msg.setSubject(title);
  msg.setText(message, "utf-8");
  msg.setSentDate(new Date());

  SMTPTransport t = (SMTPTransport)session.getTransport("smtps");

  t.connect("smtp.gmail.com", username, password);
  t.sendMessage(msg, msg.getAllRecipients());   
  t.close();
  }
}

import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class SendMail {

  public static void main(String[] args) {

  final String username = "your_user_name@gmail.com";
  final String password = "yourpassword";

  Properties props = new Properties();
  props.put("mail.smtp.starttls.enable", "true");
  props.put("mail.smtp.auth", "true");
  props.put("mail.smtp.host", "smtp.gmail.com");
  props.put("mail.smtp.port", "587");

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

  try {

  Message message = new MimeMessage(session);
  message.setFrom(new InternetAddress("your_user_name@gmail.com"));
  message.setRecipients(Message.RecipientType.TO,
  InternetAddress.parse("to_email_address@domain.com"));
  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);
  }
  }
}