IMNash IMNash - 1 month ago 16
Java Question

javax.mail.SendFailedException

Please help to reslove the below exception:

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

Answer

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

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

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