ssm邮件发送

SSM 发送邮件

1.application.properties

#服务器主机名 smtp.xx.com
mail.smtp.host=smtp.163.com
mail.smtp.username=xxxxx@163.com
#密码/客户端授权码
mail.smtp.password=xxxxxx
#编码字符
mail.smtp.defaultEncoding=utf-8
#是否进行用户名密码校验
mail.smtp.auth=true
#设置超时时间
mail.smtp.timeout=20000

2.applicationContext.xml
<bean id="javaMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl"> <property name="host" value="${mail.smtp.host}"/> <property name="username" value="${mail.smtp.username}"/> <property name="password" value="${mail.smtp.password}"/> <property name="defaultEncoding" value="${mail.smtp.defaultEncoding}"/> <property name="javaMailProperties"> <props> <prop key="mail.smtp.auth">${mail.smtp.auth}</prop> <prop key="mail.smtp.timeout">${mail.smtp.timeout}</prop> </props> </property> </bean>

3.邮件发送类
/**

  • @author wy
    *邮件发送
    */
    @Service
    public class MailSendService {
    @Autowired
    private JavaMailSender javaMailSender;
    /*普通格式发送
    * @recipient 收件人地址
    * @subject 主题
    * @content 正文
    * */
    public String sendMail(String recipient,String topic,String content){
    String mailAddress=PropertyUtil.getProperty(“mail.smtp.username”);
    MimeMessage mimeMessage = javaMailSender.createMimeMessage();
    try {
    MimeMessageHelper messageHelper = new MimeMessageHelper(mimeMessage,true,“UTF-8”);
    messageHelper.setFrom(mailAddress);//发件人
    messageHelper.setTo(recipient);
    messageHelper.setSubject(topic);
    messageHelper.setText(content,true);//true代表支持html格式
    javaMailSender.send(mimeMessage);
    } catch (MessagingException e) {
    e.printStackTrace();
    }
    return “true”;
    }
    }

4.读取application.properties 文件内容 工具类

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.Properties;

public class PropertyUtil {
private static final Logger logger = LoggerFactory.getLogger(PropertyUtil.class);
private static Properties props;
static{
loadProps();
}

synchronized static private void loadProps(){
    props = new Properties();
    InputStream in = null;
    try {
        //in = PropertyUtil.class.getClassLoader().getResourceAsStream("param.properties");
        in = PropertyUtil.class.getResourceAsStream("/application.properties");
        props.load(in);
    } catch (FileNotFoundException e) {
        logger.error("application.properties文件未找到");
    } catch (IOException e) {
        logger.error("出现IOException");
    } finally {
        try {
            if(null != in) {
                in.close();
            }
        } catch (IOException e) {
            logger.error("application.properties文件流关闭出现异常");
        }
    }

}

public static String getProperty(String key){
    if(null == props) {
        loadProps();
    }
    return props.getProperty(key);
}

public static String getProperty(String key, String defaultValue) {
    if(null == props) {
        loadProps();
    }
    return props.getProperty(key, defaultValue);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值