Sending Emails from java using specific Gmail accont

时间:2016-04-25 09:30:59

标签: java email gmail javamail

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.

2 个答案:

答案 0 :(得分:0)

我认为您的问题属性为mail.smtp.portmail.smtp.socketFactory.port

请注明,如果没有,更改端口号应该可以正常工作。

尝试这组属性......您的代码必须正常工作(从我正在运行的项目添加的工作代码)

            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", "*********");
                }
            });

答案 1 :(得分:0)

首先,修改此代码,如下面的props.put("mail.port", port);

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

对于host,您的编码仅适用于@gmail.com的一部分。它不适用于自己的域名Gmail服务(例如,**** @ abc.com)。 大多数情况下,使用SSL。这就是为什么,需要通过authroize person / account来配置邮件设置。

参考:SSL using in Javamkyoung