SendMailBean.java Source code

Java tutorial

Introduction

Here is the source code for SendMailBean.java

Source

import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import java.sql.*;
import javax.sql.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class SendMailBean {
    private Connection con = null;

    protected String username;

    public String getPasswordAndEmailAddress() {
        //load email from database
        String password = "Password";
        String email = "Email";

        emailPassword(email, username, password);
    }

    public void emailPassword(String email, String memberName, String password) {
        String host = "mail";
        String from = "w@j.com";

        Properties props = System.getProperties();

        props.put("mail.smtp.host", host);
        Session session = Session.getDefaultInstance(props, null);
        MimeMessage message = new MimeMessage(session);

        try {
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));

            message.setSubject("Password Reminder");

            message.setText("Hi " + memberName + ",\nYour password is: " + password + "\nregards - " + from);
            Transport.send(message);
        } catch (AddressException ae) {
        } catch (MessagingException me) {
        }
    }
}