邮件服务器身份认证,发送邮件时需要进行身份认证的问题?

运行环境如下:

Message newmsg = new MimeMessage(store.getSession());

// store 为 public class MaildirStore extends Store 的实例。

……

Store authenstore = (store.getSession()).getStore("pop3");

authenstore.connect("192.168.199.3","username","password");

// authenstore 为 Store 的实例。

System.out.println("authen connect successfully");

Transport.send(newmsg);

报错如下:

authen connect successfully

SendFailedException: Sending failed;

SendFaildeException: Invalid Addresses;

SendFaildeException: 550 Local user only or Authentication mechanism

at javax.mail.Transport.send0

at javax.mail.Transport.send

///

当有的邮件服务器发送邮件时,不需要身份认证,运行正常。

分析:(个人观点)

1。authenstore 为 Store 的实例  和  store 为 public class MaildirStore extends Store 的实例。是两个不同的进程,不能实现身份认证。可以在 Store 的继承类 store 中,定义 session.getStore("pop3"); 和 .connect() 的方法吗?我试了一下,不能直接定义。

2。 Session.getInstance(java.util.Properties, javax.mail.Authenticator)

这个方法大家见过吧,可以用 javax.mail.Authenticator  进行身份认证吗?如果可以,能告诉怎么应用吗?

|

我也遇见过类似的问题,不过我需要上面两种方法的结合才能解决问题,如果上面的方法不行,可以试试我的方法。

|

import javax.mail.*;

class MyAuthenticator extends Authenticator {

private String userName,password;

public MyAuthenticator(String userName,String password) {

this.userName = userName;

this.password = password;

}

public PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName,password);

}

}

使用时:

Properties prop = System.getProperties();

MyAuthenticator auth = new MyAuthenticator(userName,password);

Session session = Session.getDefaultInstance(prop,auth);

|

|

Properties props = new Properties();

props.put("mail.smtp.host",myHost);

props.setProperty("mail.transport.protocol","smtp");

props.setProperty("mail.smtp.auth","true");

Session session = Session.getInstance(props,             new Authenticator() {

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(myName,myPassword);

}

});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值