使用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>