0

I am trying to send mail in java but i keep getting this error "com.sun.mail.smtp.SMTPSendFailedException: 550 failed to meet SPF requirements" , i have scoured the internet looking if anyone else has got this problem in java and found nothing. Any body have an idea what this error means? My code that sends the email is below.

            //Create session and message
            Properties props = System.getProperties();
            props.put("mail.smtp.user", user);
            props.put("mail.smtp.password", password);
            props.put("mail.smtp.auth", "false");
            props.put("mail.smtp.host", mailhost);

            javax.mail.Authenticator auth = null;
            auth = new javax.mail.Authenticator() {
                @Override
                public javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(user, password);
                }
            };
            session = Session.getInstance(props, auth);
            Message msg = new MimeMessage(session);

           //Set from,recipients,content and other stuff here
           //...................

           //Send the message
           Transport.send(msg);
JCS
  • 897
  • 4
  • 20
  • 43

3 Answers3

1

Managed to solve the problem by setting "mail.smtp.auth" property to true and adding the property "mail.smtp.ssl.enable" and setting that to true then finally instead of using a static method below to send the message.

Transport.send(Message msg) 

i use a instance method on a transport object i get from the session object to send the message.

transport.sendMessage(Message msg, Address[] addresses)  

Below is the modified and working code.

       //Create session
        Properties props = System.getProperties();
        props.put("mail.smtp.user", user);
        props.put("mail.smtp.password", password);
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", mailhost);
        props.put("mail.smtp.ssl.enable", "true");


        javax.mail.Authenticator auth = null;
        auth = new javax.mail.Authenticator() {
            @Override
            public javax.mail.PasswordAuthentication getPasswordAuthentication() {
                return new javax.mail.PasswordAuthentication(user, password);
            }
        };
        session = Session.getInstance(props, auth);
        //get transport object from session and connect to mail server
        Transport tr = session.getTransport("smtp");
        tr.connect(session.getProperty("mail.smtp.host"),  session.getProperty("mail.smtp.user"), session.getProperty("mail.smtp.password"));

        //create message  and set from,recipients,content and other stuff here on the message object.
        Message msg = new MimeMessage(session);
       //..................
       //...................

       //Save and send the message
        msg.saveChanges();
        tr.sendMessage(msg, msg.getAllRecipients());
        tr.close();

This link really helped me solving my problem: http://javamail.java.net/nonav/docs/api/com/sun/mail/smtp/package-summary.html

JCS
  • 897
  • 4
  • 20
  • 43
0

try this configuration

props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
props.put("mail.debug", "false");
props.put("mail.smtp.port", port);

It seems these recipients' mail servers have implemented SPF checking for incoming messages, and your domain does not declare SPF.

muthukumar
  • 2,233
  • 3
  • 23
  • 30
  • I did that now i get a "javax.mail.AuthenticationFailedException", really confusing stuff as nothing has really changed on the smtp server. – JCS Aug 12 '13 at 14:55
  • You might need to connect using SSL, or use the STARTTLS command to switch to SSL/TLS after connecting. Try setting "mail.smtp.ssl.enable" to "true". See the javadocs for the com.sun.mail.smtp package for all the properties you can set. And of course make sure you're providing the correct password! – Bill Shannon Aug 12 '13 at 18:39
0

You can send mail from your gmail easily. Step by step described in here