日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

通过Java和Spring Boot应用程序将Gmail用作SMTP服务器

發布時間:2023/12/3 java 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 通过Java和Spring Boot应用程序将Gmail用作SMTP服务器 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

Gmail用戶可以使用Gmail的SMTP服務器smtp.gmail.com從其Spring Boot應用程序發送電子郵件。 為此,讓我們在應用程序中進行一些設置:

  • 在application.properties文件中提供SMTP連接屬性: spring.mail.host=smtp.gmail.com spring.mail.username=<your gmail/google app email> spring.mail.password=***** spring.mail.port=587 spring.mail.properties.mail.smtp.starttls.enable=true spring.mail.properties.mail.smtp.auth=true spring.mail.properties.mail.smtp.starttls.required=true
  • 使用Spring Boot Email工具庫–它是Spring Boot Email入門庫的包裝。 在pom.xml中添加以下內容: <dependency><groupId>it.ozimov</groupId><artifactId>spring-boot-email-core</artifactId><version>0.6.3</version> </dependency>
  • 注釋你的應用程序的主類(@SpringBootApplication注解,即類)與@EnableEmailTools: @SpringBootApplication @EnableEmailTools public class EmailApplication {public static void main(String[] args){SpringApplication.run(EmailApplication.class, args);} }
  • 讓我們編寫一個使用它的測試。ozimov.springboot.mail.service.EmailService bean發送電子郵件: @RunWith(SpringRunner.class) @SpringBootTest public class EmailServiceTest {@Autowired it.ozimov.springboot.mail.service.EmailService emailService; @Value("${spring.mail.username}") String fromEmail; @Test public void testSendEmail() throws UnsupportedEncodingException { User user = new User(); user.setEmail("sanaulla123@gmail.com"); user.setDisplayName("Mohamed Sanaulla"); final Email email = DefaultEmail.builder() .from(new InternetAddress(fromEmail, "From Name")).to(Lists.newArrayList(new InternetAddress(user.getEmail(), user.getDisplayName()))) .subject("Testing email").body("Testing body ...").encoding("UTF-8").build();emailService.send(email); } }
  • 如果一切順利,您應該在收件箱中收到一封電子郵件。

    但是,當我嘗試上述代碼時,一切都不好,并且我遇到的問題是以下異常:

    Caused by: javax.mail.AuthenticationFailedException: 534-5.7.14 <https://accounts.google.com/signin/continue?sarp=1?c=1&plt=AKgnsbs2 534-5.7.14 tEY84q9p029iw1YKFy_d8O1vYNwHLixZUNHZlZbIqZki9a-EBfcUTPIenD2i6pN704O_7S 534-5.7.14 DK4FC-8-l1K1gU537F4UxjN4v4_txZ5pqxEA8ATwDhmOBzvxAYApfJTQjHL1yhHouwbhGO 534-5.7.14 LhOzSAB6Va6u-enaDfcv73dEgv1TT4b19dBfgzIkOoz_7nJ3i-LwWxZqIRyxOEnu8iNIYQ 534-5.7.14 iV27v9s4HFOrpSOJNGufv1Hg0wU5s> 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 q6sm2366693pgp.58 - gsmtp

    發生此錯誤的原因是,我的Gmail / G Suite電子郵件(即使用自定義域的電子郵件)未配置為允許從安全性較低的應用(例如我們的應用)發送電子郵件。 為此,您需要訪問:https://www.google.com/settings/security/lesssecureapps并啟用“ 允許安全程度較低的應用程序 ”切換,如下所示:

    有時,當您訪問不太安全的應用程序鏈接時,會看到如下所示的內容:

    在這種情況下,您可能正在使用G Suite,并且需要管理員啟用“安全性較低的應用程序”功能,這可以通過執行以下步驟來完成:

  • 導航到http://google.com/a/ <域名>
  • 從菜單中導航到“安全性”設置,如下圖所示:
  • 在安全設置頁面上單擊“ 基本設置 ”,如下所示:
  • 在“基本設置”頁面上,找到“ 安全性較低的應用程序”部分,然后單擊“ 轉到安全性較低的應用程序的設置 ”,如下所示:
  • 現在,在“不太安全的應用程序”頁面上,可以使用以下選項:
    選擇“ 允許用戶管理對不太安全的應用程序的訪問 ”,然后單擊頁面底部可用的“ 保存”按鈕。 這將允許單個用戶控制來自不太安全的應用程序對其電子郵件的訪問。
  • 現在,瀏覽至https://www.google.com/settings/security/lesssecureapps頁面,現在您將能夠看到用于更新“ 允許安全性較低的應用程序 ”選項的切換開關。

    翻譯自: https://www.javacodegeeks.com/2017/09/using-gmail-smtp-server-java-spring-boot-apps.html

    總結

    以上是生活随笔為你收集整理的通过Java和Spring Boot应用程序将Gmail用作SMTP服务器的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。