import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
/**
* JavaMail Send Mail Tools
*
* @author
* @version 1.0
*/
public class MailUtil {
/**
* Accessory address
*/
private String filepath;
/**
* Take Accessories
*
* @return filepath
*/
public String getFilepath() {
return filepath;
}
/**
* Set Accessories
*
* @param filepath filepath
*/
public void setFilepath(String filepath) {
this.filepath = filepath;
}
/**
* Send e-mail server address
*/
private String mailServerHost = "smtp.exmail.qq.com";
/**
* Send e-mail server port
*/
private String mailServerPort = "25";
/**
* The need to verify the settings before sending mail
*/
private boolean validate = true;
/**
* Send e-mail account settings
*/
private String userName = "[email protected]";
/**
* E-mail password
*/
private String userPass = "Cd1555";
/**
* Set sending mail address
*/
private String fromAddress = "[email protected]";
/**
* Send e-mail objects
*/
private String toAddress = "[email protected]";
/**
* Set the subject of the message sent
*/
private String subject = "test java Send Mail";
/**
* content of email
*/
private String content = "hello Send a message content <br/> <br/> test using java - <br/>!";
/**
* Set the message transmission format is encoded using html
*/
private boolean isHTML = true;
/**
* Set whether to send mail using a secure connection
*/
private boolean isSSL = false;
/**
* Set the host to send mail
*
* @param mailServerHost
* Send e-mail server address
*/
public void setMailServerHost(String mailServerHost) {
this.mailServerHost = mailServerHost;
}
/**
* Sending mail server port provided <br/>
* Note: The default value of "25"
*
* @param mailServerPort
* Send e-mail server port
*/
public void setMailServerPort(String mailServerPort) {
this.mailServerPort = mailServerPort;
}
/**
* <br/> need to verify before sending mail settings
* Note: The default value is true for verification
*
* @param validate
To verify * true or false does not validate
*/
public void setValidate(boolean validate) {
this.validate = validate;
}
/**
* E-mail address provided <br/>
* Note: The default value of "[email protected]"
*
* @param fromAddress
* Send e-mail address
*/
public void setFromAddress(String fromAddress) {
this.fromAddress = fromAddress;
}
/**
* Set received mail address <br/>
* Note: The default value of "[email protected]"
*
* @param toAddress
* Receive e-mail address
*/
public void setToAddress(String toAddress) {
this.toAddress = toAddress;
}
/**
* Set send messages relating <br/>
* Note: The default value of "test java Send Mail"
*
* @param subject
* Subject to send mail
*/
public void setSubject(String subject) {
this.subject = subject;
}
/**
* Set send mail content <br/>
* Note: The default value of "a Hello!
* Send message by testing content <br/> java
* <br/>
* - TKK <br/>
* "
*
* @param content
* E-mail the
*/
public void setContent(String content) {
this.content = content;
}
/**
* Set the message transmission format is encoded using html <br/>
* Note: The default is true
*
* @Param isHTML
* True is false No
*/
public void setHTML(boolean isHTML) {
this.isHTML = isHTML;
}
/**
* E-mail setting whether to use a secure connection <br/>
* Note: No Default false
*
* @Param isSSL
* True is false No
*/
public void setSSL(boolean isSSL) {
this.isSSL = isSSL;
}
/**
* Send e-mail account settings
*
* @param userName
* Send e-mail account
*/
public void setUserName(String userName) {
this.userName = userName;
}
/**
* Set sending mail account password
*
* @param userPass
* Send e-mail account password
*/
public void setUserPass(String userPass) {
this.userPass = userPass;
}
/**
* send email
*
* @param toAddress
* Recipient E-mail address
* @param subject
* Email Subject
* @param content
* content of email
* @param filepath
* Accessory address
* @Return true: sent successfully; false: Failed to send
*/
public boolean sendMail(String toAddress, String subject, String content,
String filepath) {
this.toAddress = toAddress;
this.subject = subject;
this.content = content;
this.filepath = filepath;
return sendMail();
}
/**
* send email
*
* @Return true: sent successfully; false: Failed to send
*/
public boolean sendMail() {
Properties p = new Properties();
p.put("mail.smtp.host", mailServerHost);
p.put("mail.smtp.port", mailServerPort);
p.put("mail.smtp.auth", validate ? "true" : "false");
if (isSSL) {
p.put("mail.smtp.starttls.enable", "true");
p.put("mail.smtp.socketFactory.fallback", "false");
p.put("mail.smtp.socketFactory.port", mailServerPort);
}
Authenticator auth = null;
if (validate) {
auth = new MyAuthenticator(userName, userPass);
}
try {
Session session = Session.getDefaultInstance(p, auth);
Message message = new MimeMessage(session);
Address from = new InternetAddress(fromAddress);
Address to = new InternetAddress(toAddress);
message.setFrom(from);
message.setRecipient(Message.RecipientType.TO, to);
message.setSubject(subject);
message.setSentDate (new Date ());
if (isHTML) {
Multipart m = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html; charset=utf-8");
// add attachments
FileDataSource ds = new FileDataSource(filepath);
bp.setDataHandler(new DataHandler(ds));
bp.setFileName(MimeUtility.encodeText(ds.getName()));
m.addBodyPart(bp);
message.setContent(m);
} else
message.setText(content);
Transport.send(message);
return true;
} catch (Exception e) {
e.printStackTrace ();
return false;
}
}
/**
* Send test
*
* @param args
* Parameters
*/
public static void main(String[] args) {
boolean x = new MailUtil().sendMail();
LoggerUtils.info(x);
}
}