mail test classes
This commit is contained in:
parent
3d0993deab
commit
d493ec2fbd
5 changed files with 89 additions and 262 deletions
|
|
@ -3,17 +3,11 @@ package com.alexanderlogistics;
|
||||||
import java.text.DateFormat;
|
import java.text.DateFormat;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.Optional;
|
|
||||||
import java.util.logging.Logger;
|
import java.util.logging.Logger;
|
||||||
|
|
||||||
import org.imixs.workflow.FileData;
|
|
||||||
import org.imixs.workflow.exceptions.PluginException;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Ignore;
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test generates random test data
|
* This test generates random test data
|
||||||
*
|
*
|
||||||
|
|
|
||||||
|
|
@ -1,71 +0,0 @@
|
||||||
package com.alexanderlogistics;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
|
||||||
import javax.mail.Store;
|
|
||||||
|
|
||||||
import org.imixs.archive.importer.DocumentImportService;
|
|
||||||
import org.imixs.archive.importer.mail.IMAPOutlookAuthenticator;
|
|
||||||
import org.imixs.workflow.ItemCollection;
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This Imixs IMAPAuthenticator
|
|
||||||
* @author rsoika
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TestIMAPAuhtenticator {
|
|
||||||
|
|
||||||
|
|
||||||
private final static Logger logger = Logger.getLogger(TestIMAPAuhtenticator.class.getName());
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test imap connect via oauth
|
|
||||||
*
|
|
||||||
* https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testOutlookToken() {
|
|
||||||
|
|
||||||
IMAPOutlookAuthenticator auth=new IMAPOutlookAuthenticator();
|
|
||||||
|
|
||||||
|
|
||||||
ItemCollection source=new ItemCollection();
|
|
||||||
source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
|
|
||||||
source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");
|
|
||||||
source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com");
|
|
||||||
source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL");
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Properties sourceOptions = new Properties();
|
|
||||||
sourceOptions.put("microsoft.tenantid", "51e2f038-0a96-41be-801d-bb4118aa018e");
|
|
||||||
sourceOptions.put("microsoft.clientid", "39e9eec5-6939-47dc-9729-3438a9898e63");
|
|
||||||
|
|
||||||
try {
|
|
||||||
Store store = auth.openMessageStore(source, sourceOptions);
|
|
||||||
|
|
||||||
Assert.assertNotNull(store);
|
|
||||||
} catch (MessagingException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -0,0 +1,87 @@
|
||||||
|
package com.alexanderlogistics;
|
||||||
|
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
|
import javax.mail.Store;
|
||||||
|
|
||||||
|
import org.imixs.archive.importer.DocumentImportService;
|
||||||
|
import org.imixs.archive.importer.mail.IMAPBasicAuthenticator;
|
||||||
|
import org.imixs.archive.importer.mail.IMAPOutlookAuthenticator;
|
||||||
|
import org.imixs.workflow.ItemCollection;
|
||||||
|
import org.junit.Test;
|
||||||
|
|
||||||
|
import junit.framework.Assert;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* This test class test the Imixs IMAPAuthenticator classes
|
||||||
|
*
|
||||||
|
* @author rsoika
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class TestIMAPAuhtenticators {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test IMAP connect via OAuth
|
||||||
|
*
|
||||||
|
* https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testOutlookAuthenticator() {
|
||||||
|
|
||||||
|
IMAPOutlookAuthenticator auth = new IMAPOutlookAuthenticator();
|
||||||
|
|
||||||
|
ItemCollection source = new ItemCollection();
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com");
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "A6x8Q~EWVUyt~gfyTHtIJf4ig.F9tajUpEEXMaCL");
|
||||||
|
|
||||||
|
Properties sourceOptions = new Properties();
|
||||||
|
sourceOptions.put("microsoft.tenantid", "51e2f038-0a96-41be-801d-bb4118aa018e");
|
||||||
|
sourceOptions.put("microsoft.clientid", "39e9eec5-6939-47dc-9729-3438a9898e63");
|
||||||
|
|
||||||
|
try {
|
||||||
|
Store store = auth.openMessageStore(source, sourceOptions);
|
||||||
|
Assert.assertNotNull(store);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Assert.fail();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Test IMAP connect via Basic Authentication
|
||||||
|
* <p>
|
||||||
|
* NOTE: This test should fail in December 2022 as Basic Authentication is no
|
||||||
|
* longer supported by Microsoft Outlook.
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
@Test
|
||||||
|
public void testBasicAuthenticator() {
|
||||||
|
|
||||||
|
IMAPBasicAuthenticator auth = new IMAPBasicAuthenticator();
|
||||||
|
|
||||||
|
ItemCollection source = new ItemCollection();
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_SERVER, "outlook.office365.com");
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_PORT, "993");
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_USER, "imixs@alexander-logistics.com");
|
||||||
|
|
||||||
|
source.setItemValue(DocumentImportService.SOURCE_ITEM_PASSWORD, "i^i$oe-!+o1o0io(");
|
||||||
|
|
||||||
|
Properties sourceOptions = new Properties();
|
||||||
|
|
||||||
|
try {
|
||||||
|
Store store = auth.openMessageStore(source, sourceOptions);
|
||||||
|
Assert.assertNotNull(store);
|
||||||
|
} catch (MessagingException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
Assert.fail();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -1,88 +0,0 @@
|
||||||
package com.alexanderlogistics;
|
|
||||||
|
|
||||||
import java.util.Properties;
|
|
||||||
import java.util.logging.Logger;
|
|
||||||
|
|
||||||
import javax.mail.MessagingException;
|
|
||||||
import javax.mail.NoSuchProviderException;
|
|
||||||
import javax.mail.Session;
|
|
||||||
import javax.mail.Store;
|
|
||||||
|
|
||||||
import org.junit.Before;
|
|
||||||
import org.junit.Test;
|
|
||||||
|
|
||||||
import junit.framework.Assert;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* This test generates random test data
|
|
||||||
*
|
|
||||||
* https://learn.microsoft.com/en-us/exchange/client-developer/legacy-protocols/how-to-authenticate-an-imap-pop-smtp-application-by-using-oauth#get-an-access-token
|
|
||||||
*
|
|
||||||
* @author rsoika
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class TestIMAPOauth2 {
|
|
||||||
|
|
||||||
final static String FILE = "/DTVF_SKBeschrift_21010.csv";
|
|
||||||
|
|
||||||
private final static Logger logger = Logger.getLogger(TestIMAPOauth2.class.getName());
|
|
||||||
|
|
||||||
@Before
|
|
||||||
public void setUp() throws Exception {
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Test imap connect via oauth
|
|
||||||
*
|
|
||||||
* https://techcommunity.microsoft.com/t5/exchange/javamail-connecting-to-office-365-xoauth2-for-imap/m-p/1505026
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void testIMAP() {
|
|
||||||
try {
|
|
||||||
Properties properties = new Properties();
|
|
||||||
properties.put("mail.imap.ssl.enable", "true");
|
|
||||||
properties.put("mail.imap.auth.mechanisms", "XOAUTH2");
|
|
||||||
properties.put("mail.imap.auth.mechanisms", "XOAUTH2");
|
|
||||||
properties.put("mail.imap.sasl.mechanisms", "XOAUTH2");
|
|
||||||
properties.put("mail.imap.sasl.enable", "true");
|
|
||||||
// properties.put("mail.imap.sasl.enable", "true"); un-commented still results
|
|
||||||
// are same
|
|
||||||
properties.put("mail.imap.auth.login.disable", "true");
|
|
||||||
properties.put("mail.imap.auth.plain.disable", "true");
|
|
||||||
|
|
||||||
// Activate debug mode
|
|
||||||
properties.put("mail.debug", "true");
|
|
||||||
properties.put("mail.debug.auth", "true");
|
|
||||||
|
|
||||||
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
|
||||||
properties.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
|
|
||||||
properties.setProperty("mail.imap.socketFactory.fallback", "false");
|
|
||||||
properties.setProperty("mail.imap.socketFactory.port", "993");
|
|
||||||
properties.setProperty("mail.imap.starttls.enable", "true");
|
|
||||||
|
|
||||||
|
|
||||||
Session session = Session.getInstance(properties);
|
|
||||||
session.setDebug(true);
|
|
||||||
|
|
||||||
String userEmail = "imixs@alexander-logistics.com";
|
|
||||||
// i^i$oe-!+o1o0io(
|
|
||||||
String accessToken = "accessToken";
|
|
||||||
|
|
||||||
Store store;
|
|
||||||
|
|
||||||
store = session.getStore("imap");
|
|
||||||
|
|
||||||
String url="outlook.office365.com";
|
|
||||||
//String url="https://outlook.office.com/IMAP.AccessAsUser.All";
|
|
||||||
store.connect(url, 993, userEmail, accessToken);
|
|
||||||
} catch (MessagingException e) {
|
|
||||||
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
@ -30,61 +30,14 @@ import com.sun.mail.imap.IMAPFolder;
|
||||||
import junit.framework.Assert;
|
import junit.framework.Assert;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This test generates
|
* This test shows how to access outlook via oauth2
|
||||||
*
|
*
|
||||||
* See: https://www.java-forum.org/thema/javamail-mit-oauth2-an-o365.199133/
|
* See: https://www.java-forum.org/thema/javamail-mit-oauth2-an-o365.199133/
|
||||||
*
|
*
|
||||||
* @author rsoika
|
* @author rsoika
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
public class TestIMAPGetToken {
|
public class TestIMAPOutlookOAuth {
|
||||||
|
|
||||||
/**
|
|
||||||
* Tests the Imap access
|
|
||||||
*/
|
|
||||||
@Test
|
|
||||||
public void imapAccess() {
|
|
||||||
String mailServerAdress = "outlook.office365.com";
|
|
||||||
// String mailboxUserName = "###MyUserName###";
|
|
||||||
String mailboxUserName = "imixs@alexander-logistics.com";
|
|
||||||
String mailboxPassword = "###MyPassword###";
|
|
||||||
String protocol = "imaps";
|
|
||||||
|
|
||||||
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
|
|
||||||
Properties props = new Properties();
|
|
||||||
|
|
||||||
props.put("mail.imap.ssl.enable", "true");
|
|
||||||
props.put("mail.imap.port", "993");
|
|
||||||
|
|
||||||
props.put("mail.imap.auth.mechanisms", "XOAUTH2");
|
|
||||||
props.put("mail.imap.sasl.mechanisms", "XOAUTH2");
|
|
||||||
|
|
||||||
props.put("mail.imap.auth.login.disable", "true");
|
|
||||||
props.put("mail.imap.auth.plain.disable", "true");
|
|
||||||
|
|
||||||
props.setProperty("mail.imap.socketFactory.class", SSL_FACTORY);
|
|
||||||
props.setProperty("mail.imap.socketFactory.fallback", "false");
|
|
||||||
props.setProperty("mail.imap.socketFactory.port", "993");
|
|
||||||
props.setProperty("mail.imap.starttls.enable", "true");
|
|
||||||
|
|
||||||
props.put("mail.debug", "true");
|
|
||||||
props.put("mail.debug.auth", "true");
|
|
||||||
try {
|
|
||||||
|
|
||||||
mailboxPassword = getAuthToken();
|
|
||||||
System.out.println("info: AccessToken: " + mailboxPassword);
|
|
||||||
|
|
||||||
Session session = Session.getInstance(props);
|
|
||||||
session.setDebug(true);
|
|
||||||
System.out.println("info: properties: " + session.getProperties().toString());
|
|
||||||
Store store = session.getStore(protocol);
|
|
||||||
store.connect(mailServerAdress, mailboxUserName, mailboxPassword);
|
|
||||||
} catch (Exception e) {
|
|
||||||
|
|
||||||
e.printStackTrace();
|
|
||||||
Assert.fail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void imapAccess2() {
|
public void imapAccess2() {
|
||||||
|
|
@ -126,7 +79,6 @@ public class TestIMAPGetToken {
|
||||||
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);
|
||||||
|
|
||||||
String anderesPasswort = "i^i$oe-!+o1o0io(";
|
|
||||||
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);
|
||||||
|
|
@ -201,11 +153,8 @@ public class TestIMAPGetToken {
|
||||||
@Test
|
@Test
|
||||||
public void testGetToken() {
|
public void testGetToken() {
|
||||||
try {
|
try {
|
||||||
|
|
||||||
String token = getAuthToken();
|
String token = getAuthToken();
|
||||||
|
|
||||||
System.out.println("....access token = " + token);
|
System.out.println("....access token = " + token);
|
||||||
|
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
Assert.fail();
|
Assert.fail();
|
||||||
|
|
@ -306,50 +255,6 @@ public class TestIMAPGetToken {
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Helper method to receive a Microsoft access Token
|
|
||||||
*
|
|
||||||
* @return
|
|
||||||
* @throws IOException
|
|
||||||
* @throws ClientProtocolException
|
|
||||||
*/
|
|
||||||
public String getAuthTokenOld() throws ClientProtocolException, IOException {
|
|
||||||
CloseableHttpClient client = HttpClients.createDefault();
|
|
||||||
String tanantId = "51e2f038-0a96-41be-801d-bb4118aa018e";
|
|
||||||
String clientId = "75657ae4-465d-4245-9c1f-ebd6fdfc4814";
|
|
||||||
// String clientId = "6ed3fb66-a16a-47da-a4ae-fb3d8690b117";
|
|
||||||
String objectId = "229d0e4f-eef1-476c-9b8c-97c437a93314";
|
|
||||||
// String client_secret= "2a41bf2d-ffd8-444d-a2c5-6a4a418650ba";
|
|
||||||
String client_secret = "EM_8Q~6HRhhKhwJKAIXQcRdlJzn.iOj2Fq3Yhdo_";
|
|
||||||
|
|
||||||
HttpPost loginPost = new HttpPost("https://login.microsoftonline.com/" + tanantId + "/oauth2/v2.0/token");
|
|
||||||
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";
|
|
||||||
|
|
||||||
String encodedBody = "client_id=" + clientId + "&scope=" + scopes + "&client_secret=" + client_secret
|
|
||||||
+ "&grant_type=client_credentials";
|
|
||||||
|
|
||||||
loginPost.setEntity(new StringEntity(encodedBody, ContentType.APPLICATION_FORM_URLENCODED));
|
|
||||||
|
|
||||||
loginPost.addHeader(new BasicHeader("cache-control", "no-cache"));
|
|
||||||
CloseableHttpResponse loginResponse = client.execute(loginPost);
|
|
||||||
|
|
||||||
InputStream inputStream = loginResponse.getEntity().getContent();
|
|
||||||
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");
|
|
||||||
|
|
||||||
System.out.println("....access token = " + result);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
public static byte[] readAllBytes(InputStream inputStream) throws IOException {
|
public static byte[] readAllBytes(InputStream inputStream) throws IOException {
|
||||||
final int bufLen = 4 * 0x400; // 4KB
|
final int bufLen = 4 * 0x400; // 4KB
|
||||||
byte[] buf = new byte[bufLen];
|
byte[] buf = new byte[bufLen];
|
||||||
Loading…
Reference in a new issue