0

I have an Email hosting account to my company from Gmail and am trying to send Emails from this account in java but am facing the following error:

 javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsF
534-5.7.14 i7ZvgRt2ia4HE_atVycPueORguLHg4yVG6hw_JGdAgbyUkBfJySVDR_XvkzLZzQp88F-UN
534-5.7.14 aoGU0uN-UBUR91zW7jsbzeq8Ojr6FEjFQcpsVKpv9GLaUPY3ee-pUk3Y6eNABFeA8DgDlu
534-5.7.14 fNDQwLg_R1I5-veyWJ8qE73R833F8PHWFuRanCjTkyPjQogqO-VrBG6omrZHsP3I-8Wphr
534-5.7.14 AjvaiqquhwnrUrmKjyk6RKaJnYaiA> Please log in via your web browser and
534-5.7.14 then try again.
534-5.7.14  Learn more at
534 5.7.14  https://support.google.com/mail/answer/78754 w77sm17182835wmw.10 - gsmtp

and here is the properties am using in my code:

    final String username = "email@mycompnay.com";
    final String password = "********";


    final String host = "smtp.gmail.com";
    final String port = "587";

    // Creating Properties object
    Properties props = new Properties();
    // Defining properties
    props.put("mail.smtp.host", host);
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");

    props.put("mail.user", username);
    props.put("mail.password", password);
    props.put("mail.port", port);

    // Authorized the Session object.
    Session mailSession = Session.getInstance(props, new javax.mail.Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

So to be clear, my email is (myemail@mycompany.com) and mycompany.com emails are hosted on Gmail. And if I replace my email with a Gmail Email like (email@gmail.com) it works properly, So what is the problem here and how to solve it.

Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
kamel Salah
  • 31
  • 10
  • Many of the suggestions below are wrong and just make the code more complicated. See the list of [common JavaMail mistakes](http://www.oracle.com/technetwork/java/javamail/faq/index.html#commonmistakes). Did you follow the instructions in the error message and [login with your browser](https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbsFi7ZvgRt2ia4HE_atVycPueORguLHg4yVG6hw_JGdAgbyUkBfJySVDR_XvkzLZzQp88F-UNaoGU0uN-UBUR91zW7jsbzeq8Ojr6FEjFQcpsVKpv9GLaUPY3ee-pUk3Y6eNABFeA8DgDlufNDQwLg_R1I5-veyWJ8qE73R833F8PHWFuRanCjTkyPjQogqO-VrBG6omrZHsP3I-8WphrAjvaiqquhwnrUrmKjyk6RKaJnYaiA)? – Bill Shannon Apr 25 '16 at 18:51
  • Yes I did but nothing happened – kamel Salah Apr 26 '16 at 03:13

2 Answers2

0

I think your problem with properties mail.smtp.port, mail.smtp.socketFactory.port

please specify if not and change the port number should work fine to you.

try this set of properties... your code must work (A working code added from my running Project)

            v_objProperties = new Properties();
            v_objProperties.put("mail.smtp.host","smtp.gmail.com");  
            v_objProperties.put("mail.smtp.auth", "true");  
            v_objProperties.put("mail.debug", "false");  
            v_objProperties.put("mail.smtp.port",25);  
            v_objProperties.put("mail.smtp.socketFactory.port",25);  
            v_objProperties.put("mail.smtp.starttls.enable", "true");
            v_objProperties.put("mail.transport.protocol", "smtp");
            v_objSession = Session.getInstance(v_objProperties, new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("abc@abc.com", "*********");
                }
            });
Vikrant Kashyap
  • 6,398
  • 3
  • 32
  • 52
0

At first, fix this code like as below props.put("mail.port", port);

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

For host, your coding just work for a part of @gmail.com. It will not work own domain gmail services (E.g; ****@abc.com). Mostly, which is using SSL. That's why, it is need to configure mail setting by authroize person/account.

REF : SSL using in Java and mkyoung

Community
  • 1
  • 1
Zaw Than oo
  • 9,651
  • 13
  • 83
  • 131