Java Email Send sendResetPasswordMail(String to, String newPassword, final String mailUser, final String mailPassword, String mailHost, String mailPort)

Here you can find the source of sendResetPasswordMail(String to, String newPassword, final String mailUser, final String mailPassword, String mailHost, String mailPort)

Description

send Reset Password Mail

License

Open Source License

Declaration

public static void sendResetPasswordMail(String to, String newPassword, final String mailUser,
            final String mailPassword, String mailHost, String mailPort) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import java.util.Properties;

public class Main {
    public static void sendResetPasswordMail(String to, String newPassword, final String mailUser,
            final String mailPassword, String mailHost, String mailPort) {

        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", mailHost);
        props.put("mail.smtp.port", mailPort);

        Session session = Session.getInstance(props, new javax.mail.Authenticator() {
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(mailUser, mailPassword);
            }/*from w w w .j a va 2 s  .  c  o m*/
        });

        try {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(mailUser));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));

            message.setSubject("Your password has been reset");
            message.setText("Hello, \n" + "You can now login using this new password:" + "\n\n" + newPassword
                    + "\n\n\n" + "Please change it when you first login" + "\n\n" + "Best regards,\n\n"
                    + "Systems Genomics Laboratory" + "\n");

            Transport.send(message);

            System.out.println("Password reset: " + to);

        } catch (MessagingException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. sendMail(String vmName, String property, String emailId, boolean usageHigh)
  2. sendMessage(Message message)
  3. sendMessage(Message msg, Transport transport)
  4. sendMimeMessage(MimeMessage mimeMessage)
  5. sendResetPasswordMail(String to, String message)