javaWeb发送邮件报错,JAVAEmail工具错误java.lang.ClassNotFoundException: javax.activation.DataSource
发布日期:2021-06-29 15:01:10
浏览次数:3
分类:技术文章
本文共 1756 字,大约阅读时间需要 5 分钟。
JAVAEmail工具错误java.lang.ClassNotFoundException: javax.activation.DataSource
JDK9以上或JDK6以下使用mail.jar包不加JAF的activation.jar包会抛出该错误!JDK6以上不需要加该jar包; 所以在lib下要同时导入package cn.itzheng.store.test;import java.util.Properties;import javax.mail.Authenticator;import javax.mail.Message;import javax.mail.Message.RecipientType;import javax.mail.MessagingException;import javax.mail.PasswordAuthentication;import javax.mail.Session;import javax.mail.Transport;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeMessage;public class TestEmail { public static void main(String[] args) throws Exception { // 0.1 服务器的设置 Properties props = new Properties(); //props.setProperty("mail.host", "smtp.126.com"); //props.setProperty("mail.smtp.auth", "true"); // 0.2 账号和密码 Authenticator authenticator = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { // 126账号和密码(模拟账号,需要自己注册) //return new PasswordAuthentication("itcast", "123456"); return new PasswordAuthentication("admin@store.com", "admin"); } }; // 1 与126服务器建立连接:Session Session session = Session.getDefaultInstance(props, authenticator); // 2 编写邮件:Message Message message = new MimeMessage(session); // 2.1 发件人(模拟账号) //message.setFrom(new InternetAddress("itcast@126.com")); message.setFrom(new InternetAddress("admin@store.com")); // 2.2 收件人 , to:收件人 , cc :抄送,bcc:暗送(密送)。(模拟账号) message.setRecipient(RecipientType.TO, new InternetAddress("aaa@store.com")); // 2.3 主题 message.setSubject("这是我们得第一份邮件"); // 2.4 内容 message.setContent("哈哈,您到我的商城注册了。", "text/html;charset=UTF-8"); // 3 将消息进行发送:Transport Transport.send(message); System.out.println("OKOKOK"); }}
转载地址:https://code100.blog.csdn.net/article/details/104875205 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
第一次来,支持一个
[***.219.124.196]2024年04月11日 13时27分12秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
linux系统时区修改(Debian的主机和docker)
2019-04-29
docker-compose 安装
2019-04-29
crontab 定时任务
2019-04-29
查看docker veth pair与宿主机上网卡的对应关系
2019-04-29
使用 GitLab CI 进行持续集成的一些踩坑
2019-04-29
企业云盘给贸易业带来新的效益
2019-04-29
Linux入门常用命令
2019-04-29
Spring整理
2019-04-29
SpringMvc加强
2019-04-29
初识Vue全家桶 Nuxt.js(一)
2019-04-29
基本路由及动态路由(二)
2019-04-29
视图:默认模板+默认布局(自定义布局)+nuxt.js页面(三)
2019-04-29
基于nuxt下asyncData,fetch发送axios请求(四)
2019-04-29
插件机制+自定义axios(五)
2019-04-29
Redis的学习之路
2019-04-29
Windows下Redies+GUI安装,使用Jedis与spring boot 整合
2019-04-29
Windows创建本地版本库(1)
2019-04-29
基于java的酒店管理系统的设计与实现
2019-04-29
基于WEB的仓库管理系统的设计与实现
2019-04-29