Reputation: 477
I am able to send the emails using java program to james email server, but I am unable to read the emails from inbox and I am getting following exception
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at emailtest.emailtest.EmailClient.checkInbox(EmailClient.java:56)
at emailtest.emailtest.EmailClient.main(EmailClient.java:88)
debug message
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
Show INBOX for largepayloadrejecct2@soa-mq-target.subnet2ad1phx.paasinfratoophx.com
DEBUG: getProvider() returning javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle]
DEBUG IMAP: mail.imap.fetchsize: 16384
DEBUG IMAP: mail.imap.ignorebodystructuresize: false
DEBUG IMAP: mail.imap.statuscachetimeout: 1000
DEBUG IMAP: mail.imap.appendbuffersize: -1
DEBUG IMAP: mail.imap.minidletime: 10
DEBUG IMAP: closeFoldersOnStoreFailure
DEBUG IMAP: trying to connect to host "soa-mq-target.subnet2ad1phx.paasinfratoophx.com", port 143, isSSL false
* OK JAMES IMAP4rev1 Server soa-mq-target.subnet2ad1phx.paasinfratoophx.com is ready.
A0 CAPABILITY
* CAPABILITY NAMESPACE SEARCHRES QRESYNC UNSELECT ACL ENABLE QUOTA WITHIN SASL-IR CHILDREN UIDPLUS AUTH=PLAIN IDLE LITERAL+ I18NLEVEL=1 MOVE IMAP4REV1 ANNOTATION ESEARCH
A0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=soa-mq-target.subnet2ad1phx.paasinfratoophx.com, user=largepayloadrejecct2, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: A1 NO AUTHENTICATE failed. Authentication failed.
DEBUG IMAP: trying to connect to host "soa-mq-target.subnet2ad1phx.paasinfratoophx.com", port 143, isSSL false
* OK JAMES IMAP4rev1 Server soa-mq-target.subnet2ad1phx.paasinfratoophx.com is ready.
B0 CAPABILITY
* CAPABILITY NAMESPACE SEARCHRES QRESYNC UNSELECT ACL ENABLE QUOTA WITHIN SASL-IR CHILDREN UIDPLUS AUTH=PLAIN IDLE LITERAL+ I18NLEVEL=1 MOVE IMAP4REV1 ANNOTATION ESEARCH
B0 OK CAPABILITY completed.
DEBUG IMAP: AUTH: PLAIN
DEBUG IMAP: protocolConnect login, host=soa-mq-target.subnet2ad1phx.paasinfratoophx.com, user=largepayloadrejecct2, password=<non-null>
DEBUG IMAP: AUTHENTICATE PLAIN command trace suppressed
DEBUG IMAP: AUTHENTICATE PLAIN command result: B1 NO AUTHENTICATE failed. Authentication failed.
Exception in thread "main" javax.mail.AuthenticationFailedException: AUTHENTICATE failed. Authentication failed.
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:732)
at javax.mail.Service.connect(Service.java:388)
at javax.mail.Service.connect(Service.java:246)
at emailtest.emailtest.EmailClient.checkInbox(EmailClient.java:56)
at emailtest.emailtest.EmailClient.main(EmailClient.java:88)
the program I have used to read the emails
package emailtest.emailtest;
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class EmailClient extends Authenticator
{
public static final int SHOW_MESSAGES = 1;
public static final int CLEAR_MESSAGES = 2;
public static final int SHOW_AND_CLEAR =
SHOW_MESSAGES + CLEAR_MESSAGES;
protected String from;
protected Session session;
protected PasswordAuthentication authentication;
public EmailClient(String user, String host,String pass) {
this(user, host,pass, false);
}
public EmailClient(String user, String host,String pass, boolean debug) {
from = user + '@' + host;
authentication = new PasswordAuthentication(user, pass);
Properties props = new Properties();
props.put("mail.user", user);
props.put("mail.host", host);
props.put("mail.port", 143);
props.put("mail.debug", debug ? "true" : "false");
props.put("mail.store.protocol", "imap");
props.put("mail.transport.protocol", "smtp");
session = Session.getInstance(props, this);
}
public PasswordAuthentication getPasswordAuthentication() {
return authentication;
}
public void checkInbox(int mode)
throws MessagingException, IOException {
if (mode == 0) return;
boolean show = (mode & SHOW_MESSAGES) > 0;
boolean clear = (mode & CLEAR_MESSAGES) > 0;
String action =
(show ? "Show" : "") +
(show && clear ? " and " : "") +
(clear ? "Clear" : "");
System.out.println(action + " INBOX for " + from);
Store store = session.getStore("imap");
// Store store = session.getStore();
store.connect( "soa-mq-target.subnet2ad1phx.paasinfratoophx.com","largepayloadrejecct2","Adapterqa1");
Folder root = store.getDefaultFolder();
Folder inbox = root.getFolder("Inbox");
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
if (msgs.length == 0 && show)
{
System.out.println("No messages in inbox");
}
for (int i = 0; i < msgs.length; i++)
{
MimeMessage msg = (MimeMessage)msgs[i];
if (show)
{
System.out.println(" From: " + msg.getFrom()[0]);
System.out.println(" Subject: " + msg.getSubject());
System.out.println(" Content: " + msg.getContent());
}
if (clear)
{
msg.setFlag(Flags.Flag.DELETED, true);
}
}
inbox.close(true);
store.close();
System.out.println();
}
public static void main(String[] args) throws Exception{
// CREATE CLIENT INSTANCES largepayloadrejecct2@soa-mq-target.subnet2ad1phx.paasinfratoophx.com
EmailClient emailClient = new EmailClient("largepayloadrejecct2", "soa-mq-target.subnet2ad1phx.paasinfratoophx.com","Adapterqa1", true);
// LIST MESSAGES FOR email client
emailClient.checkInbox(EmailClient.SHOW_MESSAGES);
}
}
but using this below program I am able to send the emails to the email server.
package emailtest.emailtest;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class EmailSender {
public EmailSender() {
}
public static void sendEmail( String to, String subject, String from, String content) throws Exception{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", "smtp");
props.setProperty("mail.host", "soa-mq-target.subnet2ad1phx.paasinfratoophx.com");
props.setProperty("mail.user", "largepayloadrejecct4");
props.setProperty("mail.password", "Adapterqa1");
Session mailSession = Session.getDefaultInstance(props, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.addFrom(new Address[] { new InternetAddress("largepayloadrejecct4@soa-mq-target.subnet2ad1phx.paasinfratoophx.com",from)}); // the reply to email address and a logical descriptor of the sender of the email!
message.setSubject(subject);
message.setContent(content, "text/plain");
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
transport.connect();
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
}
public static void main (String[] args) throws Exception {
String content = "Some test content.";
EmailSender.sendEmail("largepayloadrejecct2@soa-mq-target.subnet2ad1phx.paasinfratoophx.com","An interesting message","THE APP72",content);
System.out.println("email sent ");
}
}
Also I have checked the imap is enabled
<imapservers>
<imapserver enabled="true">
<jmxName>imapserver</jmxName>
<bind>soa-mq-target.subnet2ad1phx.paasinfratoophx.com:143</bind>
:
:
<!-- Set the maximum simultaneous incoming connections for this service -->
<connectionLimit>0</connectionLimit>
<!-- Set the maximum simultaneous incoming connections per IP for this service -->
<connectionLimitPerIP>0</connectionLimitPerIP>
<plainAuthDisallowed>false</plainAuthDisallowed>
<auth>
<plainAuthEnabled>true</plainAuthEnabled>
</auth>
</imapserver>
It is saying Authenticate failed but using the same authentication I am able send the emails. In IMAP is there a different way to pass the authentication?
I tried multiple things (Note: ssl is not enabled)
I am not sure what wrong I am doing , please suggest
Upvotes: 0
Views: 100