lavenderyhj
7/24/2018 - 4:14 AM

发送邮件

发送邮件的方法

public class MailUtil {
    private static Logger logger = LoggerFactory.getLogger(MailUtil.class);

    private static String host;
    private static String port;
    private static String username;
    //	private static String password;
    private static String sendFrom;
    private static String sendNick;

    // private static String isDebug;

    static {
        host = PropertiesUtil.getString("mail.host");
        port = PropertiesUtil.getString("mail.port");
        username = PropertiesUtil.getString("mail.username");
//		password = PropertiesUtil.getString("mail.password");
        sendFrom = PropertiesUtil.getString("mail.sendFrom");
        sendNick = PropertiesUtil.getString("mail.sendNick");
        
    }

    /**
     <!-- spring mail sender -->
     <bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"  scope="singleton" >
     <property name="host" value="${mail.host}" />			<!-- SMTP发送邮件的服务器的IP和端口 -->
     <property name="port" value="${mail.port}" />
     <property name="username" value="${mail.username}" />	<!-- 登录SMTP邮件发送服务器的用户名和密码 -->
     <property name="password" value="${mail.password}" />
     <property name="javaMailProperties">					<!-- 获得邮件会话属性,验证登录邮件服务器是否成功 -->
     <props>
     <prop key="mail.smtp.auth">true</prop>
     <prop key="prop">true</prop>
     <!-- <prop key="mail.smtp.timeout">25000</prop> -->
     </props>
     </property>
     </bean>
     */
  
    /**
     * 发送邮件 (完整版) (纯JavaMail)
     *
     * @param toAddress       : 收件人邮箱
     * @param mailSubject     : 邮件主题
     * @param mailBody        : 邮件正文
     * @param mailBodyIsHtml: 邮件正文格式,true:HTML格式;false:文本格式
     *                        //@param inLineFile	: 内嵌文件
     * @param attachments     : 附件
     */
    public static boolean sendMail(String toAddress, String mailSubject, String mailBody,
                                   boolean mailBodyIsHtml, File[] attachments) {
        try {
            //区分环境
            String env = ConfigEnum.Evn.getValue();
            mailSubject += " ---" + env;
            // 创建邮件发送类 JavaMailSender (用于发送多元化邮件,包括附件,图片,html 等    )
            JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
            mailSender.setHost(host);            // 设置邮件服务主机
            mailSender.setUsername(username);    // 发送者邮箱的用户名
//        	mailSender.setPassword(password); 	// 发送者邮箱的密码

            //配置文件,用于实例化java.mail.session
            Properties pro = new Properties();
            pro.put("mail.smtp.auth", "false");        // 登录SMTP服务器,需要获得授权 (网易163邮箱新近注册的邮箱均不能授权,测试 sohu 的邮箱可以获得授权)
            pro.put("mail.smtp.socketFactory.port", port);
            pro.put("mail.smtp.socketFactory.fallback", "false");
            mailSender.setJavaMailProperties(pro);

            //创建多元化邮件 (创建 mimeMessage 帮助类,用于封装信息至 mimeMessage)
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, ArrayUtils.isNotEmpty(attachments), "UTF-8");

            helper.setFrom(sendFrom, sendNick);
            helper.setTo(toAddress);
            // if (isDebug != null && isDebug.equals("true")) {
            //     mailSubject += "--测试";
            // }

            helper.setSubject(mailSubject);
            helper.setText(mailBody, mailBodyIsHtml);

            // 添加内嵌文件,第1个参数为cid标识这个文件,第2个参数为资源
            //helper.addInline(MimeUtility.encodeText(inLineFile.getName()), inLineFile);

            // 添加附件
            if (ArrayUtils.isNotEmpty(attachments)) {
                for (File file : attachments) {
                    helper.addAttachment(MimeUtility.encodeText(file.getName()), file);
                }
            }

            mailSender.send(mimeMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }

    static int total = 0;



}