Java发送文本邮件及附件邮件,QQ邮箱POP3与SMTP授权码(springframework+javax.mail)

利用springframework+javax.mail发邮件(普通邮件、带附件邮件、HTML格式邮件)

一、引入相关的库

 

    org.springframework

    spring-core

    4.2.5.RELEASE

org.springframework

spring-beans

4.2.5.RELEASE

    org.springframework

    spring-context

    4.2.5.RELEASE

 

    org.springframework

    spring-context-support

    4.2.5.RELEASE

    javax.mail

    mail

    1.4.7

二、装配JavaMailSender类的配置信息

xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
   xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
   xmlns:cache="http://www.springframework.org/schema/cache" xmlns:p="http://www.springframework.org/schema/p"
   xsi:schemaLocation="http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache
     http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">

   <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
      <property name="host" value="smtp.qq.com" />//ip,下图有
      <property name="port" value="587" />//端口,下图有
      <property name="username" value="本人邮箱" />
      <property name="password" value="授权码" />
      <property name="protocol" value="smtp" />
      <property name="defaultEncoding" value="utf-8" />
      <property name="javaMailProperties">
         <props>
            <prop key="mail.smtp.auth">trueprop>
         props>
      property>
   bean>
   
   
beans>

Java发送文本邮件及附件邮件,QQ邮箱POP3与SMTP授权码(springframework+javax.mail)_第1张图片

Java发送文本邮件及附件邮件,QQ邮箱POP3与SMTP授权码(springframework+javax.mail)_第2张图片

编写工具类

import java.io.File;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;

@Component("emailutil")
public class EmailUtil {

   @Autowired
   @Qualifier("mailSender")
   private JavaMailSender mailSender=null;

   public void sendEmail(String to,String title,String content){
      //发送邮件
      SimpleMailMessage m = new SimpleMailMessage();
      m.setTo(to);
      m.setFrom("发件人邮箱");
      m.setSubject(title);
      m.setText(content);
      mailSender.send(m);
      System.out.println("发送成功!");
   }
   
   public void sendEmail(String to,String title,String html,String[] filepaths,boolean isHtml){
      //用复杂的格式传输复杂的邮件(带附件,html格式)
      MimeMessage me = mailSender.createMimeMessage();
      try {
         MimeMessageHelper helper = new MimeMessageHelper(me,true,"utf-8");
         helper.setTo(to);
         helper.setFrom("发件人邮箱");
         helper.setSubject(title);
         helper.setText(html,isHtml);
         
         if(filepaths != null && filepaths.length>0){
            for(String path:filepaths){
               File file = new File(path);
               helper.addAttachment(MimeUtility.encodeWord(file.getName()), file);
               
            }
         }
         
         mailSender.send(helper.getMimeMessage());
         System.out.println("发送成功!");
      } catch (Exception e) {
         // TODO Auto-generated catch block
         throw new RuntimeException("发送邮件错误"+e.getMessage(),e);
      }

   }
}

四、测试类

public class Test{

@Autowired
private EmailUtil emailUtil;

 

//发送普通文本邮件
public void sendMessage(){

//(收件人,标题,邮件内容)
     emailUtil.sendEmail("收件人邮箱", "每日会员报表", "报表内容");
    }

//发送文件邮件
public void sendFile(){
    //(收件人,标题,邮件内容,文件名数组,是否是html格式)
    emailUtil.sendEmail("收件人邮箱", "每日会员报表", "请查看每日报表", new String[]{"D:/report.xls"},true);
}

 

}

 

 

 

 

 

你可能感兴趣的:(Java发送文本邮件及附件邮件,QQ邮箱POP3与SMTP授权码(springframework+javax.mail))