update mail interface

This commit is contained in:
Ralph Soika 2023-02-08 17:58:04 +01:00
parent 98f3de046e
commit 4f88884d71
3 changed files with 220 additions and 211 deletions

View file

@ -39,247 +39,253 @@ import junit.framework.Assert;
*/ */
public class TestIMAPOutlookOAuth { public class TestIMAPOutlookOAuth {
@Test @Test
public void imapAccess2() { public void imapAccess2() {
String userEmailId = "imixs@alexander-logistics.com"; String userEmailId = "imixs@alexander-logistics.com";
try { try {
String oauth2AccessToken = getAuthToken(); String oauth2AccessToken = getAuthToken();
String host = "outlook.office365.com"; String host = "outlook.office365.com";
String port = "993"; String port = "993";
Store store = null; Store store = null;
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory"; String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Properties props = new Properties(); Properties props = new Properties();
props.put("mail.imaps.ssl.enable", "true"); props.put("mail.imaps.ssl.enable", "true");
props.put("mail.imaps.sasl.enable", "true"); props.put("mail.imaps.sasl.enable", "true");
props.put("mail.imaps.port", port); props.put("mail.imaps.port", port);
props.put("mail.imaps.auth.mechanisms", "XOAUTH2"); props.put("mail.imaps.auth.mechanisms", "XOAUTH2");
props.put("mail.imaps.sasl.mechanisms", "XOAUTH2"); props.put("mail.imaps.sasl.mechanisms", "XOAUTH2");
props.put("mail.imaps.auth.login.disable", "true"); props.put("mail.imaps.auth.login.disable", "true");
props.put("mail.imaps.auth.plain.disable", "true"); props.put("mail.imaps.auth.plain.disable", "true");
props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY); props.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.imaps.socketFactory.fallback", "false"); props.setProperty("mail.imaps.socketFactory.fallback", "false");
props.setProperty("mail.imaps.socketFactory.port", port); props.setProperty("mail.imaps.socketFactory.port", port);
props.setProperty("mail.imaps.starttls.enable", "true"); props.setProperty("mail.imaps.starttls.enable", "true");
props.put("mail.imaps.auth.plain.disable", "true"); props.put("mail.imaps.auth.plain.disable", "true");
props.put("mail.debug", "true"); props.put("mail.debug", "true");
props.put("mail.debug.auth", "true"); props.put("mail.debug.auth", "true");
Session session = Session.getInstance(props); Session session = Session.getInstance(props);
session.setDebug(true); session.setDebug(true);
store = session.getStore("imaps"); store = session.getStore("imaps");
System.out.println("OAUTH2 IMAP trying to connect with system properties to Host:" + host + ", Port: " System.out.println("OAUTH2 IMAP trying to connect with system properties to Host:" + host + ", Port: "
+ port + ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken); + port + ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken);
store.connect(host, userEmailId, oauth2AccessToken); store.connect(host, userEmailId, oauth2AccessToken);
System.out.println("IMAP connected with system properties to Host:" + host + ", Port: " + port System.out.println("IMAP connected with system properties to Host:" + host + ", Port: " + port
+ ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken); + ", userEmailId: " + userEmailId + ", AccessToken: " + oauth2AccessToken);
if (store.isConnected()) { if (store.isConnected()) {
System.out.println("Connection Established using imap protocol successfully !"); System.out.println("Connection Established using imap protocol successfully !");
IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX"); IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE); inbox.open(Folder.READ_WRITE);
// fetches all messages from the INBOX... // fetches all messages from the INBOX...
Message[] messages = inbox.getMessages(); Message[] messages = inbox.getMessages();
System.out.println("..." + messages.length + " new messages found"); System.out.println("..." + messages.length + " new messages found");
} }
} catch (Exception e) { } catch (Exception e) {
System.out.println("Store.Connect failed with the errror: " + e.getMessage()); System.out.println("Store.Connect failed with the errror: " + e.getMessage());
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
e.printStackTrace(new PrintWriter(sw)); e.printStackTrace(new PrintWriter(sw));
String exceptionAsString = sw.toString(); String exceptionAsString = sw.toString();
System.out.println(exceptionAsString); System.out.println(exceptionAsString);
Assert.fail(); Assert.fail();
} }
} }
/** /**
* Tests the Imap access * Tests the Imap access
* *
* https://stackoverflow.com/questions/73463357/cannot-authenticate-to-imap-on-office365-using-javamail * https://stackoverflow.com/questions/73463357/cannot-authenticate-to-imap-on-office365-using-javamail
* *
* mail.store.protocol="imap" mail.imap.host="outlook.office365.com" * mail.store.protocol="imap" mail.imap.host="outlook.office365.com"
* mail.imap.port="993" mail.imap.ssl.enable="true" * mail.imap.port="993" mail.imap.ssl.enable="true"
* mail.imap.starttls.enable="true" mail.imap.auth="true" * mail.imap.starttls.enable="true" mail.imap.auth="true"
* mail.imap.auth.mechanisms="XOAUTH2" mail.imap.user="<email box>" * mail.imap.auth.mechanisms="XOAUTH2" mail.imap.user="<email box>"
*/ */
@Test @Test
public void imapAccess3() { public void imapAccess3() {
Properties props = new Properties(); Properties props = new Properties();
props.put("mail.store.protocol", "imap"); props.put("mail.store.protocol", "imap");
props.put("mail.imap.host", "outlook.office365.com"); props.put("mail.imap.host", "outlook.office365.com");
props.put("mail.imap.port", "993"); props.put("mail.imap.port", "993");
props.put("mail.imap.ssl.enable", "true"); props.put("mail.imap.ssl.enable", "true");
props.put("mail.imap.starttls.enable", "true"); props.put("mail.imap.starttls.enable", "true");
props.put("mail.imap.auth", "true"); props.put("mail.imap.auth", "true");
props.put("mail.imap.auth.mechanisms", "XOAUTH2"); props.put("mail.imap.auth.mechanisms", "XOAUTH2");
props.put("mail.imap.user", "imixs@alexander-logistics.com"); props.put("mail.imap.user", "imixs@alexander-logistics.com");
// props.put("mail.imaps.auth.plain.disable","true"); // props.put("mail.imaps.auth.plain.disable","true");
props.put("mail.debug", "true"); props.put("mail.debug", "true");
props.put("mail.debug.auth", "true"); props.put("mail.debug.auth", "true");
try { try {
String token = getAuthToken(); String token = getAuthToken();
Session session = Session.getInstance(props); Session session = Session.getInstance(props);
session.setDebug(true); session.setDebug(true);
System.out.println("info: properties: " + session.getProperties().toString()); System.out.println("info: properties: " + session.getProperties().toString());
Store store = session.getStore("imap"); Store store = session.getStore("imap");
store.connect("outlook.office365.com", "imixs@alexander-logistics.com", token); store.connect("outlook.office365.com", "imixs@alexander-logistics.com", token);
} catch (Exception e) { // IMAPFolder inbox = (IMAPFolder) store.getFolder("INBOX");
// inbox.open(Folder.READ_WRITE);
// Message[] messages = inbox.getMessages();
// System.out.println("..." + messages.length + " new messages found");
// messages[0].getSubject();
e.printStackTrace(); } catch (Exception e) {
Assert.fail();
}
}
/** e.printStackTrace();
* Test the getToken method Assert.fail();
*/ }
@Test }
public void testGetToken() {
try {
String token = getAuthToken();
System.out.println("....access token = " + token);
} catch (Exception e) {
e.printStackTrace();
Assert.fail();
}
}
/** /**
* Helper method to receive a Microsoft access Token * Test the getToken method
* */
* https://jwt.ms/ @Test
* public void testGetToken() {
* "OAuth2": { try {
* String token = getAuthToken();
* "Authority": System.out.println("....access token = " + token);
* https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0, } catch (Exception e) {
* e.printStackTrace();
* "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63", Assert.fail();
* }
* "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL" }
*
* }
*
*
*
*
*
* Hallo Herr Soika,
*
*
*
* ich habe eine neue App angelegt und hier die Berechtigungen neu gesetzt.
*
*
*
* Hier die Daten dazu:
*
*
*
* "OAuth2": {
*
* "Authority":
* https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0,
*
* "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63",
*
* "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL"
*
* }
*
*
*
* Hier noch der Link zu einem Video, welches ganz gut erklärt wie das ganze
* eingebunden werden muss.
*
* Anhand dieser Anleitung bin ich ebefalls vorgegangen.
*
*
*
* https://www.youtube.com/watch?v=bMYA-146dmM&t=356s
*
*
*
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public String getAuthToken() throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
String tanantId = "51e2f038-0a96-41be-801d-bb4118aa018e";
String clientId = "39e9eec5-6939-47dc-9729-3438a9898e63";
String client_secret = "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL";
HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token"); /**
String scopes = "https://outlook.office365.com/.default"; * Helper method to receive a Microsoft access Token
// scopes="https://outlook.office365.com/IMAP.AccessAsUser.All"; *
// scopes="https://graph.microsoft.com/.default"; * https://jwt.ms/
// scopes="openid profile offline_access https://outlook.office365.com/SMTP.Send *
// https://outlook.office365.com/POP.AccessAsUser.All * "OAuth2": {
// https://outlook.office365.com/IMAP.AccessAsUser.All"; *
* "Authority":
* https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0,
*
* "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63",
*
* "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL"
*
* }
*
*
*
*
*
* Hallo Herr Soika,
*
*
*
* ich habe eine neue App angelegt und hier die Berechtigungen neu gesetzt.
*
*
*
* Hier die Daten dazu:
*
*
*
* "OAuth2": {
*
* "Authority":
* https://login.microsoftonline.com/51e2f038-0a96-41be-801d-bb4118aa018e/v2.0,
*
* "ClientId": "39e9eec5-6939-47dc-9729-3438a9898e63",
*
* "ClientSecret": "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL"
*
* }
*
*
*
* Hier noch der Link zu einem Video, welches ganz gut erklärt wie das ganze
* eingebunden werden muss.
*
* Anhand dieser Anleitung bin ich ebefalls vorgegangen.
*
*
*
* https://www.youtube.com/watch?v=bMYA-146dmM&t=356s
*
*
*
* @return
* @throws IOException
* @throws ClientProtocolException
*/
public String getAuthToken() throws ClientProtocolException, IOException {
CloseableHttpClient client = HttpClients.createDefault();
String tanantId = "51e2f038-0a96-41be-801d-bb4118aa018e";
String clientId = "39e9eec5-6939-47dc-9729-3438a9898e63";
String client_secret = "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL";
String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token");
+ "&grant_type=client_credentials"; String scopes = "https://outlook.office365.com/.default";
// scopes="https://outlook.office365.com/IMAP.AccessAsUser.All";
// scopes="https://graph.microsoft.com/.default";
// scopes="openid profile offline_access https://outlook.office365.com/SMTP.Send
// https://outlook.office365.com/POP.AccessAsUser.All
// https://outlook.office365.com/IMAP.AccessAsUser.All";
loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED)); String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret
+ "&grant_type=client_credentials";
loginPost.addHeader(new BasicHeader("cache-control", "no-cache")); loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED));
CloseableHttpResponse loginResponse = client.execute(loginPost);
InputStream inputStream = loginResponse.getEntity().getContent(); loginPost.addHeader(new BasicHeader("cache-control", "no-cache"));
byte[] response = readAllBytes(inputStream); CloseableHttpResponse loginResponse = client.execute(loginPost);
ObjectMapper objectMapper = new ObjectMapper();
JavaType type = objectMapper.constructType(
objectMapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class));
Map<String, String> parsed = new ObjectMapper().readValue(response, type);
String result = parsed.get("access_token");
System.out.println("....access token = " + result); InputStream inputStream = loginResponse.getEntity().getContent();
return result; byte[] response = readAllBytes(inputStream);
} ObjectMapper objectMapper = new ObjectMapper();
JavaType type = objectMapper.constructType(
objectMapper.getTypeFactory().constructParametricType(Map.class, String.class, String.class));
Map<String, String> parsed = new ObjectMapper().readValue(response, type);
String result = parsed.get("access_token");
public static byte[] readAllBytes(InputStream inputStream) throws IOException { System.out.println("....access token = " + result);
final int bufLen = 4 * 0x400; // 4KB return result;
byte[] buf = new byte[bufLen]; }
int readLen;
IOException exception = null;
try { public static byte[] readAllBytes(InputStream inputStream) throws IOException {
try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) { final int bufLen = 4 * 0x400; // 4KB
while ((readLen = inputStream.read(buf, 0, bufLen)) != -1) byte[] buf = new byte[bufLen];
outputStream.write(buf, 0, readLen); int readLen;
IOException exception = null;
return outputStream.toByteArray(); try {
} try (ByteArrayOutputStream outputStream = new ByteArrayOutputStream()) {
} catch (IOException e) { while ((readLen = inputStream.read(buf, 0, bufLen)) != -1)
exception = e; outputStream.write(buf, 0, readLen);
throw e;
} finally { return outputStream.toByteArray();
if (exception == null) }
inputStream.close(); } catch (IOException e) {
else exception = e;
try { throw e;
inputStream.close(); } finally {
} catch (IOException e) { if (exception == null)
exception.addSuppressed(e); inputStream.close();
} else
} try {
} inputStream.close();
} catch (IOException e) {
exception.addSuppressed(e);
}
}
}
} }

