io.afu.utils.mail.MailUtils Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of utils Show documentation
Show all versions of utils Show documentation
RffanLAB Utils For Many Way use
package io.afu.utils.mail;
import org.apache.commons.mail.*;
import java.net.URL;
public class MailUtils {
/**
* 依赖包
*
* org.apache.commons
* commons-email
* 1.5
*
* @param args
*/
private String charset="UTF-8";
private String host;
private Boolean ssl = false;
private int port = 25;
private String username;
private String password;
private String from;
private String to;
public String getCharset() {
return charset;
}
public void setCharset(String charset) {
this.charset = charset;
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public Boolean getSsl() {
return ssl;
}
public void setSsl(Boolean ssl) {
this.ssl = ssl;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public MailUtils(){
}
public MailUtils(String username, String password, String host, String from, String to, boolean ssl){
this.host = host;
if (ssl){
this.port = 465;
this.ssl = ssl;
}
this.from = from;
this.to = to;
this.username = username;
this.password = password;
}
public void send(String subject,String content){
try {
HtmlEmail email = new HtmlEmail();
email.setSSLOnConnect(this.ssl);
email.setHostName(this.host);
email.setSmtpPort(this.port);
email.setCharset(this.charset);
email.setAuthentication(this.username, this.password);
email.setFrom(this.from);
email.addTo(this.to);
email.setSubject(subject);
email.setHtmlMsg(content);
email.send();
} catch (EmailException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
}
}