java spring mail_使用spring的JavaMail发送邮件

以前我们使用JavaMail发送邮件,步骤挺多的。现在的项目跟Spring整合的比较多。所以这里主要谈谈SpringMail发送。

导入jar包。

配置applicationContext-email.xml。

编写代码。

Maven地址如下

org.springframework

spring-context-support

4.2.6.RELEASE

javax.mail

mail

1.4.5

配置applicationContext-email.xml

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd

http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd

http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

//

true

true

25000

在这里,要注意一下:

个人邮箱使用的主机是:

-->

企业邮箱使用的主机是:

关于qq邮箱的授权码,如果是企业邮箱,则使用登录密码,否则要去获取授权码,要不然会报异常

Exception in thread "main" org.springframework.mail.MailAuthenticationException: Authentication failed; nested exception is javax.mail.AuthenticationFailedException

当然,我们可以使用163邮箱发送,步骤也是一样,要去申请授权码,只是有点奇怪的是,163发送会被当成垃圾邮箱处理。所以这里建议不要使用163邮箱发送邮件。

详细代码如下

public class EmailUtil {

public void sendMsg() {

ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml",

"applicationContext-email.xml");

MailSender ms = (MailSender) ac.getBean("mailSender");

SimpleMailMessage smm = new SimpleMailMessage();

// 发送

smm.setFrom("11--@qq.com");//此处省略邮箱

// 接收

smm.setTo("18----@163.com");

// 主题

smm.setSubject("库存预警" + System.currentTimeMillis());

// 内容

smm.setText("库存预警");

//

smm.setSentDate(new Date());

ms.send(smm);

System.out.println("end");

}

public static void main(String[] args) {

EmailUtil emailUtil = new EmailUtil();

emailUtil.sendMsg();

}

}

发送成功截图如下:

d95e89772c495e5de7245b13135acac9.png

好了,Spring整合email到此结束。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值