Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package servlet;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import helper.MailHelper;
- public class MailServlet extends HttpServlet {
- private static final long serialVersionUID = 3303982021932755186L;
- @Override
- protected void doPost(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- processRequest(request, response);
- }
- public void processRequest(HttpServletRequest request,
- HttpServletResponse response) throws ServletException, IOException {
- String email = request.getParameter("mail");
- String subject = request.getParameter("subject");
- String message = request.getParameter("message");
- if (isValidParameters(email, subject, message)) {
- try {
- MailHelper.sendMail(email, subject, message);
- request.getRequestDispatcher("success.jsp").forward(request,
- response);
- } catch (Exception ex) {
- request.getRequestDispatcher("internalError.jsp").forward(
- request, response);
- }
- } else {
- request.getRequestDispatcher("error.jsp")
- .forward(request, response);
- }
- }
- private boolean isValidParameters(String... parameters) {
- boolean isValid = true;
- for (String param : parameters) {
- if (param == null || param.isEmpty()) {
- isValid = false;
- break;
- }
- }
- return isValid;
- }
- }
- package helper;
- 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.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- public class MailHelper {
- //input here your mail credentials
- static final String username = "myemail@gmail.com";
- static final String password = "mypassword";
- public static void sendMail(String mail, String subject, String message)
- throws AddressException, MessagingException {
- Message msg = new MimeMessage(getSession());
- msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(mail));
- msg.setSubject(subject);
- msg.setText(message);
- Transport.send(msg);
- }
- private static Session getSession() {
- Session session = Session.getDefaultInstance(getProperties(),
- new javax.mail.Authenticator() {
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- return session;
- }
- private static Properties getProperties() {
- Properties properties = new Properties();
- properties.put("mail.smtp.host", "smtp.gmail.com");
- properties.put("mail.smtp.socketFactory.port", "465");
- properties.put("mail.smtp.socketFactory.class",
- "javax.net.ssl.SSLSocketFactory");
- properties.put("mail.smtp.auth", "true");
- properties.put("mail.smtp.port", "465");
- return properties;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement