Sending e-mail by using the JavaMail API and JDBC : Email « Servlets « Java






Sending e-mail by using the JavaMail API and JDBC

 

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) {
    }
  }
}

 








Related examples in the same category

1.Sending E-Mail from Servlets
2.Java servlet application
3.Email JNDI Filter
4.Email Bean
5.Servlet and email
6.Mail Accessor