javamail发邮件始终报javax.mail.NoSuchProviderException:smtp错误

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javamail发邮件始终报javax.mail.NoSuchProviderException:smtp错误相关的知识,希望对你有一定的参考价值。

代码如下,错误如题,是书上的例子,代码本身应该没有问题,也用过不少网上抄来的方法,始终没法成功发出这么小小的一封邮件,网上总是说什么包有冲突,我的项目是SSH网站,只引入了JAVA EE5和一些Spring,Hibernate,Struts的包,怎么可能有冲突呢?!希望高人能为我解决这个问题,或者干脆给我一段能顺利执行的代码,而不是又报一堆“找不到XXX方法的错误”,感激不尽!
另外补充一下,代码里的smtp服务器用户名之类的设置应该没有问题,因为程序走到Session ssn = Session.getInstance(props, null);就挂了,都没用到那些用户名密码

Java code
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

public class zztest
public static void main(String[] args)
String smtphost = "smtp.qq.com"; // 发送邮件服务器
String user = "32729223"; // 邮件服务器登录用户名
String password = "cctv"; // 邮件服务器登录密码
String from = "32729223@qq.com"; // 发送人邮件地址
String to = "jxerjames@163.com"; // 收件人邮件地址
String subject = "测试test!"; // 邮件标题
String body = "测试test!"; // 邮件内容

// 以下为发送程序,用户无需改动
try
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth","true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject("UTF-8");
message.setText(body);

Transport transport = ssn.getTransport("smtp");
transport.connect(smtphost, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
//transport.send(message);
transport.close();

catch(Exception m)
m.printStackTrace();

参考技术A 可能是版本问题! 如果你用的MyEclipse的话右键Build Path->config build path->add library J2EE1.4或者上网下载mail.jar

你的代码没问题!或者给你个例子:

package com.mail;

import java.util.Date;
/**
* 邮件类
* @author Dave zhf_0630@126.com
* 2009-03-26
*/
public class Email
public final static String TO="xxx@xxx.com";
public final static String FROM="xxx@xxx.com";
public final static String CC="xxx@nxxx.com";
public final static String BCC="xxx@nxxx.com";
public final static String Email_Subject="Test Email With JavaMail";
public final static String Email_Header="This Is Email Header";
public final static String Email_Body="This Is Email Body";
public final static String Email_Host="smtp.xxx.com";
public Date sendDate=null;

public Email()


package com.mail;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 邮件服务器认证
* @author Dave zhf_0630@126.com
* 2009-03-26
*/
public class MailAuthenticator extends Authenticator
private final static String username="xxx";
private final static String password="xxx";

protected PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication(username,password);




package com.mail;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
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;

/**
* 发送邮件
*
* @author Dave zhf_0630@126.com
* 2009-03-26
*/
public class SendEmail

/**
* 发送普通邮件
*
* @throws Exception
*/
public void doSendNormalMail() throws Exception

Properties prop = new Properties(); // 获取系统环境
Authenticator auth = new MailAuthenticator(); // 邮件服务器认证
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop, auth);// 设置对话和邮件服务器进行通讯

Message message = new MimeMessage(session);
/*
* 设置邮件对象
*/
try
message.setSubject(Email.Email_Subject + "Normal Email"); // 设置邮件主题
message.setContent("Hello", "text/plain"); // 设置邮件格式
message.setText(Email.Email_Body); // 设置邮件体
message.setHeader("Header:", Email.Email_Header); // 设置邮件标题
message.setSentDate(new Date()); // 设置发送时间
Address address = new InternetAddress(Email.FROM, "Dave"); // 设置发信人地址
message.setFrom(address);

/*
* 设置多个发件人地址
*
* Address address[]=new InternetAddress("","") new
* InternetAddress("",""); message.addFrom(address);
*/

Address toAddress = new InternetAddress(Email.TO); // 设置接收人地址
message.setRecipient(Message.RecipientType.TO, toAddress);

// 设置多个收件人地址
// message.addRecipient(Message.RecipientType.TO,new
// InternetAddress("zhf_0630@126.com"));

message.saveChanges();
System.out.println("sendNormalEmail() 开始发送邮件……");
Transport.send(message); // 发送邮件
System.out.println("发送成功!");

catch (MessagingException e)
System.out.println("发送失败!");
e.printStackTrace();




/**
* 发送带附件的邮件
* @throws UnsupportedEncodingException
*/
public void sendEmailWithAttachment() throws UnsupportedEncodingException
Properties prop = new Properties();
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Authenticator auth = new MailAuthenticator();
Session session = Session.getDefaultInstance(prop, auth);
Message message = new MimeMessage(session);

try
message.setSubject(Email.Email_Subject + "Email With Accatchment");// 设置邮件主题
message.setContent("Hello", "text/plain"); // 设置邮件格式
message.setText(Email.Email_Body); // 设置邮件体
message.setHeader("Header:", Email.Email_Header); // 设置邮件标题
message.setSentDate(new Date()); // 设置发送时间
Address address = new InternetAddress(Email.FROM, "Dave"); // 设置发信人地址
message.setFrom(address);

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("bodypart");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("E:\\test.xls");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("E:\\test.xls");
multipart.addBodyPart(messageBodyPart);// Put parts in
message.setContent(multipart);
Address toAddress = new InternetAddress(Email.TO);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.saveChanges();
System.out.println("sendEmailWithhtml() 开始发送邮件……");
Transport.send(message);
System.out.println("发送成功!");
catch (MessagingException e)
System.out.println("发送失败!");
e.printStackTrace();




/**
* 发送带html的邮件
*
* @throws UnsupportedEncodingException
*/

public void sendEmailWithHtml() throws UnsupportedEncodingException

Properties prop = new Properties();
Authenticator auth = new MailAuthenticator();
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop, auth);
Message message = new MimeMessage(session);

String htmlText = "<h1>Hello</h1>"
+ "<a href=\"http://news.sina.com.cn\" target=\"blank\">点击连接</a>";
System.out.println(htmlText);
try
message.setSubject(Email.Email_Subject + "Email With Html");
message.setContent(htmlText + Email.Email_Body,
"text/html;charset=gb2312");
// message.setText(Email.Email_Body);
message.setSentDate(new Date());

Address address = new InternetAddress(Email.FROM, "Dave");
Address toAddress = new InternetAddress(Email.TO);
message.setFrom(address);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.saveChanges();
System.out.println("sendEmailWithHtml() 开始发送邮件……");
Transport.send(message);
System.out.println("发送成功!");
catch (MessagingException e)
System.out.println("发送失败!");
e.printStackTrace();



/**
* 测试函数
*
* @param args
*/
public static void main(String args[])
try

// 发送普通邮件
new SendEmail().doSendNormalMail();

// 发送带html的邮件
new SendEmail().sendEmailWithHtml();

// 发送带附件的邮件
new SendEmail().sendEmailWithAttachment();

catch (Exception e)
e.printStackTrace();


本回答被提问者和网友采纳

以上是关于javamail发邮件始终报javax.mail.NoSuchProviderException:smtp错误的主要内容,如果未能解决你的问题,请参考以下文章

java Mail 发送外部邮件失败,求解,

JavaMail - 如何阅读实际的邮件内容,而不是“javax.mail.internet.MimeMultipart”

关于oozie在发邮件的报错Caused by: javax.mail.AuthenticationFailedException

使用javamail发送邮件

JavaMail入门第二篇 创建邮件

JavaMail入门第二篇 创建邮件