E-Mail notification in java ee

package com.service;
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.InternetAddress;
import javax.mail.internet.MimeMessage;

import com.db.Users;


public class ResetPassword
{
public static void sendPassword(Users u)
{
// TODO Auto-generated method stub
String to=u.getEmail();
String subject="Password Sent";
String message="Hello Dear , Your Password is : "+u.getPassword();

String from="lit.bishnu.java@gmail.com"; // give ur mailid
String password="litbishnu"; // give ur passsword

try {
//Authentication with Gmail server
Properties props=new Properties();
props.put("mail.smtp.host", "smtp.gmail.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.port", "465");


Authenticator auth = new Authenticator() {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, password);
}
};

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

//Composing the message
MimeMessage msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
msg.setSubject(subject);
msg.setText(message);

//Sending message
Transport.send(msg);
System.out.println("Message delivered successfully , Check your mail ...........");
//response.sendRedirect("mail.jsp?msg=mail delivered");
} catch (MessagingException e) {
// TODO: handle exception
//throw new RuntimeException(e);
e.printStackTrace();
}
}
}


---------------------------------------------------------------------------------------------------------

This is not the full code.Here is the part of code.So understand it then work with it.Directly copy paste this code will not work.

Comments

Popular posts from this blog

Git Commands With Output Log

Java Interview Preparation (conceptual)

Java 8 Function Interface