Send Mail using Java

To send an e-mail using your Java Application is simple enough but to start with you should have JavaMail API installed on your machine.

Download mail.jar file, copy this jar in the newly created top level directories. You need to add mail.jar files in your CLASSPATH.

 

Send Simple Email:

Here is an example of send a simple email from your machine.

 

Example :


import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


public class EmailSender {

    public static void sendEmail(String emailId)throws Exception {
        Properties props = null;
        if (props == null) {
            props = new Properties();
            props.put("mail.smtp.host", "smtp.live.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.user", "test@hotmail.com");
            props.put("mail.smtp.pwd", "password");
        }
        Session session = Session.getInstance(props, null);
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("test@hotmail.com"));
        msg.setSubject("Auto Generated Mail");
        msg.setText("Testing Mail");
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress("test1@gmail.com"));
        Transport transport = session.getTransport("smtp");
        transport.connect("smtp.live.com", 587, "test@hotmail.com", "password");
        transport.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Mail sent successfully at "+emailId);
        transport.close();
    }
    
    public static void main(String[] args) {
		try {
			EmailSender.sendEmail("Test", "to@gmail.com", "Testing");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}



Send mail with attachment using java

 

Example:


import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;


public class MailHandler {

public static void sendForgetPwdMail(String fileName, String emailId, String password)throws Exception {
        Properties props = null;
        if (props == null) {
            props = new Properties();
            props.put("mail.smtp.host", "smtp.live.com");
            props.put("mail.smtp.port", "587");
            props.put("mail.smtp.user", "sender@hotmail.com");
            props.put("mail.smtp.pwd", "password");
        }
        Session session = Session.getInstance(props, null);
        session.setDebug(true);
        Message msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("sender@hotmail.com"));
        msg.setSubject("Auto Generated Mail");
		msg.setRecipient(Message.RecipientType.TO, new InternetAddress(emailId));
        Transport transport = session.getTransport("smtp");
        BodyPart message1 = new MimeBodyPart();
        message1.setText("Testing Mail");
        
        MimeBodyPart message2 = new MimeBodyPart();
        
        DataSource source = new FileDataSource(fileName);
        message2.setDataHandler(new DataHandler(source));
        message2.setFileName(fileName);
        
        Multipart multipart = new MimeMultipart();
        
        multipart.addBodyPart(message1);
        multipart.addBodyPart(message2);
        
        msg.setContent(multipart);
        
        transport.connect("smtp.live.com", 587, "sender@hotmail.com", "password");
        transport.sendMessage(msg, msg.getAllRecipients());
        System.out.println("Mail sent successfully at "+emailId);
        transport.close();
    }
    
    public static void main(String[] args) {
		try {
			MailHandler.sendForgetPwdMail("C:\\logo.jpg", "test@gmail.com", "Testing");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}