package maildemo;
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
/**
*
* @author vijayP
*/
class Mailer{
public static void send(String from,String password,String to,String sub,String msg){
//Get properties object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.zoho.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.starttls.enable", "true");
props.put("mail.debug", "true");
props.put("mail.smtp.port", "465");
//get Session
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from,password);
}
});
//compose message
try {
MimeMessage message = new MimeMessage(session);
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject(sub);
message.setText(msg);
//send message
Transport.send(message);
System.out.println("message sent successfully");
} catch (MessagingException e) {
System.out.println("MessagingException"+e);
// throw new RuntimeException(e);
}
}
}
public class MailDemo {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Mailer.send(Constants.SMTP_AUTH_USER,Constants.SMTP_AUTH_PWD,Constants.SMTP_TO_USER,"hello vijay","How r u?");
}
}