javax.mail.SendFailedException

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21294146/
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-13 07:55:55  来源:igfitidea点击:

javax.mail.SendFailedException

javajavamail

提问by IMNash

Please help to reslove the below exception:

请帮助重新解决以下异常:

DEBUG SMTP RCVD: 250 2.1.0 Sender OK

DEBUG SMTP SENT: RCPT TO:<[email protected]>
DEBUG SMTP RCVD: 550 5.7.1 Unable to relay

Invalid Addresses
  [email protected]
DEBUG SMTPTransport: Sending failed because of invalid destination addresses
DEBUG SMTP SENT: RSET
DEBUG SMTP RCVD: 250 2.0.0 Resetting

DEBUG SMTP SENT: QUIT

Exception in thread "main" javax.mail.SendFailedException: Invalid Addresses;
  nested exception is:
    class javax.mail.SendFailedException: 550 5.7.1 Unable to relay

    at com.sun.mail.smtp.SMTPTransport.rcptTo(SMTPTransport.java:804)
    at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:320)
    at javax.mail.Transport.send0(Transport.java:151)
    at javax.mail.Transport.send(Transport.java:80)
    at StatsEmail.sendEmail(StatsEmail.java:95)
    at Statements.main(Statements.java:73)

SRC/

源代码/

    String host = "XXXXX";
    String from = "XXXX";


    java.util.Properties props = new java.util.Properties();


    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.debug", "true");

    props.put("mail.smtp.port","25");


    props.put("mail.smtp.debug", "true");

    String[] to = {"[email protected]"};

    javax.mail.Session session = Session.getDefaultInstance(props, null);
    javax.mail.internet.MimeMessage message = new javax.mail.internet.MimeMessage(session);

    message.setFrom(new InternetAddress(from));

    javax.mail.internet.InternetAddress[] toAddress = new javax.mail.internet.InternetAddress[to.length];


    for(int i =0; i< to.length; i++ ){

        toAddress[i] = new InternetAddress(to[i]); 
    }



    for(int i=0; i<toAddress.length;i++){

        message.addRecipient(Message.RecipientType.TO, toAddress[i]);
    }


    message.setSubject("TestMail");
    message.setText("TestJavaMail");

    Transport.send(message);


    System.out.println("Send Successful");

采纳答案by Spindizzy

Your code seems to access any other server but not the gmail smtp server.

您的代码似乎可以访问任何其他服务器,但不能访问 gmail smtp 服务器。

If you want to use the gmail server you need authentication for the account. The following piece of source code may help you. Note: I used javax.mail version 1.4.7

如果要使用 gmail 服务器,则需要对帐户进行身份验证。以下源代码可能对您有所帮助。注意:我使用的是 javax.mail 版本 1.4.7

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


public class App {

    public static void main(String[] args) {
        String from = "[email protected]";
        String to = "[email protected]";

        Properties properties = createConfiguration();

        SmtpAuthenticator authentication = new SmtpAuthenticator();

        Session session = Session.getDefaultInstance(properties, authentication);

        try {

            MimeMessage message = new MimeMessage(session);
            message.setFrom(new InternetAddress(from));
            message.addRecipient(Message.RecipientType.TO,
                new InternetAddress(to));

            message.setSubject("This is the Subject Line!");

            // Now set the actual message
            message.setText("This is actual message");

           // Send message
           Transport.send(message);

           System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
           mex.printStackTrace();
     }
 }

 private static Properties createConfiguration() {
    return new Properties() {
        {
            put("mail.smtp.auth", "true");
            put("mail.smtp.host", "smtp.gmail.com");
            put("mail.smtp.port", "587");
            put("mail.smtp.starttls.enable", "true");
        }
    };
 }

 private static class SmtpAuthenticator extends Authenticator {

        private String username = "[email protected]";
        private String password = "secret";

        @Override
        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    }
}