Training of CBX (Training_06)

发送邮件: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);
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值