SSM 整合框架实现发送邮件功能
1.导⼊发送邮件的依赖
ssm框架实现登录功能2.发送邮件的账号开启POP3/SMTP/IMAP/SMIP
服务
l 配置⽂件中添加发送邮件所需的配置信息
<!-- 发送邮件jar 包-->
<!--spring ⽀持-->
<dependency >
<groupId >org.springframework </groupId >
<artifactId >spring-context-support </artifactId >
<version >5.0.0.RELEASE </version >
</dependency >
<!--邮件发送-->
<dependency >
<groupId >com.sun.mail </groupId >
<artifactId >javax.mail </artifactId >
<version >1.6.1</version >
</dependency >
<!--邮件配置-->
<context:property-placeholder location ="classpath:email.properties" ignore-unresolvable ="true"/>
<!--配置邮件接⼝-->
<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>
4.创建email.properties 接上配置邮件接⼝
#服务器主机名
mail.smtp.host=smtp.qq
mail.smtp.username=开启服务的邮箱号@qq
#密码/客户端授权码
mail.smtp.password=开启服务的邮箱号的密钥
#编码字符
mail.smtp.defaultEncoding=utf-8
#是否进⾏⽤户名密码校验
mail.smtp.auth=true
#设置超时时间
mail.smtp.timeout=20000
5.在控制层测试
package ller;
import org.springframework.beans.factory.annotation.Autowired;
import io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.io.IOException;
import java.util.Properties;
/
**
* 测试邮件发送controller
*/
@Controller public class SendMailController {
@Autowired
private JavaMailSender javaMailSender;//在spring中配置的邮件发送的bean
@RequestMapping("sendMailTest")
public Object sendMailTest(){
MimeMessage ateMimeMessage();//创建邮件对象
MimeMessageHelper mMessageHelper;
Properties prop = new Properties();
String from;
try {
//从配置⽂件中拿到发件⼈邮箱地址
prop.Class().getResourceAsStream("/email.properties"));
from = ("mail.smtp.username")+"";
mMessageHelper=new MimeMessageHelper(mMessage,true);
mMessageHelper.setFrom(from);//发件⼈邮箱
mMessageHelper.setTo("收件⼈邮箱号@qq");//收件⼈邮箱
mMessageHelper.setSubject("ssm框架测试邮件发送");//邮件的主题
mMessageHelper.setText("<p>这是使⽤spring,springmvc,mybatis整合框架的邮件功能发送的⼀封邮件,测试</p>",true);//邮件的⽂本内容,true表⽰⽂本以html格式打开
File file=new File("C:\\Users\\lcl\\Pictures\\Saved Pictures\\blog.csdn_Mr__Viking_article_details_81090046.png");//在邮件中添加⼀张图⽚ FileSystemResource resource=new FileSystemResource(file);
//mMessageHelper.addInline("fengye", resource);//这⾥指定⼀个id,在上⾯引⽤
//mMessageHelper.addAttachment("QQ截图20200721221932.png", resource);//在邮件中添加⼀个附件
javaMailSender.send(mMessage);//发送邮件
} catch (MessagingException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
--》启动项⽬ --》输⼊地址测试
6.
效果图
return "发送成功";
}
}
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论