//1.发送邮件方法
public class MailUtils {
private static boolean status = false;
private static Logger logger = Logger.getLogger(GiftReceivedController.class);
public static boolean sendMail(String fromaddr, String toId, String cC, String mailSubject, String messageContent,
String localFilePath, String attachFileName) throws MessagingException {
boolean debug = true;
String to = toId;
String subject = mailSubject;
String message = messageContent;
String localfile = localFilePath;
String host = PropertyUtil.getNewProperty("MAIL_HOST");
String type = PropertyUtil.getNewProperty("MAIL_TYPE");
logger.debug("Mail [" + host + "][" + type + "] F[" + fromaddr + "] T[" + toId + "] CC[" + cC + "] Sub["
+ mailSubject + "] Body[" + messageContent + "] fPath[" + localFilePath + "] fName[" + attachFileName
+ "]");
String from = fromaddr; // UIConstants.fromaddr;
final Properties prop = new Properties();
prop.put(type, host);
//-----------QQ邮箱-----------
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.user", PropertyUtil.getNewProperty("FROM_EMAIL"));
prop.put("mail.password", "mbxxtagblwvqbjie");
// 构建授权信息,用于进行SMTP进行身份验证
Authenticator authenticator = new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
// 用户名、密码
String userName = prop.getProperty("mail.user");
String password = prop.getProperty("mail.password");
return new PasswordAuthentication(userName, password);
}
};
//-----------QQ邮箱 end-----------
Session ses1 = Session.getInstance(prop, authenticator);
ses1.setDebug(debug);
Message msg = new MimeMessage(ses1);
//设置发送邮件
msg.setFrom(new InternetAddress(from));
//接收邮箱to
StringTokenizer st = new StringTokenizer(to, ",");
int count = st.countTokens();
InternetAddress[] address = new InternetAddress[count];
for (int i = 0; i < count; i++) {
address[i] = new InternetAddress((String) st.nextToken());
}
msg.setRecipients(Message.RecipientType.TO, address);
if (cC != null) { //设置抄送人
StringTokenizer st1 = new StringTokenizer(cC, ",");
int count1 = st1.countTokens();
InternetAddress[] address1 = new InternetAddress[count1];
for (int i = 0; i < count1; i++) {
address1[i] = new InternetAddress((String) st1.nextToken());
}
msg.setRecipients(Message.RecipientType.CC, address1);
}
//设置标题
msg.setSubject(subject);
// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();
// Fill the message 添加主题内容 设置编码方式
messageBodyPart.setContent(message, "text/html;charset=utf-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
if (localFilePath != null) { //如果有附件 则添加附件
System.out.println("localfile :: " + localfile);
messageBodyPart = new MimeBodyPart();
javax.activation.DataSource source = new FileDataSource(localfile);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(attachFileName);
multipart.addBodyPart(messageBodyPart);
}
msg.setContent(multipart, "text/html");
messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent("<br><br>", "text/html");
multipart.addBodyPart(messageBodyPart);
msg.setContent(multipart);
msg.setSentDate(new Date());
Transport.send(msg);
status = true;
return status;
}
@Override
public void submitSendEmaiToHRBP(String emailTemplateName, String giftid, UserProfile toUser, EmployeeDetails fromemp,
String localFilePath, String attachFileName) {
String employeeName = fromemp.getempname();
String sector = fromemp.getempsbaname();
String business = fromemp.getempsbuname();
String toemail = toUser.getEmail();
if(StringUtils.isEmpty(toemail)){
return;
}
//获取邮件模板
EmailTemplate emailTemplate = coreService.getEmailTemplateByName(emailTemplateName);
if(emailTemplate !=null) {
String emailContent = emailTemplate.getContent();
String linkReport = "";
String url = "http://localhost:8080/gdsweb/rec/view-gift?Id="+giftid+"&pageType=1";
//替换模板关键字
emailContent = emailContent.replace("[Name]", employeeName);
emailContent = emailContent.replace("[Sector]", sector);
emailContent = emailContent.replace("[Business]", business);
linkReport = "<a href='"+url+"'>Please click here to view the report</a>";
emailContent = emailContent.replace("[Please click here to view the report]", linkReport);
try {
String from_mail = PropertyUtil.getNewProperty("FROM_EMAIL");
logger.info(from_mail + " " + toemail);
//发送邮件
MailUtils.sendMail(from_mail, toemail, null,
emailTemplate.getSubject(), emailContent,
localFilePath,attachFileName);
} catch (Exception e) {
e.printStackTrace();
}
logger.info("Email sent to " + toemail + ".....SUCCESSFULLY");
}
}