Reputation: 81
I am not able to connect to IMAP store . It was working earlier now its throwing below error
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:474)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:176)
Code:
Properties properties = new Properties();
properties.put("mail.imap.host", "outlook.office365.com"));
properties.put("mail.imap.port", "993");
properties.setProperty("mail.imap.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
properties.setProperty("mail.imap.socketFactory.fallback", "false");
properties.setProperty("mail.imap.socketFactory.port", String.valueOf("993")));
session = Session.getInstance(properties);
store = session.get("imap");
store.connect(username,password);
I checked all the links , i tried few solutions suggested but they dint work .
Debug Info.
DEBUG: JavaMail version 1.4ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_112\jre\lib\javamail.providers (The system cannot find the file specified)
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Sun Microsystems, Inc], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: !anyLoaded
DEBUG: not loading resource: /META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jdk1.8.0_112\jre\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Sun Microsystems, Inc]
DEBUG: mail.imap.fetchsize: 16384
* OK The Microsoft Exchange IMAP4 service is ready. [TQBBADEAUABSADAAMQAwADEAQwBBADAAMAA2ADcALgBJAE4ARABQAFIARAAwADEALgBQAFIATwBEAC4ATwBVAFQATABPAE8ASwAuAEMATwBNAA==]
A0 CAPABILITY
* CAPABILITY IMAP4 IMAP4rev1 AUTH=PLAIN AUTH=XOAUTH2 SASL-IR UIDPLUS ID UNSELECT CHILDREN IDLE NAMESPACE LITERAL+
A0 OK CAPABILITY completed.
IMAP DEBUG: AUTH: PLAIN
IMAP DEBUG: AUTH: XOAUTH2
DEBUG: protocolConnect login, host=outlook.office365.com, [email protected], password=<non-null>
A1 AUTHENTICATE PLAIN
+
cGF2a3VtYXIwQHB1YmxpY2lzZ3JvdXBlLm5ldABwYXZrdW1hcjBAcHVibGljaXNncm91cGUubmV0AFNyaWRoYWJ2cmsrMTM=
A1 NO AUTHENTICATE failed.
javax.mail.AuthenticationFailedException: AUTHENTICATE failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:474)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
Upvotes: 3
Views: 30939
Reputation: 21
I faced the same issue for gmail account when i try to run the code in different device .It is because the google will not allow the gmail account to login to other device .Solution to this is you should allow access to the account using below url :
https://accounts.google.com/DisplayUnlockCaptcha
Upvotes: 0
Reputation: 1438
I also had this issue in reading emails from an outlook office365 account which was upgraded recently.
I changed code to use microsoft ews-java-api and issue resolved.
Example code to make connection and read emails with attachment and passed subject using microsoft ews-java-api:
import microsoft.exchange.webservices.data.core.ExchangeService;
import microsoft.exchange.webservices.data.core.PropertySet;
import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion;
import microsoft.exchange.webservices.data.core.enumeration.property.WellKnownFolderName;
import microsoft.exchange.webservices.data.core.enumeration.search.LogicalOperator;
import microsoft.exchange.webservices.data.core.service.item.EmailMessage;
import microsoft.exchange.webservices.data.core.service.item.Item;
import microsoft.exchange.webservices.data.core.service.schema.ItemSchema;
import microsoft.exchange.webservices.data.credential.ExchangeCredentials;
import microsoft.exchange.webservices.data.credential.WebCredentials;
import microsoft.exchange.webservices.data.property.complex.FileAttachment;
import microsoft.exchange.webservices.data.search.ItemView;
import microsoft.exchange.webservices.data.core.service.folder.Folder;
import microsoft.exchange.webservices.data.search.filter.SearchFilter;
import org.springframework.util.CollectionUtils;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
public class ExchangeEmailTest {
public static void main(String[] args) {
try {
ExchangeService service = createConnection();
readAttachmentEmail(service);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static ExchangeService createConnection() throws Exception {
String email = "your email";
String password= "your email password";
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
ExchangeCredentials credentials = new WebCredentials(email, password);
service.setUrl(new URI("https://outlook.office365.com/EWS/Exchange.asmx"));
service.setCredentials(credentials);
return service;
}
private static void readAttachmentEmail(ExchangeService service) throws Exception {
// Bind to the Inbox.
Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
// set number of items you want to retrieve
ItemView view = new ItemView(1);
List<SearchFilter> searchFilterCollection = new ArrayList<>();
// flag to pick only email which contains attachments
searchFilterCollection.add(new SearchFilter.IsEqualTo(ItemSchema.HasAttachments, Boolean.TRUE));
searchFilterCollection.add(new SearchFilter.ContainsSubstring(ItemSchema.Subject,"subject filter"));
SearchFilter finalSearchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, searchFilterCollection);
ArrayList<Item> items = service.findItems(inbox.getId(), finalSearchFilter, view).getItems();
if(!CollectionUtils.isEmpty(items)) {
Item item= items.get(0);
System.out.println("id==========" + item.getDateTimeReceived());
System.out.println("sub==========" + item.getSubject());
EmailMessage message = EmailMessage.bind(service, item.getId(), new PropertySet(ItemSchema.Attachments));
FileAttachment attachment = (FileAttachment) message.getAttachments().getItems().get(0);
attachment.load(" < attachment save directory > "+attachment.getName());
}
}
}
Upvotes: 4
Reputation: 11
Instead of giving the plain password,create a app password and use it.It will work.
Steps Login to Office 365 Got MYAccount-->Security and Privacy-->Additional security verification-->Create and manage app passwords
Just use the below properties only
Properties props = new Properties();
props.put("mail.imap.host", "outlook.office365.com");
props.setProperty("mail.imap.starttls.enable", "true");
props.setProperty("mail.imaps.port", "993");
props.setProperty("mail.debug", "true");
Upvotes: 1
Reputation: 81
Authentication failed error was coming as the Imap for the account was not enabled at firewall.
Upvotes: 3