SMTP主机(邮件服务器)需要身份验证
我给你一个验证,如下:
package mailsystem;import javax.mail.*;public class Email_Autherticatorbean extends javax.mail.Authenticator
{
private String m_username = null;
private String m_userpass = null; public void setUsername(String username)
{
m_username = username;
} public void setUserpass(String userpass)
{
m_userpass = userpass;
} public Email_Autherticatorbean()
{
super();
} public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
//在需要身份验证时自动被调用
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
再加下面的:
props.put("mail.smtp.auth","true"); //设置身份验证为真,若须身份验证则必须设为真 //获得邮件会话对象
//session = Session.getDefaultInstance(props,null);
//第二个参数为身份验证
session = Session.getDefaultInstance(props, new Email_Autherticatorbean( user, password ) );
要完整的程序的话留下邮箱,我给你发过去。
我给你一个验证,如下:
package mailsystem;import javax.mail.*;public class Email_Autherticatorbean extends javax.mail.Authenticator
{
private String m_username = null;
private String m_userpass = null; public void setUsername(String username)
{
m_username = username;
} public void setUserpass(String userpass)
{
m_userpass = userpass;
} public Email_Autherticatorbean()
{
super();
} public Email_Autherticatorbean(String username, String userpass)
{
super();
setUsername(username);
setUserpass(userpass);
}
//在需要身份验证时自动被调用
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(m_username,m_userpass);
}
}
再加下面的:
props.put("mail.smtp.auth","true"); //设置身份验证为真,若须身份验证则必须设为真 //获得邮件会话对象
//session = Session.getDefaultInstance(props,null);
//第二个参数为身份验证
session = Session.getDefaultInstance(props, new Email_Autherticatorbean( user, password ) );
要完整的程序的话留下邮箱,我给你发过去。
[email protected]