javaWeb发送邮件报错,JAVAEmail工具错误java.lang.ClassNotFoundException: javax.activation.DataSource
发布日期:2021-06-29 15:01:10 浏览次数:2 分类:技术文章

本文共 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 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:Java Web 网络商城案例演示五(用户激活)
下一篇:Java Web 网络商城案例演示四、(向邮箱发送信息功能的实现)邮箱服务器的搭建,java Web发送邮件

发表评论

最新留言

感谢大佬
[***.8.128.20]2024年05月01日 03时36分30秒