163.net需要smtp认证,你要自己继承一个 Authenticator 类,比如:
public class MailAuth extends Authenticator
{
String userName,password;
public MailAuth(String user,String pass)
{
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
然后:Properties props = new Properties();
//初始化参数
props.put("mail.smtp.host",smtpHost);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有
//创建Session
MailAuth ma=new MailAuth(smtpUser,smtpPass);
Session mailSession=Session.getInstance(props,ma);这样就行了
public class MailAuth extends Authenticator
{
String userName,password;
public MailAuth(String user,String pass)
{
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
}
然后:Properties props = new Properties();
//初始化参数
props.put("mail.smtp.host",smtpHost);
props.put("mail.transport.protocol","smtp");
props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有
//创建Session
MailAuth ma=new MailAuth(smtpUser,smtpPass);
Session mailSession=Session.getInstance(props,ma);这样就行了
解决方案 »
- 有个小概念不太清楚,请教下大家
- random
- 关于静态方法
- 请教一个定时器问题
- 有个线程的例子和预期的不一样,大家看看
- JAVA新手上路:关于编译运行问题
- 两个线程调用同一个static方法,彼此会不会影响,全局变量不用说了,那局部变量呢
- 请问各位,哪里有好的OA系统介绍啊,现我们要做一个java版的OA系统,没这方面的经验,请大家给点这方面的资料啊,谢谢啦
- 请教JAVA中整个面板容器的放大缩小问题
- BufferedReader使用readLine()方法返回一个奇葩的null值,麻烦指教。
- 关于prepareStatement.setCharacterStream的问题?
- javascript简单问题
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
import java.util.*;
public class mymail extends Authenticator { String userName,password; public mymail(String user,String pass) {
super();
userName=new String(user);
password=new String(pass);
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(userName,password);
}
public static void main(String[] args){
try{
Properties props=new Properties();
props.put("mail.host","163.net");
props.put("mail.transport.protocol","smtp");
//props.put("mail.transport.port",smtpPort);
props.put("mail.smtp.auth","true"); //这行一定要有
mymail mm=new mymail("zmrljl","fcyljl");
Session mailConnection=Session.getInstance(props,mm);
Message msg=new MimeMessage(mailConnection);
Address bill=new InternetAddress("[email protected]");
Address elliotte=new InternetAddress("[email protected]");
msg.setContent("resistansce is futile ,tyou wiil be assistant!","text/plain");
msg.setFrom(bill);
msg.setRecipient(Message.RecipientType.TO,elliotte);
msg.setSubject("you must ok");
Transport.send(msg); }
catch(Exception e){
e.printStackTrace() ;
}
}
}
/////////////////
javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.AuthenticationFailedException at javax.mail.Transport.send0(Transport.java:218) at javax.mail.Transport.send(Transport.java:80) at webmail.mymail.main(mymail.java:46)
这个类需要抛出异常吗??
[email protected]你把debug设置成true可以看到交互内容,另外你耶应该可以看到Exception的内容啊:javax.mail.MessagingException: 553 You are not authorized to send mail as <MAIL FROM: < ....>>, authentication is required