SSM发邮件Util

本文介绍了如何在Spring Boot应用中利用@Autowired注解自动配置JavaMailSender,演示了发送简单邮件和带附件的邮件过程。通过`applicationContext-mail.xml`配置文件,展示了SMTP配置和邮件发送的详细步骤。
摘要由CSDN通过智能技术生成

使用javax.mail.jar发送邮件,一般都自带有。

spring的包扫描需要扫到@Component注解

package com.lin.mybook.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.Date;

/**
 * @author @全体人员
 * @nickname Tim
 * @date 2022-09-15 09:05:58
 */
@Component
public final class EmailUtil {
    private EmailUtil(){}

    @Autowired
    private JavaMailSender javaMailSender;

    public  boolean sendEmail(
            Object to,
            String subject,
            String content
    ){
        try {
            SimpleMailMessage m=new SimpleMailMessage();
            m.setFrom(((JavaMailSenderImpl)javaMailSender).getUsername());
            if(to.getClass().isArray()){
                String[] tos= (String[]) to;
                m.setTo(tos);
            }else{
                String tos= (String) to;
                m.setTo(tos);
            }
            m.setSubject(subject);
            m.setText(content);
            javaMailSender.send(m);
        }catch (Exception e){
            e.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean sentEmailEnclosure(
            Object to,
            String subject,
            String content,
            File[] files,
            boolean isHtml
    ) {
        try{
            MimeMessage mi=javaMailSender.createMimeMessage();
            MimeMessageHelper helper=new MimeMessageHelper(mi,true,"utf-8");
            helper.setFrom(((JavaMailSenderImpl)javaMailSender).getUsername());
            if(to.getClass().isArray()){
                String[] tos= (String[]) to;
                helper.setTo(tos);
            }else{
                String tos= (String) to;
                helper.setTo(tos);
            }
            helper.setSubject(subject);
            helper.setText(content,isHtml);
            helper.setSentDate(new Date(System.currentTimeMillis()+6000*1000));
            if(files!=null){
                for(File file:files){
                    helper.addAttachment(file.getName(),file);
                }
            }
            javaMailSender.send(mi);
        } catch (MessagingException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}

 

在主文件application.xml文件中使用下面语句导入其他的xml文件
<!--导入其他xml-->
<import resource="applicationContext-mail.xml"></import>
<?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" />
		<property name="port" value="587" />
		<property name="username" value="2574995172@qq.com" />
		<property name="password" value="xxxxxxxxxxx" />
		<property name="protocol" value="smtp" />
		<property name="defaultEncoding" value="utf-8" />
		<property name="javaMailProperties">
			<props>
				<prop key="mail.smtp.auth">true</prop>
			</props>
		</property>
	</bean>
 </beans>

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值