LoginFailed on connecting outlook 365
Clash Royale CLAN TAG#URR8PPP
LoginFailed on connecting outlook 365
package com.email.test;
import java.util.Arrays;
import java.util.Properties;
import javax.mail.BodyPart;
import javax.mail.Flags;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.search.FlagTerm;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public abstract class SampleEmailReader
public static final int SUCCESS = 0;
public static final int FAILURE = -1;
private transient static final Logger logger = LoggerFactory.getLogger(SampleEmailReader.class);
public SampleEmailReader()
public void processEmailAttachements()
try
logger.info("Started processEmailAttachements ");
Properties emailProperties = new Properties();
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
emailProperties.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
emailProperties.setProperty("mail.imaps.socketFactory.fallback", "false");
emailProperties.setProperty("mail.imaps.port", "993");
emailProperties.setProperty("mail.imaps.socketFactory.port", "993");
emailProperties.setProperty("mail.imaps.auth", "true");
emailProperties.setProperty("mail.imaps.host", "outlook.office365.com");
emailProperties.setProperty("mail.imaps.auth.plain.disable", "true");
emailProperties.setProperty("mail.imaps.auth.gssapi.disable", "true");
emailProperties.setProperty("mail.imaps.auth.ntlm.disable", "true");
emailProperties.setProperty("mail.imaps.ssl.enable", "true");
Session session = Session.getInstance(emailProperties,
new javax.mail.Authenticator()
protected PasswordAuthentication getPasswordAuthentication()
return new PasswordAuthentication("test@outlook.com", "password@2016");
);
Store store = session.getStore("imaps");
store.connect();
Folder inbox = store.getFolder("INBOX");
inbox.open(Folder.READ_WRITE);
logger.info("Inbox MessageCount-->" + inbox.getMessageCount());
Message messages = inbox.search(new FlagTerm(
new Flags(Flag.SEEN), false));
logger.info("Number of UnRead Mails = " + messages.length);
for (Message inboxMessage : messages)
int status = processMessageBody(inboxMessage);
if (status == SUCCESS)
inboxMessage.setFlag(Flag.SEEN, true);
else
inboxMessage.setFlag(Flag.SEEN, false);
inbox.close(true);
store.close();
catch (Exception ex)
ex.printStackTrace();
logger.error("Error in processEmailAttachements", ex);
private int processMessageBody(Message message)
int status = FAILURE;
try
logger.info("Started processMessageBody*******");
logger.info("Message Subject******" + message.getSubject());
logger.info("From Address *******"
+ Arrays.toString(message.getFrom()));
Object content = message.getContent();
if (content instanceof String)
logger.error("Invalid Content Type.No need to process the Mail with Subject "
+ message.getSubject());
else if (content instanceof Multipart)
Multipart multiPart = (Multipart) content;
try
for (int i = 0; i < multiPart.getCount(); i++)
BodyPart bodyPart = multiPart.getBodyPart(i);
if (bodyPart.getDisposition() != null
&& bodyPart.getDisposition().equalsIgnoreCase(
Part.ATTACHMENT)
&& bodyPart.getFileName() != null)
status = readExcel(bodyPart);
catch (Exception e)
e.printStackTrace();
logger.error("BatchException in procesMultiPart", e);
catch (Exception e)
e.printStackTrace();
logger.error("Exception in processMessageBody", e);
return status;
public abstract int readExcel(BodyPart bodyPart);
Executing
package EmailTable;
import javax.mail.BodyPart;
public class Email
public static void main(String args)
// TODO Auto-generated method stub
SampleEmailReader reader = new SampleEmailReader()
@Override
public int readExcel(BodyPart bodyPart)
// TODO Auto-generated method stub
return 0;
;
reader.processEmailAttachements();
Getting Autheticate Failed.. But username password is correct. and i am able to login outlook 365 with the username and password. ONLY difference I am seeing there is certitificate displayed on the browser.. Do I need to use any certificate to read the mail?
javax.mail.AuthenticationFailedException: LOGIN failed.
at com.sun.mail.imap.IMAPStore.protocolConnect
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.