javax.mail.SendFailedException

时间:2014-01-22 21:16:33

标签: java javamail

请帮助解决以下异常:

DEBUG SMTP RCVD: 250 2.1.0 Sender OK

DEBUG SMTP SENT: RCPT TO:<XXXX@gmail.com>
DEBUG SMTP RCVD: 550 5.7.1 Unable to relay

Invalid Addresses
  XXX@gmail.com
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 = {"XXXX@gmail.com"};

    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");

1 个答案:

答案 0 :(得分:1)

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

如果您想使用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 = "XXXX@gmail.com";
        String to = "XXXX@web.de";

        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 = "XXXX@gmail.com";
        private String password = "secret";

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