发送邮件:SaveDocAction.java
package com.core.cbx.colortony.action;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
import com.core.cbx.action.actionContext.SaveDoc;
import com.core.cbx.action.exception.ActionException;
import com.core.cbx.data.constants.ColorTony;
import com.core.cbx.data.entity.DynamicEntity;
public class SaveDocAction extends com.core.cbx.action.SaveDocAction<SaveDoc> {
@Override
protected void process(SaveDoc actionContext) throws ActionException {
final DynamicEntity tony = actionContext.getDoc();
tony.put(ColorTony.STATUS, ColorTony.WorkflowStatus.IN_PROGRESS);
final BigDecimal defaultValue=BigDecimal.valueOf(0);//默认值为0
final BigDecimal value1= tony.getBigDecimal(ColorTony.RGB_CODE, defaultValue);//RGB默认值为0
final BigDecimal value2=tony.getBigDecimal(ColorTony.CMYK_CODE, defaultValue);//CMYK默认值为0
tony.put(ColorTony.HSV_CODE,value1.multiply(value2));//两个相乘的结果放到HSV
super.process(actionContext);//运行父类的process
}
@Override
protected void postprocess(SaveDoc actionContext) throws ActionException {
// TODO Auto-generated method stub
final String to = "18877882860@163.com";// 收件人电子邮箱
final String from = "tony.tang@coresolutions.com";// 发件人电子邮箱
final String host = "cube.zh.coresolutions.com"; // 指定发送邮件的主机为 localhost
final Properties properties = System.getProperties();// 获取系统属性
properties.setProperty("mail.smtp.host", host);// 设置邮件服务器
final Session session = Session.getDefaultInstance(properties, new Authenticator() { // 获取默认的 Session 对象。
@Override
public PasswordAuthentication getPasswordAuthentication() { // 邮箱服务器账户、第三方登录授权码
return new PasswordAuthentication("tony.tang@coresolutions.com", "3NXjDVaxcA"); // 发件人邮件用户名、密码
}
});
try {
final MimeMessage message = new MimeMessage(session);// 创建默认的 MimeMessage 对象
message.setFrom(new InternetAddress(from));// Set From: 头部头字段
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set To: 头部头字段
message.setSubject("Tony's work");// Set Subject: 主题文字
BodyPart messageBodyPart = new MimeBodyPart(); // 创建消息部分
messageBodyPart.setText("finished time for October 17, 2018,I'm Tony,today is good day");// 消息
final Multipart multipart = new MimeMultipart();// 创建多重消息
multipart.addBodyPart(messageBodyPart);// 设置文本消息部分
messageBodyPart = new MimeBodyPart();// 附件部分
final String filename = "C:/Users/tony.tang/Desktop/file.zip"; // 设置要发送附件的文件路径
final DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
try {
messageBodyPart.setFileName(MimeUtility.encodeText(filename));
} catch (final UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}// 处理附件名称中文(附带文件路径)乱码问题
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);// 发送完整消息
Transport.send(message); // 发送消息
System.out.println("Sent message successfully....");
} catch (final MessagingException mex) {
mex.printStackTrace();
}
super.postprocess(actionContext);
}
}