Spring Java 发送各种邮件

本文详细介绍Spring框架中的邮件发送功能,包括配置与使用JavaMailSender发送简单邮件、带图片的HTML邮件及带附件邮件的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.youkuaiyun.com/jiangjunshow

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

                Spring邮件抽象层的主要包为 org.springframework.mail 。它包括了发送电子邮件的主要接口 MailSender ,和 值对象 SimpleMailMessage ,它封装了简单邮件的属性如 fromto , ccsubject , text 。 包里还包含一棵以 MailException 为根的checked Exception继承树,它们提供了对底层邮件系统异常的高级别抽象。 要获得关于邮件异常层次的更丰富的信息,请参考Javadocs。

为了使用JavaMail中的一些特色, 比如MIME类型的信件, Spring提供了MailSender的一个子接口, 即org.springframework.mail.javamail.JavaMailSender。Spring还提供了一个回调接口org.springframework.mail.javamail.MimeMessagePreparator, 用于准备JavaMail的MIME信件。

这里简单的介绍了如何使用spring发送各种形式的邮件以及配置。

1、在src目录下建立mail.properties文件里边包含一下内容

mail.host=smtp.exmail.qq.com(建议使用这个,如果使用smtp.qq.com可能会出现安全认证的问题
mail.username=你的邮箱名
mail.password=你的邮箱密码
mail.from=发送方的邮箱

2、使用spring配置

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

           
<?xml version="1.0" encoding="UTF-8"?>
<beans
  xmlns= "http://www.springframework.org/schema/beans"
  xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance"
  xmlns:p= "http://www.springframework.org/schema/p"
  xsi:schemaLocation= "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
<!-- 加载Properties文件 --> 
    <bean id= "configurer" class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" > 
        <property name= "locations" > 
            <list> 
                <value>classpath:mail.properties </value> 
            </list> 
        </property> 
    </bean> 
    <bean id= "mailMessage" class= "org.springframework.mail.SimpleMailMessage" > 
        <property name= "from" > 
            <value>${mail.from} </value> 
        </property> 
        <!-- 查看SimpleMailMessage源码还可以注入标题,内容等 --> 
    </bean> 
    <!-- 申明JavaMailSenderImpl对象 --> 
    <bean id= "mailSender" class= "org.springframework.mail.javamail.JavaMailSenderImpl" > 
        <property name= "defaultEncoding" value= "UTF-8" /> 
        <property name= "host" value= "${mail.host}" /> 
        <property name= "username" value= "${mail.username}" /> 
        <property name= "password" value= "${mail.password}" /> 
        <property name= "javaMailProperties" > 
            <props> 
                <!-- 设置认证开关 --> 
                <prop key= "mail.smtp.auth" >true </prop> 
                <!-- 启动调试开关 --> 
                <prop key= "mail.debug" >true </prop> 
                <!-- 设置发送延时 -->
                <prop key= "mail.smtp.timeout" >0 </prop>
            </props> 
        </property> 
    </bean> 
</beans>
 来自CODE的代码片
Spring_mail_applicationContext.xml
3、发送简单的邮件

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

           
package mail ;
import javax.mail.MessagingException ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.support.ClassPathXmlApplicationContext ;
import org.springframework.mail.MailSender ;
import org.springframework.mail.SimpleMailMessage ;
/**
* 本类测试邮件发送Html形式
*
* @author Eternity_._
*
*/
public class SingleMailSend {
  static ApplicationContext actx = new ClassPathXmlApplicationContext (
    "applicationContext.xml" );
  static MailSender sender = ( MailSender ) actx . getBean ( "mailSender" );
  static SimpleMailMessage mailMessage = ( SimpleMailMessage ) actx . getBean ( "mailMessage" );
  public static void main ( String args []) throws MessagingException {  
   mailMessage . setSubject ( "你好" );
   mailMessage . setText ( "这个是一个通过Spring框架来发送邮件的小程序" );
   mailMessage . setTo ( "9197****1@qq.com" );
   sender . send ( mailMessage );
  }
}
 来自CODE的代码片
Spring_mail_SingleMail.java
4、发送带有图片的邮件,以嵌入HTML的方式

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

           
package springmail ;
import java.io.File ;
import javax.mail.MessagingException ;
import javax.mail.internet.MimeMessage ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.support.ClassPathXmlApplicationContext ;
import org.springframework.core.io.FileSystemResource ;
import org.springframework.mail.javamail.JavaMailSenderImpl ;
import org.springframework.mail.javamail.MimeMessageHelper ;
public class SpringAttachedImageMail {
  public static void main ( String [] args ) throws MessagingException {
   ApplicationContext ctx = new ClassPathXmlApplicationContext (
     "applicationContext.xml" );
   JavaMailSenderImpl sender = ( JavaMailSenderImpl ) ctx
     . getBean ( "mailSender" );
   MimeMessage mailMessage = sender . createMimeMessage ();
   MimeMessageHelper messageHelper = new MimeMessageHelper ( mailMessage , true );
   messageHelper . setFrom ( "9197****1@qq.com" );
   messageHelper . setTo ( "9197****1@qq.com" );
   messageHelper . setSubject ( "测试邮件中嵌套图片!!" );
   // true 表示启动HTML格式的邮件
   messageHelper . setText (
     "<html><head></head><body><h1>hello!!spring image html mail</h1>"
      + "<a href=http://www.baidu.com>baidu</a>"  + "<img src=cid:image/></body></html>" , true );
   FileSystemResource img = new FileSystemResource ( new File ( "单.png" ));
   messageHelper . addInline ( "image" , img ); //跟cid一致
   sender . send ( mailMessage );
   System . out . println ( "邮件发送成功..." );
  }
}
 来自CODE的代码片
Spring_mail_AttachedFileMail.java
5、发送带有附件的邮件

  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41

           
package springmail ;
import java.io.File ;
import javax.mail.MessagingException ;
import javax.mail.internet.MimeMessage ;
import org.springframework.context.ApplicationContext ;
import org.springframework.context.support.ClassPathXmlApplicationContext ;
import org.springframework.core.io.FileSystemResource ;
import org.springframework.mail.javamail.JavaMailSenderImpl ;
import org.springframework.mail.javamail.MimeMessageHelper ;
public class SpringAttachedImageMail {
  public static void main ( String [] args ) throws MessagingException {
   ApplicationContext ctx = new ClassPathXmlApplicationContext (
     "applicationContext.xml" );
   JavaMailSenderImpl sender = ( JavaMailSenderImpl ) ctx
     . getBean ( "mailSender" );
   MimeMessage mailMessage = sender . createMimeMessage ();
   MimeMessageHelper messageHelper = new MimeMessageHelper ( mailMessage , true );
   messageHelper . setFrom ( "9197****1@qq.com" );
   messageHelper . setTo ( "9197****1@qq.com" );
   messageHelper . setSubject ( "测试邮件中嵌套图片!!" );
   // true 表示启动HTML格式的邮件
   messageHelper . setText (
     "<html><head></head><body><h1>hello!!spring image html mail</h1>"
      + "<a href=http://www.baidu.com>baidu</a>"  + "<img src=cid:image/></body></html>" , true );
   FileSystemResource img = new FileSystemResource ( new File ( "单.png" ));
   messageHelper . addAttachment ( "单.png" , file ); //添加到附件
   sender . send ( mailMessage );
   System . out . println ( "邮件发送成功..." );
  }
}
 来自CODE的代码片
Spring_mail_AttachedFileMail.java

6、如果使用的是QQ邮箱的话建议把这几项都选上,不然可能调试不能通过。



附:所需的jar:http://download.youkuaiyun.com/detail/eternity0_0/7179617

           

给我老师的人工智能教程打call!http://blog.youkuaiyun.com/jiangjunshow
这里写图片描述
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值