package com.vci.server.base.mail;
|
|
import java.util.Locale;
|
import java.util.Properties;
|
|
import javax.mail.Message;
|
import javax.mail.Session;
|
import javax.mail.Transport;
|
|
import com.vci.common.exception.LocaleCommonDisplay;
|
import com.vci.corba.common.VCIError;
|
|
public class EmailSender implements IEmailSender{
|
|
private static EmailSender emailSender = null;
|
|
public static EmailSender getInstance() {
|
if (emailSender == null) {
|
emailSender = new EmailSender();
|
}
|
|
return emailSender;
|
}
|
|
@Override
|
public boolean sendEmail(MailObject obj) throws VCIError {
|
boolean rs = false;
|
try {
|
EmaiAuthenticator auth = new EmaiAuthenticator();
|
Properties pros = EmailProperties.getProperties();
|
Session session = Session.getInstance(pros, auth);
|
MailMessage mailMessage = new MailMessage();
|
Message message = mailMessage.getMailMessage(session, obj);
|
Transport.send(message);
|
rs = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
throw new VCIError("", new String[]{LocaleCommonDisplay.getI18nString("700001", "RMIPMail",Locale.getDefault())});
|
}
|
|
return rs;
|
}
|
|
/**
|
* 判断当前用户名密码是否正确
|
* @param host, smtp服务器
|
* @param userName, 用户名
|
* @param password, 密码
|
* @return
|
*/
|
@Override
|
public boolean verifyUserHasAuth(String host, String userName, String password) {
|
boolean isHas = false;
|
Properties props = new Properties();
|
props.put("mail.smtp.host", host);//指定SMTP服务器
|
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
|
try {
|
Session mailSession = Session.getInstance(props);
|
mailSession.setDebug(true);//是否在控制台显示debug信息
|
|
Transport transport = mailSession.getTransport("smtp");
|
transport.connect(host, userName, password);
|
transport.close();
|
isHas = true;
|
} catch (Exception e) {
|
e.printStackTrace();
|
//System.out.println(e);
|
}
|
|
return isHas;
|
}
|
|
public static void main(String[] args) throws VCIError {
|
EmailSender sender = EmailSender.getInstance();
|
MailObject obj = new MailObject();
|
obj.setMailFrom("liucq@vci-tech.com");
|
obj.setSubject("我的测试邮件题目");
|
obj.setText("请点击链接: www.163.com ss");
|
String[] mailTos = new String[1];
|
mailTos[0] = "liucq@vci-tech.com";
|
obj.setMailTos(mailTos);
|
sender.sendEmail(obj);
|
}
|
}
|