java-mail-send-01 Sending mail using JavaMail API is quite easy in Java application, if you are creating console based, desktop or web based Java application, JavaMail API is very easy to integrate and help you to send not only text but also Hyper Text messages.

To work with JavaMail API you will need to download javamail-1.4.7  zipped file, after extracting it you will need following files in your java project library.

  • mail.jar
  • dsn.jar
  • gimap.jar
  • imap.jar
  • mailapi.jar
  • pop3.jar
  • smtp.jar

Now, lets create a simple class SendMail.java

import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
*
* @author girish
*/
public class SendMail {
private String username;
private String password;
private String to;
private String msg;
private String subject;

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getPassword() {
return password;
}

public void setPassword(String password) {
this.password = password;
}

public String getTo() {
return to;
}

public void setTo(String to) {
this.to = to;
}

public String getMsg() {
return msg;
}

public void setMsg(String msg) {
this.msg = msg;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public void send_mail() throws MessagingException{
Properties props = new Properties();//create an instance of properties
props.put(“mail.smtp.auth”, “true”); // set smtp to true
props.put(“mail.smtp.starttls.enable”, “true”);// enables starttls
props.put(“mail.smtp.host”, “smtp.gmail.com”);// add a host name, here i am using                                                                                                //gmail
props.put(“mail.smtp.port”, “587”);// smtp port for gmail is 587

// create Session instance from getInstance() static method

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

// create message instance using MimeMessage(session)
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));// set the recipient to
message.setSubject(subject);//set the subject
//message.setText(msg);// this is commented if you enable you can only send text                                                              message
message.setContent(msg,”text/html”);// setContent method is used to send HTML                                                                                   //formated messages
Transport.send(message);// Using static send() method of Transport class

System.out.println(“Done”);

}

public static void main(String args[]){//main method

SendMail sm=new SendMail();
sm.setUsername(“<your gmail username>”);
sm.setPassword(“<your gmail password>”);

sm.setMsg(“Testing message for JavaMail API”);
sm.setSubject(“LAVIT : ONLINE PROGRAMMING TRAINING ACADEMY”);
sm.setTo(“clairvoyant.1947@gmail.com”);

try {
sm.send_mail();
System.out.println(“Mail send successfully!!”);
} catch (MessagingException ex) {
System.out.println(“ERROR SENDING “+ex.getMessage());
}
}//end of main method
}//end of class

Keep in mind using

message.setText(msg); // used to send simple text message
message.setContent(msg,”text/html”);//used to send message embedded with HTML Tag

now you can use this complete class with any of other project , this will helpful for any type of Java Application

Thanks and Regards

Girish

Founder, Lavit Online Training Academy

for more visit : LAVIT ONLINE TRAINING ACADEMY  

TRY IT and ENJOY

Advertisements