import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
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 OTPMail {
public static void main(String ar[]){
final String userName = "madhu.jse";
final String password = "madhavi@1";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host",
"smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
Session session = Session.getDefaultInstance(props, new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(userName, password);
}
});
Message message = new MimeMessage(session);
try {
message.setFrom(new InternetAddress("madhu.jse@gmail.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("madhavigurram.java@gmail.com"));
message.setSubject("OTP");
message.setText("Hi this is from Madhu");
Transport.send(message);
System.out.println("Message send successfully");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
}
0 comments:
Post a Comment