20_java使用谷歌邮箱发送邮件
生活随笔
收集整理的這篇文章主要介紹了
20_java使用谷歌邮箱发送邮件
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
最后有貼代碼。
主要是為了記錄問題: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 objectProperties 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.");}}?
總結
以上是生活随笔為你收集整理的20_java使用谷歌邮箱发送邮件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 移动硬盘里面装linux系统,在移动硬盘
- 下一篇: 2022年怎么申请企业邮箱?企业云邮箱申