Giter Site home page Giter Site logo

Comments (2)

looly avatar looly commented on August 17, 2024

我搜了几篇文章:

使用Gmail的Oauth2:https://kgiann78.github.io/java/gmail/2017/03/16/JavaMail-send-mail-at-Google-with-XOAUTH2/

使用Outlook的Oauth2:https://medium.com/@tempmailwithpassword/sending-emails-with-java-using-oauth2-and-office-365-b164d54f68fc

事实上,换成Oauth2登录后,你需要使用token方式而不是密码方式,但是微软的token生成貌似比较麻烦,你需要新建应用,然后借助MSAL4J这个库生成token。

ConfidentialClientApplication app = ConfidentialClientApplication.builder("your-application-id", ClientCredentialFactory.createFromSecret("your-client-secret"))    .authority("https://login.microsoftonline.com/your-tenant-id")    .build();
IClientCredential credential = ClientCredentialFactory.createFromSecret("your-client-secret");
SilentParameters silentParameters = SilentParameters.builder(Collections.singleton("Mail.Send"))    .build();
IAuthenticationResult result = app.acquireTokenSilently(silentParameters).join();
String accessToken = result.accessToken();

然后使用Hutool:

final MailAccount mailAccount = new MailAccount();
mailAccount.setHost("smtp.office365.com");
mailAccount.setPort(587);
mailAccount.setFrom("[email protected]");
mailAccount.setUser("xxxx");
// 这里使用生成的token
mailAccount.setPass("token".toCharArray());
mailAccount.setAuth(true);
mailAccount.setStarttlsEnable(true);
// 这里关掉SSL
mailAccount.setSslEnable(false);
// 使用XOAUTH2
mailAccount.setCustomProperty("mail.smtp.auth.mechanisms", "XOAUTH2");
mailAccount.setCustomProperty("mail.smtp.auth.login.disable", "true");
mailAccount.setCustomProperty("mail.smtp.auth.plain.disable", "true");

final String id = Mail.of(mailAccount)
	.setTos("[email protected]")
	.setContent("Mail test from Outlook!")
	.setTitle("测试Outlook邮件")
	.send();

我没测试过,你可以试下。

from hutool.

zq99299 avatar zq99299 commented on August 17, 2024

嗯 好的,我看到也是通过自定义属性方式,以为hutool的工具没有自定义来着,我尝试成功再来回复具体方法

from hutool.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.