65 lines
2.1 KiB
Java
65 lines
2.1 KiB
Java
//From: http://www.codejava.net/java-ee/jsp/sending-e-mail-with-jsp-servlet-and-javamail
|
|
package tools;
|
|
|
|
import java.util.Date;
|
|
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.AddressException;
|
|
import javax.mail.internet.InternetAddress;
|
|
import javax.mail.internet.MimeMessage;
|
|
|
|
/**
|
|
* A utility class for sending e-mail messages
|
|
* @author www.codejava.net
|
|
*
|
|
*/
|
|
public class EmailUtility {
|
|
|
|
public static void sendEmail(String host, String port,
|
|
final String userName, final String password, String toAddress,
|
|
String subject, String message) throws AddressException,
|
|
MessagingException
|
|
{
|
|
// sets SMTP server properties
|
|
Properties properties = new Properties();
|
|
|
|
properties.put("mail.smtp.host", host);
|
|
properties.put("mail.smtp.port", port);
|
|
properties.put("mail.smtp.auth", "true");
|
|
//properties.put("mail.smtp.starttls.enable","true"); STARTTLS requested but already using SSL
|
|
properties.put("mail.smtp.EnableSSL.enable","true");
|
|
properties.put("mail.smtp.socketFactory.port", port);
|
|
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
|
|
//properties.put("mail.debug", "true");
|
|
|
|
|
|
// creates a new session with an authenticator
|
|
Authenticator auth = new Authenticator() {
|
|
public PasswordAuthentication getPasswordAuthentication() {
|
|
return new PasswordAuthentication(userName, password);
|
|
}
|
|
};
|
|
|
|
Session session = Session.getInstance(properties, auth);
|
|
|
|
//creates a new e-mail message
|
|
Message msg = new MimeMessage(session);
|
|
|
|
msg.setFrom(new InternetAddress(userName));
|
|
InternetAddress[] toAddresses = { new InternetAddress(toAddress) };
|
|
msg.setRecipients(Message.RecipientType.TO, toAddresses);
|
|
msg.setSubject(subject);
|
|
msg.setSentDate(new Date());
|
|
//msg.setText(message);
|
|
msg.setContent(message, "text/html; charset=utf-8");
|
|
|
|
// sends the e-mail
|
|
Transport.send(msg);
|
|
}
|
|
} |