common-mailテスト
4598 ワード
package cas.mdm.opermanage.fileload.service;
import java.util.Map;
import org.apache.commons.mail.EmailAttachment;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.MultiPartEmail;
public class EmailTool {
//smtp
private String hostName = "smtp.longshine.com";
// email
private String destEmail;
private String destName = "sir";
// email
private String fromEmail = "[email protected]";
private String fromName = "caoyitian";
private String subject = "a";
private String content = "Hello";
//
private String userName = "";
//
private String password = "";
private MultiPartEmail multiEmail;
private String obj2String(Object o){
return o==null?"":o.toString();
}
public void applyProperty(Map map){
this.hostName = this.obj2String(map.get("hostName"));
this.destEmail = this.obj2String(map.get("destEmail"));
this.destName = this.obj2String(map.get("destName"));
this.fromEmail = this.obj2String(map.get("fromEmail"));
this.fromName = this.obj2String(map.get("fromName"));
this.subject = this.obj2String(map.get("subject"));
this.userName = this.obj2String(map.get("userName"));
this.password = this.obj2String(map.get("password"));
}
public EmailTool(){
multiEmail = new MultiPartEmail();
}
public void addFile(String filePath,String decription,String name){
if(multiEmail!=null){
EmailAttachment attachment = new EmailAttachment();
attachment.setPath(filePath);
attachment.setName(name);
attachment.setDescription(decription);
attachment.setDisposition(EmailAttachment.ATTACHMENT);
try {
multiEmail.attach(attachment);
} catch (EmailException e) {
e.printStackTrace();
}
}
}
public static void main(String[] args){
EmailTool et = new EmailTool();
et.addFile("d:/listenerDebug/a_b_03_yi01_20111120.xml", "", "xml file");
System.out.println(et.send());
}
public boolean send(String dest){
try {
multiEmail.setHostName(hostName);
multiEmail.addTo(dest, destName);
multiEmail.setFrom(fromEmail, fromName);
multiEmail.setSubject(subject);
multiEmail.setMsg(content);
multiEmail.setTLS(true);
multiEmail.setAuthentication(userName, password);
} catch (EmailException e) {
e.printStackTrace();
multiEmail = null;
}
if(multiEmail!=null){
try{
multiEmail.send();
return true;
}catch(Exception e){
e.printStackTrace();
return false;
}
}
return false;
}
public boolean send(){
return send(destEmail);
}
public String getHostName() {
return hostName;
}
public void setHostName(String hostName) {
this.hostName = hostName;
}
public String getDestEmail() {
return destEmail;
}
public void setDestEmail(String destEmail) {
this.destEmail = destEmail;
}
public String getDestName() {
return destName;
}
public void setDestName(String destName) {
this.destName = destName;
}
public String getFromEmail() {
return fromEmail;
}
public void setFromEmail(String fromEmail) {
this.fromEmail = fromEmail;
}
public String getFromName() {
return fromName;
}
public void setFromName(String fromName) {
this.fromName = fromName;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
}