javax.mail.SendFailedException:无效地址(尝试使用 Rediffmail 发送电子邮件时)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6633573/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-16 08:27:08  来源:igfitidea点击:

javax.mail.SendFailedException: Invalid Addresses (While trying to send emal using Rediffmail)

javaemailjavamail

提问by Suhail Gupta

This program attempts to send email by first connecting to smtp.rediffmail.com. There is no compile time error or compile time exception.But as i try to run the following program it generates the following exception.

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

javax.mail.SendFailedException: Invalid Addresses; nested exception is: com.sun.mail.smtp.SMTPAddressFailedException: 421 Authorization failed: please authenticate by doing get message first

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

I can't figure out what the exception is and why i am getting this exception .

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

Here is the complete program.In this i have tried to make TLSconnection with rediffmail server.

这是完整的程序。在此我尝试与 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 = "[email protected]";
       String from = "[email protected]";
       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);
              }
   }
}

Why do i get this exception ?

为什么我会收到此异常?

回答by sdolgy

as per: http://www.techtalkz.com/microsoft-outlook/193842-pop3.html

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

In your account settings, enable the "Log on to incoming server before sending mail" on the "Outgoing Server" tab of your account properties. How to locate these properties and tabs is Outlook version-specific but you decided that information wasn't important.

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

The error is specific to the SMTP service you are trying to use from your client. It's not a code problem. Check your rediffmail.comaccount settings

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

回答by andbi

You're trying to use TLS auth but I don't see any port settings in your code. Usually smtp server uses different ports for TLS/SSL authentication, try setting it via mail.smtp.socketFactory.port. For TLS default value is 587, for SSL - 993as far as I remember.

您正在尝试使用 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");

回答by Vijay.Karjala

The problem with the code is this:

代码的问题是这样的:

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

the send method is a static method and you are not suppose to access it with an instance of Transport class. it should be - Transport.send(msg);

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