一、利用JavaMail发送邮件案例:

1、maven项目结构:

2、先在pom.xml里边加入Javamail依赖,系统会根据坐标自动下载mail包(前提是配置好了maven):

3、配置email.properties属性文件,主要是为了不更改代码的前提下,该改变发送邮件的一些基本信息:

4、实现发送邮件的主体类SendMailUtils,代码下:

package top.hzelin.util;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties; import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart; public class SendMailUtils {
private static String from = "";
private static String user = "";
private static String password = "";
/*
* 读取属性文件的内容,并为上面上个属性赋初始值
*/
static {
Properties prop = new Properties();
InputStream is = SendMailUtils.class.getClassLoader().getResourceAsStream("email.properties");
try {
prop.load(is);
from = prop.getProperty("from");
user=prop.getProperty("username");
password=prop.getProperty("password");
} catch (IOException e) {
e.printStackTrace();
}
}
public static void sendMail(String to,String text,String title) {
Properties props = new Properties();
props.setProperty("mail.smtp.host", "smtp.163.com");//设置邮件服务器主机名
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.smtp.auth", "true");//发送服务器需要身份验证
Session session = Session.getDefaultInstance(props);//设置环境信息
session.setDebug(true);
MimeMessage message = new MimeMessage(session);
Multipart multipart = null;
BodyPart contentPart = null;
Transport transport = null;
try {
message.setFrom(from);//设置发件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject(title);
multipart = new MimeMultipart();//设置附件
contentPart = new MimeBodyPart();
contentPart.setContent(text, "text/html;charset=utf-8");
multipart.addBodyPart(contentPart);
message.setContent(multipart);
message.saveChanges();
transport = session.getTransport("smtp");
transport.connect("smtp.163.com", user, password);
transport.sendMessage(message, message.getAllRecipients());
} catch (MessagingException e) { e.printStackTrace();
}finally {
try {
transport.close();
} catch (MessagingException e) {
e.printStackTrace();
}
} } }

5、测试发送邮件功能是否可用SendEmailTest:

注意:email.properties配置文件中的密码应该是客户端授权码,不是登录密码,设置位置如下:

利用JavaMail发送邮件:smtp.163.com的更多相关文章

  1. express利用nodemailer发送邮件(163邮箱)

    Nodemailer 是一个简单易用的Node.js邮件发送组件 首先安装这个组件 npm install nodemailer --save 安装之后,可以在某个get请求下,发送邮件,具体路由代码 ...

  2. spring利用javamail,quartz定时发送邮件 <转>

    原文地址:spring利用javamail,quartz定时发送邮件 <转>作者:物是人非 spring提供的定时发送邮件功能,下面是一个简单的例子以供大家参考,首先从spring配置文件 ...

  3. javamail发送邮件,支持yahoo,google,163.com,qq.com邮件发送

    https://www.iteye.com/blog/fangyunfeng-1847352 https://blog.csdn.net/weixin_38465623/article/details ...

  4. java利用commons-email发送邮件并进行封装

    本例中利用commons-email发送邮件并进行封装,支持html内容和附件:Commons Email是Apache的Commons子项目下的一个邮件客户端组件,它是基于JavaMail的,大大简 ...

  5. JavaMail(二):利用JavaMail发送复杂邮件

    上一篇文章我们学习了利用JavaMail发送简单邮件,这篇文章我们利用JavaMail发送稍微复杂一点的邮件(包含文本.图片.附件).这里只贴出核心代码,其余代码可参考JavaMail(一):利用Ja ...

  6. JavaMail发送邮件第一版

    首先,我们先来了解一个基本的知识点,用什么工具来发邮件? 简单的说一下,目前用的比较多的客户端:OutLook,Foxmail等 顺便了解一下POP3.SMTP协议的区别: POP3,全名为" ...

  7. java中使用javamail发送邮件

    1. 电子邮件协议 电子邮件的在网络中传输和网页一样需要遵从特定的协议,常用的电子邮件协议包括 SMTP,POP3,IMAP. 其中邮件的创建和发送只需要用到 SMTP协议,所有本文也只会涉及到SMT ...

  8. JavaMail发送邮件的笔记及Demo

    最近碰到一个需求,就是注册用户时候需要向用户发送激活邮箱,于是照着网上搜来的demo自己试着运行了一下,发件时我用的是网易163邮箱,收件时用QQ邮箱,运行后报了一个错误: 网络上搜索解决方式,多次尝 ...

  9. 利用SSIS发送邮件

    璎Nicole珞 博客园 闪存 首页 新随笔 联系 管理 订阅 随笔- 15  文章- 0  评论- 0  SSIS 利用发送邮件服务 Send Email Task   1. 在SSIS中如何发送邮 ...

随机推荐

  1. Lerning Entity Framework 6 ------ Defining the Database Structure

    There are three ways to define the database structure by Entity Framework API. They are: Attributes ...

  2. 日期时间类:Date,Calendar,计算类:Math

    日期时间类 计算机如何表示时间? 时间戳(timestamp):距离特定时间的时间间隔. 计算机时间戳是指距离历元(1970-01-01 00:00:00:000)的时间间隔(ms). 计算机中时间2 ...

  3. Maximum repetition substring(POJ - 3693)(sa(后缀数组)+st表)

    The repetition number of a string is defined as the maximum number \(R\) such that the string can be ...

  4. Swift5 语言指南(二十六) 内存安全

    默认情况下,Swift可以防止代码中发生不安全行为.例如,Swift确保变量在使用之前进行初始化,在取消分配后不访问内存,并检查数组索引是否存在越界错误. Swift还确保对同一内存区域的多次访问不会 ...

  5. Swift5 语言指南(二十二) 扩展

    扩展为现有的类,结构,枚举或协议类型添加新功能.这包括扩展您无法访问原始源代码的类型的能力(称为追溯建模).扩展类似于Objective-C中的类别.(与Objective-C类别不同,Swift扩展 ...

  6. SpringMvc 启动原理源码分析

    了解一个项目启动如何实现是了解一个框架底层实现的一个必不可少的环节.从使用步骤来看,我们一般是引入包之后,配置web.xml文件.官方文档示例的配置如下: <web-app> <se ...

  7. Spring Boot启动流程

    基础准备 1,BeanPostProcessor:这个接口的作用在于对于新构造的实例可以做一些自定义的修改.比如如何构造.属性值的修改.构造器的选择等等 2,BeanFactoryPostProces ...

  8. Android学习总结——输入法将BottomNavigationBar(底部导航栏)顶上去的问题

    在应用清单中给当前<Activity>设置: android:windowSoftInputMode="adjustPan" 关于android:windowSoftI ...

  9. linux下安装lnmp环境

    安装nginx   1 检查是否安装该程序: which nginx           #查看nginx是否存在 which php             #查看php是否存在 which mys ...

  10. Python学习--02输入和输出、运算符

    命令行输入 x = input("Please input x:") y = raw_input("Please input x:") 使用input和raw_ ...