View file

@ -6,3 +6,6 @@ Umsatz (ohne Soll-/Haben-Kennzeichen);Soll-Haben-Kennzeichen;WKZ Umsatz;Kurs;Bas
76,56;"S";"EUR";0,000000;0,00;;8011;10211;;1412;"188448";;;"G LA-ZEL-2208-060";0;;;0;;;;;Faelligkeit;20230125;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 76,56;"S";"EUR";0,000000;0,00;;8011;10211;;1412;"188448";;;"G LA-ZEL-2208-060";0;;;0;;;;;Faelligkeit;20230125;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
76,56;"H";"EUR";0,000000;0,00;;8011;10211;;1412;"188450";;;"SG LA-ZEL-2208-060";0;;;0;;;;;Faelligkeit;20221228;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 76,56;"H";"EUR";0,000000;0,00;;8011;10211;;1412;"188450";;;"SG LA-ZEL-2208-060";0;;;0;;;;;Faelligkeit;20221228;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10080,00;"H";"USD";1,019129;9890,80;"EUR";8010;10211;;1612;"188733";;;"R EX-SAL-2211-127";0;;;0;;;;;Faelligkeit;20221230;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; 10080,00;"H";"USD";1,019129;9890,80;"EUR";8010;10211;;1612;"188733";;;"R EX-SAL-2211-127";0;;;0;;;;;Faelligkeit;20221230;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
450,00;"H";"USD";0,977500;460,36;"EUR";8017;15259;;1111;"185815";;;"R EX-GCA-2209-116";0;;;0;;;;;Faelligkeit;20221211;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1098,08;"H";"EUR";0,000000;0,00;;8017;15259;;1511;"186307";;;"R EX-GCA-2211-016";0;;;0;;;;;Faelligkeit;20221215;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
2288,54;"H";"USD";0,977800;2340,50;"EUR";8010;15259;;1711;"186547";;;"R EX-LUF-2211-017";0;;;0;;;;;Faelligkeit;20221217;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

Can't render this file because it has a wrong number of fields in line 2.

View file

@ -26,7 +26,7 @@
<org.imixs.office.version>4.5.3</org.imixs.office.version> <org.imixs.office.version>4.5.3</org.imixs.office.version>
<org.imixs.adapters.version>2.3.1</org.imixs.adapters.version> <org.imixs.adapters.version>2.3.1</org.imixs.adapters.version>
<!-- 2.2.14 --> <!-- 2.2.14 -->
<org.imixs.archive.version>2.3.0</org.imixs.archive.version> <org.imixs.archive.version>2.3.1-SNAPSHOT</org.imixs.archive.version>
<org.imixs.melman.version>1.0.22</org.imixs.melman.version> <org.imixs.melman.version>1.0.22</org.imixs.melman.version>
<org.imixs.ml.version>1.1.6-SNAPSHOT</org.imixs.ml.version> <org.imixs.ml.version>1.1.6-SNAPSHOT</org.imixs.ml.version>
<microprofile.version>2.2</microprofile.version> <microprofile.version>2.2</microprofile.version>