【问题标题】:javax.mail.SendFailedException: Invalid Addresses (While trying to send emal using Rediffmail)javax.mail.SendFailedException:无效地址(尝试使用 Rediffmail 发送电子邮件时)
【发布时间】:2011-07-09 08:08:50
【问题描述】:

此程序尝试通过首先连接到 smtp.rediffmail.com 来发送电子邮件。没有编译时错误或编译时异常。但是当我尝试运行以下程序时,它会生成以下异常。

javax.mail.SendFailedException:无效地址; 嵌套异常是: com.sun.mail.smtp.SMTPAddressFailedException:421 授权失败:请通过身份验证 首先获取消息

我无法弄清楚异常是什么以及为什么会出现此异常。

这是完整的程序。在此我尝试与 rediffmail 服务器建立 TLS 连接。

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;

class rediff {
   public static void main(String args[]) {
      Properties props = new Properties();
      props.put("mail.smtp.host", "smtp.rediffmail.com");
      props.put("mail.stmp.user", "from");

      //To use TLS
      props.put("mail.smtp.auth", "true");
      props.put("mail.smtp.starttls.enable", "true");
      props.put("mail.smtp.password", "password");

      Session session = Session.getDefaultInstance(props, new Authenticator() {
        @Override
            protected PasswordAuthentication getPasswordAuthentication() {
               String username = "from";
               String password = "password";
                return new PasswordAuthentication("from", "password");
            }
      });
       String to = "me@gmail.com";
       String from = "from@rediff.com";
       String subject = "Testing...";
       MimeMessage msg = new MimeMessage(session);
         try {
           msg.setFrom(new InternetAddress(from));
           msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(to));
           msg.setSubject(subject);
           msg.setText("rediff program working...!");
           Transport transport = session.getTransport("smtp");
           transport.send(msg);
           System.out.println("fine!!");
         }   catch(Exception exc) {
                 System.out.println(exc);
              }
   }
}

为什么会出现这个异常?

【问题讨论】:

    标签: java email jakarta-mail


    【解决方案1】:

    根据:http://www.techtalkz.com/microsoft-outlook/193842-pop3.html

    在您的帐户设置中,启用“登录到传入服务器之前 发送邮件”在您帐户属性的“发送服务器”选项卡上。如何 查找这些属性和选项卡是特定于 Outlook 版本的,但您 认为信息不重要。

    该错误特定于您尝试从客户端使用的 SMTP 服务。这不是代码问题。检查您的rediffmail.com 帐户设置

    【讨论】:

    • 我没有找到任何此类选项
    • @grassPro ... 有什么理由让你跑来跑去放 -1 而不提供反馈或 cmets?好像有点傻。
    【解决方案2】:

    代码的问题是这样的:

    Transport transport = session.getTransport("smtp");
    transport.send(msg);
    

    send 方法是一个静态方法,您不应该使用 Transport 类的实例来访问它。它应该是 - Transport.send(msg);

    【讨论】:

      【解决方案3】:

      您正在尝试使用 TLS 身份验证,但我在您的代码中看不到任何端口设置。通常 smtp 服务器使用不同的端口进行 TLS/SSL 身份验证,请尝试通过mail.smtp.socketFactory.port 进行设置。据我所知,对于 TLS,默认值为 587,对于 SSL - 993

          props.put("mail.smtp.socketFactory.port", port);
          props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
      

      【讨论】:

      • TLS 真的需要端口设置吗?顺便说一句同样的例外
      • 这是 SSL 。我正在尝试使用 TLS 。请阅读问题
      猜你喜欢
      • 2020-09-01
      • 2010-11-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-01-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多