rendered paste bodypackage com.base.mail;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
/**
* EmailManager to send out mass mails. Each out going message is sent in its own thread.
*
* @author victori
*
*/
public class EmailManager {
private List<String> emails = new ArrayList();
private String from;
private Properties props = new Properties();
private String body;
private String subject;
private String mime = "text/plain";
private Session session;
/**
* Constructor needs a SMTP server and PORT to use.
*
* @param smtp
* @param port
*/
public EmailManager(String smtp, String port) {
props.put("mail.smtp.host", smtp);
props.put("mail.smtp.port", ""+port);
session = Session.getInstance(props,null);
}
/**
* Send out the emails
*
*/
public void sendEmails() {
for(String email: emails) {
new EmailMailer(session,from,email,subject,body,mime);
}
}
class EmailMailer implements Runnable {
private Session session;
private String from;
private String to;
private String subject;
private String body;
private String mime;
public EmailMailer(Session session,String from,String to,String subject, String body,String mime) {
this.session = session;
this.from = from;
this.to = to;
this.subject = subject;
this.body = body;
this.mime = mime;
synchronized(this) {
new Thread(this).start();
}
}
public void run() {
try {
Message msg = new MimeMessage(session);
Address from = new InternetAddress(this.from);
Address whom = new InternetAddress(this.to);
msg.setContent(body,mime);
msg.setFrom(from);
msg.setRecipient(Message.RecipientType.TO, whom);
msg.setSubject(subject);
Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
System.out.println("Issue email transportation to " + this.to);
}
}
}
/**
* Get sender email.
*
* @return
*/
public String getFrom() {
return from;
}
/**
* Set sender email.
*
* @param from
*/
public void setFrom(String from) {
this.from = from;
}
public Properties getProps() {
return props;
}
public void setProps(Properties props) {
this.props = props;
}
/**
* Append an email to the mass mail list.
*
* @param email
*/
public void appendEmail(String email) {
emails.add(email);
}
/**
* Remove an email from the mass mail list.
*
* @param email
*/
public void removeEmail(String email) {
for(int i = 0; i < emails.size(); i++) {
if (emails.get(i).equals(email)) {
emails.remove(i);
}
}
}
/**
* Get email list.
*
* @return
*/
public List getEmails() {
return emails;
}
/**
* Set email list.
*
* @param emails
*/
public void setEmails(List emails) {
this.emails = emails;
}
/**
* Set the content of the email.
*
* @param body
*/
public void setBody(String body) {
this.body = body;
}
/**
* Get the content of the email.
*
* @return
*/
public String getBody() {
return body;
}
/**
* Set the subject of the email.
*
* @param subject
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Get the subject of the email.
*
* @return
*/
public String getSubject() {
return subject;
}
/**
* Set mime type, if none set default to text/plain.
*
* @param mime
*/
public void setMime(String mime) {
this.mime = mime;
}
/**
* Get the current email mimetype.
*
* @return
*/
public String getMime() {
return mime;
}
}