props.put还有两个属性没设
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
//设置邮件传输协议为:smtp
props.put("mail.transpost.protocol","smtp");
//设置邮件服务器端口
props.put("mail.smtp.port","25");
解决方案 »
- 关于备份数据库的问题,是否只能在数据库服务器上面备份?
- java 服务器时间校对
- structs <html:radio>怎样设初始值?
- 找不到网页[记录Java学习历程问题1]
- 怎样用WriteObject向文件中写入一个一个对象,然后,再用ReadObject一次全读出来?
- 刚开始接触java,望高手指点.
- Spring+hibernate 获取bean出现空指针错误!请大家指点,我是初学者
- 求 大神帮忙 看看 这个程序还缺什么 为什么 运行 后 是 this si my jsp page
- 使用TOMCAT 发布JSP如何不让我 写的每个SERVLET 或者 修改BEAN的时候都不重新启动TOMCAT才能看到效果?
- 异常 Caused by: javax.crypto.BadPaddingException: Given final block not properly p
- 点击提交按钮对应的action里,需要指定到网站的根目录么?
- 菜鸟100分跪求hibernate连接池问题高手帮忙```连接池问题--在线等
代码
Properties props = new Properties();
Authenticator auth = new Email_Autherticator();
props.put("mail.smtp.host", "smtp.163.com");
props.put("mail.transpost.protocol", "smtp");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");
Session ssn = Session.getDefaultInstance(props, auth);
MimeMessage message = new MimeMessage(ssn);
message.setSubject("test subject");
message.setText("test Body");
message.setSentDate(new Date());
Address fromAddress = new InternetAddress("[email protected]");
message.setFrom(fromAddress);
Address toAddress = new InternetAddress("[email protected]");
message.addRecipient(Message.RecipientType.TO, toAddress);
Transport.send(message); public class Email_Autherticator extends Authenticator {
public Email_Autherticator() {
super();
} public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("test", "test");
}
}
-------------------------------------------------------------
报错信息
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
at javax.mail.Session.loadProvidersFromStream(Session.java:928)
at javax.mail.Session.access$000(Session.java:174)
at javax.mail.Session$1.load(Session.java:870)
at javax.mail.Session.loadResource(Session.java:1084)
at javax.mail.Session.loadProviders(Session.java:889)
at javax.mail.Session.<init>(Session.java:210)
at javax.mail.Session.getDefaultInstance(Session.java:299)
at com.xxx.util.mail.Test.send(Test.java:38)
at com.xxx.util.mail.Test.main(Test.java:27)“Session ssn = Session.getDefaultInstance(props, auth);”这行代码报错
javamail的包有两种方式,一种是核心包和provider包分离的,可以自由组合,
一种是包括所有的包。看你的出错信息,应该只是一个核心包,其它的provider包没有安装进去。
可写成:
public class CheckSendMail extends Authenticator {
/**
* Email发送帐号密码验证类
*/ private String m_username = null;
private String m_userpass = null;
public CheckSendMail(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
public void setUsername(String username)
{
m_username = username;
}
public void setUserpass(String userpass)
{
m_userpass = userpass;
}
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}}