20_java使用谷歌邮箱发送邮件

来源:csdn博客 分类: 文章浏览史 发布时间:2021-02-02 17:03:22 最后更新:2021-02-02 浏览:397
转载声明:
本文为摘录自“csdn博客”,版权归原作者所有。
温馨提示:
为了更好的体验,请点击原文链接进行浏览
摘录时间:
2021-02-02 17:03:22

最后有贴代码。

主要是为了记录问题:Please log in via your web browser and then try again

javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1&scc=1&plt=AKgnsbvO
534-5.7.14 o9jW8RRzSdzbRqYjP_N-zT5jDgtO5pB1uSeynVxOR2Ce2_tByqDdLI-SzA10rYh3Uuczkb
534-5.7.14 2k8jIaCXgElD9jZVuEdyUIDA0Hv7E7vk9K9PinhaDqvjhv6m-SCaMTLE6736d7jTC5xhTb
534-5.7.14 7gwQBT-5C0wh2xIwXQnvFtjpaj_5qbcesr_jNpGAuDAQcR3Uwn8aasXsbeRsby5SKXa655
534-5.7.14 rkO_KwxTJR9Jk2syvu3naDQ1lTJEU> 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 s125-v6sm15148194iod.18 - gsmtp

原因:gmail没有对设备授权

分两步:

1.打开开关:允许不够安全的应用

链接:https://myaccount.google.com/lesssecureapps

2.访问这个链接:https://accounts.google.com/DisplayUnlockCaptcha允许任何设备登陆

 

最后,请尝试在您的新设备上或在新应用中重新登录您的 Google 帐号。

第一次用谷歌邮箱发邮件,搞了一下午,快崩溃了。终于解决了。

 

最后贴上Java代码,从网上直接找的:

package top.rabbit20.test;

import java.util.Date;
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.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class SendGmail {
    /*
     * gmail邮箱SSL方式
     */
    private static void gmailssl(Properties props) {
        final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
        props.put("mail.debug", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.ssl.enable", "true");
        props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.auth", "true");
    }
 
 
    //gmail邮箱的TLS方式
    private static void gmailtls(Properties props) {
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.starttls.enable", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "587");
    }
 
    /*
     * 通过gmail邮箱发送邮件
     */
    public static void gmailSender(String email) {
 
        // Get a Properties object
        Properties props = new Properties();
        //选择ssl方式
        gmailssl(props);
 
        final String username = "account@gmail.com";//gmail邮箱
        final String password = "psw";//密码
        Session session = Session.getDefaultInstance(props,
                new Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password);
                    }
                });
        // -- Create a new message --
        Message msg = new MimeMessage(session);
 
 
        // -- Set the FROM and TO fields --
        try {
            msg.setFrom(new InternetAddress(username));
            msg.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(email));
            msg.setSubject("fsda");
            msg.setText("fadsfa");
            msg.setSentDate(new Date());
            Transport.send(msg);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        }
 
        System.out.println("Message sent.");
    }
    
}

 

php技术微信