好了,基本原理和业务场景搞清楚了,下来以qq邮箱作为案例(163等其他邮箱也是类似的),基于ssm框架的,springboot同理,当然ssm框架都会配置了springboot还不是手到擒来
点击设置—— 账户
请记住这串编号,后面的配置会用到
<!--邮件发送--> <dependency> <groupid>com.sun.mail</groupid> <artifactid>javax.mail</artifactid> <version>1.6.1</version> </dependency> <!--引入spring的上下文jar--> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context</artifactid> <version>4.3.22.relea</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-context-support --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-context-support</artifactid> <version>4.3.4.relea</version> </dependency> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-webmvc</artifactid> <version>4.3.4.relea</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-tx --> <dependency> <groupid>org.springframework</groupid> <artifactid>spring-tx</artifactid> <version&stay with my heartgt;4.3.13.relea</version> </dependency> <dependency> <groupid>junit</groupid> <artifactid>junit</artifactid> <version>4.12</version> </dependency>
在163邮箱中同上的申请配置,可能和qq页面所在位置不一样,请自行查找(基本都是一样的)
#服务器主机名qq邮箱 smtp.xx.com 根据自己邮箱的使用自行设置 163邮箱: smtp.163.commail.smtp.host=smtp.qq.com#自己的邮箱mail.smtp.urname=********@qq.com#密码/客户端授权码 这里的授权码就是刚才在邮箱中生成的mail.smtp.password=******** #编码字符mai腊八粥的材料l.smtp.defaultencoding=utf-8#是否进行用户名密码校验mail.smtp.auth=true#设置超时时间mail.smtp.timeout=20000
spring-core.xml
<?xml version="1.0" encoding="utf-8"?><beans xmlns="/d/file/titlepic/" xmlns:xsi="/d/file/titlepic/xmlschema-instance" xmlns:context="http://www.springframework.org/sche茉莉花的诗句ma/context" xmlns:mvc="/d/file/titlepic/" xsi:schemalocation="http://www.springframework.org/schema/beans /d/file/titlepic/spring-beans.xsd /d/file/titlepic/ /d/file/titlepic//spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!--读取属性文件--> <context:property-placeholder location="classpath:mail.properties"/> <!--配置邮件接口--> <bean id="javamailnder" class="org.springframework.mail.javamail.javamailnderimpl"> <property name="host" value="${mail.smtp.host}"/> <property name="urname" value="${mail.smtp.urname}"/> <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> <context:component-scan ba-package="com.*"> <context:include-filter type="annotation" expression="org.springframework.stereotype.controller"/> </context:component-scan></beans>
import org.springframework.beans.factory.annotation.autowired;import org.springframework.core.io.filesystemresource;import org.springframework.mail.javamail.javamailnder;import org.springframework.mail.javamail.mimemessagehelper;import org.springframework.stereotype.con英文文章troller;import org.springframework.web.bind.annotation.*;import javax.mail.messagingexception;import javax.mail.internet.internetaddress;import javax.mail.internet.mimemessage;import java.io.file;import java.io.ioexception;import java.util.properties;/** * @description: 发送邮件 * @author: 张楚涵 * @date: 2019/8/14 0014 15:58 * @version:1.0.0 */@restcontrollerpublic class nemailcontroller { @autowired private javamailnder javamailnder;//在spring中配置的邮件发送的bean @requestmapping(value = "/nd",method = requestmethod.get,produces = "text/html; chart=utf-8") public object ndmail03(){ mimemessage mmessage=javamailnder.createmimemessage();//创建邮件对象 mimemessagehelper mmessagehelper; properties prop = new properties(); string from; try { //从配置文件中拿到发件人邮箱地址 //根据自己的目录设置 prop.load(this.getclass().getclassloader().getresourceasstream("mail.properties")); from = prop.get("mail.smtp.urname")+""; mmessagehelper=new mimemessagehelper(mmessage,true); // mmessagehelper.tfrom(from);//发件人邮箱 // 第二个参数是你想发送邮件时想用的名字 mmessagehelper.tfrom(new internetaddress(from, "###", "utf-8")); mmessagehelper.tto("*******@qq.com");//收件人邮箱 mmessagehelper.tsubject("******");//邮件的主题 mmessagehelper.tsubject("spring的邮件发送");//邮件的主题 //邮件的文本内容,true表示文本以html格式打开 mmessagehelper.ttext("<p>这是使用spring的邮件功能发送的一封邮件</p><br/>" + "<a href='https://blog.csdn.net/qq_41840847'>打开我的博客主页</a><br/>" + "<img src='cid:fengye'>",true); /* file file=new file("f:/img/mr.png");//在邮件中添加一张图片 filesystemresource resource=new filesystemresource(file); mmessagehelper.addinline("fengye", resource);//这里指定一个id,在上面引用 mmessagehelper.addattachment("mr.png", resource);//在邮件中添加一个附件 */ javamailnder.nd(mmessage);//发送邮件 } catch (messagingexception e) { e.printstacktrace(); return "发送失败"; } catch (ioexception e) { e.printstacktrace(); } return "发送成功"; }}
本文发布于:2023-04-05 10:40:39,感谢您对本站的认可!
本文链接:https://www.wtabcd.cn/fanwen/zuowen/6702d64fb708e747fa533fd6afc62aa6.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文word下载地址:qq邮箱电脑版在哪里(电脑qq邮箱默认下载路径).doc
本文 PDF 下载地址:qq邮箱电脑版在哪里(电脑qq邮箱默认下载路径).pdf
留言与评论(共有 0 条评论) |