/**
*
*/
Package com.email;
Import java.security.GeneralSecurityException;
/**
* <p>description: </p>
* @author Chu Kaixin
* @date June 6, 2018
*
*/
Import java.util.List;
Import java.util.Properties;
Import javax.mail.MessagingException;
Import javax.mail.Session;
Import Javax.mail.Transport;
Import javax.mail.internet.AddressException;
Import javax.mail.internet.InternetAddress;
Import Javax.mail.internet.MimeMessage;
Import Javax.mail.internet.MimeMessage.RecipientType;
Import Com.sun.mail.util.MailSSLSocketFactory;
/**
* Mail Sender: Can be single, can be mass
*/
public class MailSender {
/**
* Properties file for sending mail
*/
Private final transient Properties props = system.getproperties ();
/**
* Server Mailbox Login Verification
*/
private transient mailauthenticator mailauthenticator;
/**
* Email Session
*/
private transient session session;
/**
* Initialize the mailbox sender
*
* @param mailserviceurl
* Server Email address
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
Public MailSender (final string mailserviceurl,final string username, final string password) {
Init (mailserviceurl, username, password);
}
/**
* Initialize the mailbox sender
*
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
Public MailSender (final string username, final string password) {
Resolves an SMTP server through a mailbox address, which works for most mailboxes
Final String Mailserviceurl = "smtp." + Username.split ("@") [1];
Init (mailserviceurl, username, password);
}
/**
* Initialization operation
*
* @param mailserviceurl
* Server Email address
* @param username
* Server mailbox User name
* @param password
* Server Mailbox Login Password
*/
public void init (string mailserviceurl, string Username, string password) {
Initialize Props
Props.put ("Mail.smtp.auth", "true");
Props.put ("Mail.smtp.host", Mailserviceurl);
Mailsslsocketfactory SF;
try {
SF = new Mailsslsocketfactory ();
Sf.settrustallhosts (TRUE);
Props.put ("Mail.smtp.ssl.enable", "true");
Props.put ("Mail.smtp.ssl.socketFactory", SF);
} catch (Generalsecurityexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
Server Mailbox Validation
Mailauthenticator = new Mailauthenticator (username, password);
Create a session and want to log in as a mailbox
Session = Session.getinstance (props, mailauthenticator);
}
/**
* Send mail
*
* @param recipient
* Email address of the person receiving the letter
* @param subject
* Mail Title
* @param content
* Email Content
* @throws addressexception
* @throws messagingexception
*/
public void Send (string recipient, string subject, string content) throws Addressexception, messagingexception{
Creating MIME-type messages
Final MimeMessage msg = new MimeMessage (session);
Set Sender
Msg.setfrom (New InternetAddress (Mailauthenticator.getusername ()));
Set up the recipient.
Msg.setrecipient (recipienttype.to, New InternetAddress (recipient));
Set the message header
Msg.setsubject (subject);
Set up message content
Msg.setcontent (Content, "text/html;charset=utf-8");
Send mail
Transport.send (msg);
}
/**
* Mass Mailing
*
* @param recipients
* Email address of the person receiving the letter
* @param subject
* Mail Title
* @param content
* Email Content
* @throws addressexception
* @throws messagingexception
*/
public void Send (list<string> recipients, string subject, string content) throws Addressexception, messagingexception{
Creating MIME-type messages
Final MimeMessage msg = new MimeMessage (session);
Set Sender
Msg.setfrom (New InternetAddress (Mailauthenticator.getusername ()));
Set up the people who receive the message
int num = Recipients.size ();
Internetaddress[] addresses = new Internetaddress[num];
for (int i = 0; i < num; i++) {
Addresses[i] = new InternetAddress (Recipients.get (i));
}
Msg.setrecipients (recipienttype.to, addresses);
Set the message header
Msg.setsubject (subject);
Set up message content
Msg.setcontent (Content, "text/html;charset=utf-8");
Transport.send (msg);
}
/**
* Send mail
*
* @param recipient
* Email address of the person receiving the letter
* @param mail
* Mail Object
* @throws addressexception
* @throws messagingexception
* @throws
*/
public void Send (String recipient, mail mail) throws Addressexception, messagingexception{
This.send (recipient, Mail.getsubject (), mail.getcontent ());
}
/**
* Mass Mailing
*
* @param recipients
* Email address of the person receiving the letter
* @param mail
* Mail Object
* @throws addressexception
* @throws messagingexception
* @throws
*/
public void Send (list<string> recipients, mail mail) throws Addressexception, messagingexception{
This.send (Recipients, Mail.getsubject (), mail.getcontent ());
}
} |