发送邮件到腾讯企业邮箱
目录
引言:
了解相关概念:
准备开发:
(2)我们需要引入一个Jar包
(3)编写Controller
(4)编写业务逻辑类
(5)工具类
(6)测试
参考文章:
引言:
最近有一个需求需要用到发送邮箱功能,使用的邮箱不一样,自然邮箱的地址也不一样。
下面这篇文章我将简单的描述一下如何发送邮件到企业邮箱。接下来就让我们一起看这个功能如何实现吧!
了解相关概念:
1.我现在用的是腾讯企业邮箱,那么要发送到腾讯企业邮箱上的话,我们首先要有腾讯企业邮箱。腾讯企业邮箱的地址是 smtp.exmail.qq.com
其中可以注意到smtp 这是邮箱的传输协议,
发送邮件用的是SMTP 协议,(处理用户邮箱发送请求的服务器我们通常称之为邮件发送服务器)。
接收邮件用的是POP3协议,(处理用户邮件接收请求的服务器我们称之为邮件接收服务器) 。
准备开发:
(1)我们需要准备一个腾讯企业邮箱的用户名和密码 ,并且需要一个接收方邮箱号。
整个流程如下:
(2)我们需要引入一个Jar包
这是一个为方便Java 开发人员在应用程序中实现邮件发送和接收功能而提供的一套标准开发包,它支持一些常用的邮件协议 比如上面将的SMTP ,POP3 等。
<!-- 发送邮件的maven --><dependency><groupId>javax.mail</groupId><artifactId>mail</artifactId><version>1.4.7</version></dependency>
(3)编写Controller
package com.test.sendmail;import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;/*** @Author tanghh* @Date 2020/4/17 10:05*/
@RestController
public class SendEmailController {@Autowiredprivate SendEmailService sendEmailService;@ApiOperation(value = "实现企业邮箱发送邮件")@GetMapping(value = "/sendEmail")public void sendEmailToCompanyEmail(){//发送邮件到腾讯企业邮箱sendEmailService.sendEmail();}
}
(4)编写业务逻辑类
package com.test.sendmail;import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Date;/*** @Author tanghh* @Date 2020/4/17 10:06*/
@Service
public class SendEmailServiceImpl implements SendEmailService {private Logger logger = LoggerFactory.getLogger(SendEmailServiceImpl.class);@Overridepublic void sendEmail() {try{//收件人腾讯企业邮箱账号String receiveAccount = "接收方邮箱账号号";//发送方账号密码String userName = "发送方邮箱账号";String password = "发送方邮箱密码";//邮箱主题String theme = "企业邮箱主题";//邮件内容String content = "企业邮箱发送内容";//获取Session对象Session session = Session.getDefaultInstance(SendMailUtil.setTencentExEmail(),new Authenticator() {//此访求返回用户和密码的对象@Overrideprotected PasswordAuthentication getPasswordAuthentication() {PasswordAuthentication pa = new PasswordAuthentication(userName, password);return pa;}});// for (int i = 0; i <2 ; i++) {// 有循环的情况下,实现单独发送的功能 收件人方只显示自己的邮箱MimeMessage mimeMessage = new MimeMessage(session);mimeMessage.setFrom(new InternetAddress(userName, userName));mimeMessage.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveAccount));//设置主题mimeMessage.setSubject(theme);mimeMessage.setSentDate(new Date());//设置内容mimeMessage.setText(content);mimeMessage.saveChanges();try {//具体发送邮件的方法Transport.send(mimeMessage);} catch (MessagingException e) {logger.error(e.getMessage());
// continue;}
// }}catch (Exception e){logger.error("发送邮箱失败",e);}}
}
(5)工具类
package com.test.sendmail;import com.sun.mail.util.MailSSLSocketFactory;import java.security.GeneralSecurityException;
import java.util.Properties;/*** @Author tanghh* @Date 2020/4/17 10:08*/
public class SendMailUtil {/*** 邮箱协议*/private static String MAIL_TRANSPORT_PROTOCOL = "smtp";/*** 发件服务器地址(以下是腾讯企业邮箱)*/private static String MAIL_SMTP_HOST = "smtp.exmail.qq.com";/*** 端口*/private static String MAIL_SMTP_PORT = "465";/*** 使用smtp身份验证*/private static String MAIL_SMTP_AUTH = "true";/*** 邮箱配置*/public static Properties setTencentExEmail (){Properties prop = new Properties();//协议prop.setProperty("mail.transport.protocol", MAIL_TRANSPORT_PROTOCOL);//服务器prop.setProperty("mail.smtp.host", MAIL_SMTP_HOST);//端口prop.setProperty("mail.smtp.port", MAIL_SMTP_PORT);//使用smtp身份验证prop.setProperty("mail.smtp.auth", MAIL_SMTP_AUTH);//开启安全协议 使用SSL,企业邮箱必需!MailSSLSocketFactory sf = null;try {sf = new MailSSLSocketFactory();sf.setTrustAllHosts(true);} catch (GeneralSecurityException e1) {e1.printStackTrace();}prop.put("mail.smtp.ssl.enable", "true");prop.put("mail.smtp.ssl.socketFactory", sf);return prop;}
}
(6)测试
在浏览器上访问:http://localhost:8006/sendEmail
如果需要发送多个邮件的话,可以将我注释的for循环放开。
参考文章:
https://blog.csdn.net/qq_41151659/article/details/96475739
如果觉得小编写的不错的话,可以给小编一个赞喔,
发送邮件到腾讯企业邮箱相关推荐
- python程序下载腾讯企业邮箱附件_python 发送邮件 QQ+腾讯企业邮箱
一.使用QQ邮箱或者腾讯企业邮箱 python 发送邮件属于网络编程方向的,在工作中,我需要经常用邮件来检测我的程序运行状况.使用起来十分方便,这里我就用腾讯企业邮箱作为我的收发邮箱来使用. 使用py ...
- 阿里云ECS发送邮件到腾讯企业邮箱
1.安装软件 yum install mailx yum install sendmail 2.配置 vim /etc/mail.rc在文件最后加上如下内容: set smtp=smtps://smt ...
- Springboot发送邮件到腾讯企业邮箱
一.pom <!--email--><dependency><groupId>org.springframework.boot</groupId>< ...
- qq收不到oracle邮件,腾讯企业邮箱发送邮件失败
一直使用的腾讯企业邮箱,但是最近发现发送邮件全是失败的,提示: javax.mail.MessagingException: Could not connect to SMTP host: smtp. ...
- Java + 腾讯企业邮箱 + javamail + SSL 发送邮件(转载:http://www.cnblogs.com/LUA123/p/5575134.html)
说实话腾讯的企业邮箱真心不错! 腾讯企业邮箱官网:http://exmail.qq.com/login/ 新用户注册:http://exmail.qq.com/onlinesell/intro 点击开 ...
- Java腾讯企业邮箱发送邮件
##Java通过腾讯企业邮箱发送邮件(多人发送) 企业邮箱需要使用ssl private static String account = "企业邮箱账户";// 登录账户priva ...
- python程序下载腾讯企业邮箱附件_python3使用腾讯企业邮箱发送邮件的实例
1.腾讯企业邮箱SMTP服务器地址:smtp.exmail.qq.com,ssl端口为:465 2.确保腾讯企业邮箱中开启了SMTP服务: 3.示例代码: #!/usr/bin/env python3 ...
- 向腾讯企业邮箱发送邮件失败,报错: 559
代码 : springboot 搬砖级 发送邮件代码 发送方: 阿里云邮件推送服务 接收方: 腾讯企业邮箱 ---------------------------------------------- ...
- python 实现腾讯企业邮箱发送邮件
import smtplib from email.mime.text import MIMEText from email.utils import formataddr import traceb ...
- java发送QQ群邮件,Java使用腾讯企业邮箱 、javamail 、 SSL 发送邮件/群发
引入相关依赖: javax.mail mail 1.4.7 commons-io commons-io 2.4 如果使用spring的发邮件,还需要添加依赖: org.springframework ...
最新文章
- 设计模式学习(一)——策略模式
- 【转】每天一个linux命令(50):crontab命令
- python获取列表的最后一个元素
- java cancel_Java RunnableFuture.cancel方法代码示例
- 从零开始学习docker(五)网络的另外两种类型host,none
- python输出列/行内容显示不全
- 用编码实现gridview的创建!(一)(转www.asp.net)
- android t跳转到fragment,Android 使用EventBus进行Fragment和Activity通信
- 深度相机(三)--三种方案对比
- python to datetime_Python中缺少datetime.timedelta.to_seconds()-float?
- ArcGIS多面体(multipatch)解析——引
- C#学生管理系统源代码
- mac安装mysql(命令行方式)
- SAM2695 带效果器的低功耗单芯片合成器
- 今年最热门的十大编程语言排行榜新鲜出炉,第一名着实出乎意料
- TTL与非门电路的工作原理
- 入秋的第一篇数据结构算法:看看归并与快排的风采,三面蚂蚁金服成功拿到offer
- HTML5期末大作业:漫画网站设计——海贼王我当定了(6页) web前端期末大作业 html+css+javascript网页设计实例 企业网站制作 计算机毕设网页设计源码
- 【FXCG】什么是风险?
- 两个定律,给你找了一个换手机的借口
热门文章
- Java爬堆糖图片爬虫
- 堆糖生活家喜欢的图片批量下载
- 如何看待360与腾讯之争
- tensorflow2.X tf.data.Dataset详解
- BVH with SAH (Bounding Volume Hierarchy with Surface Area Heuristic)
- [软件更新]Pidgin 2.5.8_IM 即时通信
- u3d计算机获取键盘输入,Unity 中的键盘输入
- App如何生成下载二维码?扫码即可下载App
- python前面三个大于号是啥_JavaScript 无符号位移运算符 三个大于号 的使用方法...
- 易到要在网约车市场突围并不容易