Depth of the IBM MQ - java

Can you please help me, I am trying to find the ibm mq depth with PCFAgent over SSL channel.
Security.setProperty("ssl.SocketFactory.provider", "com.ibm.jsse2.SSLSocketFactoryImpl");
Security.setProperty("ssl.ServerSocketFactory.provider", "com.ibm.jsse2.SSLServerSocketFactoryImpl");
System.setProperty("javax.net.ssl.trustStore","abc-dev.jks");
System.setProperty("javax.net.ssl.trustStorePassword","abcdabcd");
System.setProperty("javax.net.ssl.keyStore", "abc-dev.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "abcdabcd");
MQEnvironment.sslCipherSuite = "TLS_RSA_WITH_AES_128_CBC_SHA";
int attrs[] = { 2016, 3 };
System.out.println("parameter creation");
PCFParameter parameters[] = { new MQCFST(2016, "*"), new MQCFIN(20, 1),
new MQCFIL(1002, attrs) };
String name = null;
Integer depth = null;
System.out.println("parameter creation end");
try {
PCFAgent agent;
if (args.length == 1) {
System.out.print("Connecting to local queue manager " + args[0]
+ "... ");
agent = new PCFAgent(args[0]);
} else {
System.out.print("Connecting to queue manager at " + args[0]
+ ":" + args[1] + " over channel " + args[2] + "... ");
agent = new PCFAgent(args[0], Integer.parseInt(args[1]),
args[2]);
}
System.out.println("Connected.");
System.out.print("Sending PCF request... ");
com.ibm.mq.MQMessage responses[] = agent.send(13, parameters);
System.out.println("Received reply.");
for (int i = 0; i < responses.length; i++) {
MQCFH cfh = new MQCFH(responses[i]);
if (cfh.reason == 0) {
for (int j = 0; j < cfh.parameterCount; j++) {
PCFParameter p = PCFParameter
.nextParameter(responses[i]);
switch (p.getParameter()) {
case 2016:
name = (String) p.getValue();
break;
case 3: // '\003'
depth = (Integer) p.getValue();
break;
}
}
System.out.println("Queue " + name + " curdepth " + depth);
} else {
System.out.println("PCF error:\n" + cfh);
for (int j = 0; j < cfh.parameterCount; j++)
System.out.println(PCFParameter
.nextParameter(responses[0]));
}
}
System.out.print("Disconnecting... ");
agent.disconnect();
System.out.println("Done.");
} catch (ArrayIndexOutOfBoundsException abe) {
System.out
.println("Usage: \n\tjava ListQueueDepth queue-manager\n\tjava ListQueueDepth host port channel");
} catch (NumberFormatException nfe) {
System.out.println("Invalid port: " + args[1]);
System.out
.println("Usage: \n\tjava ListQueueDepth queue-manager\n\tjava ListQueueDepth host port channel");
} catch (MQException mqe) {
System.err.println(mqe);
} catch (IOException ioe) {
System.err.println(ioe);
}
When I try run this program remotely, I am getting the following exception:
com.ibm.mq.MQException: MQJE001: Completion Code 2, Reason 2035
EDIT to add additional clarifying details from the comments:
The MQ Admin found an error in the AMQERR01.LOG related to the SYSTEM.DEFAULT.MODEL.QUEUE at the same time the application receives the 2035.
The same program works for a non-SSL channel when I take off the security settings and ciphersuite.

Well, I don't see where you set a UserID in the code. Therefore, you are sending a blank UserID which if the channel's MCAUSER is blank means, it becomes 'mqm' UserID because of an old security hole.
For MQ v7.1 or higher, the default CHLAUTH rule will block the connection if you are on a 'SYSTEM' channel. Or for MQ v8.0 or higher, the channel may require a UserID and Password. You need to look at either the event queue or queue manager logs for the reason for the RC of 2035 (not authorized).
int attrs[] = { 2016, 3 };
PCFParameter parameters[] = { new MQCFST(2016, "*"),
new MQCFIN(20, 1),
new MQCFIL(1002, attrs) };
Also, what is with the hard-coded values? Did you decompile someone else's program?
Why aren't you using the IBM MQ supplied defines? Here's what it should look like:
int[] attrs = {
CMQC.MQCA_Q_NAME,
CMQC.MQIA_CURRENT_Q_DEPTH
};
PCFParameter[] parameters = {
new MQCFST (CMQC.MQCA_Q_NAME, "*"),
new MQCFIN (CMQC.MQIA_Q_TYPE, CMQC.MQQT_LOCAL),
new MQCFIL (CMQCFC.MQIACF_Q_ATTRS, attrs)
};

The PCFAgent class creates a dynamic queue for the response messages from the command server. By default the model queue used is SYSTEM.DEFAULT.MODEL.QUEUE. The user needs to have OAM permission +get +dsp on the model queue.
If the program works when connecting to a non-SSL channel then the connection must be resolving to a different userid that has the required OAM permission against the SYSTEM.DEFAULT.MODEL.QUEUE.
The error in the queue manager's AMQERR01.LOG would show the userid that is missing the permission(s) along with the the specific permission(s) missing.
Depending on what version of MQ client jar files you are using, if you do not specify a UserID the MQ client will either send blank UserID or the UserID the java process is running under. On the queue manager side if the MCAUSER attribute of the channel is blank, then it would inherit either mqm if a blank UserID was received or the UserID your process is running under. If the MCAUSER attribute of the channel is not blank that is the UserID that will be used for permission.

Related

Javamail : Setting timeout for folder.search

I have been using Javamail (version 1.6.4) for a while now, mostly for event listening, mail parsing and copy/delete mails (from Exchnage using IMAPS). Lately i was asked to use sub-folders for a business usecase. When moving mail from the Inbox folder to a sub-folder the UID changes so i'm using folder.search() in order to find the new UID. This works most of the times but sometimes the search goes indefnitley (or its duration is very long) which may lag the entire application.
Is there a way to set a timeout (or other way to throw an exception if it runs too long) for folder.search()?
I understood that imap search is done on the server side but i just wanted to validate. this is an example of how i send search to the server (we can assume subject is unique for this discussion):
private static String findMailIdBySubject(String mailbox, String srcFolder, String subject) {
Folder srcFolderObj = null;
boolean wasConnectionEstablished = connectToMail(mailbox);
if (!wasConnectionEstablished) {
return null;
}
// get mailboxStore object
MailboxStore mailboxStore = MailBoxStoreList.getMailStoreByMailBox(mailbox);
try {
// Open inbox folder to get messages metadata
srcFolderObj = mailboxStore.getStore().getFolder(srcFolder);
srcFolderObj.open(Folder.READ_WRITE);
// create search Term
SearchTerm term = new SearchTerm() {
private static final long serialVersionUID = 7L;
#Override
public boolean match(Message message) {
try {
String mailSubject = message.getSubject();
if (mailSubject == null) {
mailSubject = "";
}
if (mailSubject.equals(subject)) {
return true;
}
} catch (MessagingException ex) {
log.error("Failed to search for mail with mailbox: " + mailbox + " in folder: " + srcFolder
+ " subject: " + subject + " Error: " + ExceptionUtils.getStackTrace(ex));
}
return false;
}
};
// search for the relevant message
Message[] messages = srcFolderObj.search(term);
UIDFolder uf = (UIDFolder) srcFolderObj;
return String.valueOf(uf.getUID(messages[0]));
} catch (Exception e) {
log.error("Subject: Failed to find id of mail in mailbox " + mailbox + " in folder " + srcFolder
+ " , Error: " + ExceptionUtils.getStackTrace(e));
return null;
} finally {
try {
if (srcFolderObj != null && srcFolderObj.isOpen()) {
srcFolderObj.close();
}
} catch (Exception e) {
}
}
}
i also tried to replace SearchTerm override with the following but performance was the same:
SearchTerm searchTerm = new SubjectTerm(subject);
Thanks in advance!

IBM MQ CLIENT java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path exception [duplicate]

This question already has answers here:
java.lang.UnsatisfiedLinkError
(6 answers)
Closed 6 years ago.
I am tring to connect to IBM websphere Client with a Java Programme
the Following are the code:=
import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQMessage;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
/**
* Simple example program
*/
public class MQSample {
// code identifier
static final String sccsid = "#(#) MQMBID sn=p750-002-131001_DE su=_FswqMCqGEeOZ3ui-rZDONA pn=MQJavaSamples/wmqjava/MQSample.java";
// define the name of the QueueManager
private static final String qManager = "QM_ORANGE";
// and define the name of the Queue
private static final String qName = "SYSTEM.DEFAULT.LOCAL.QUEUE";
// private static final String qName = "QM_APPLE";
public static void main(String args[]) {
try {
System.out.println("Connecting to queue manager: " + qManager);
MQQueueManager qMgr = new MQQueueManager(qManager);
int openOptions =1| 16;
System.out.println("Accessing queue: " + qName);
MQQueue queue = qMgr.accessQueue(qName, openOptions);
MQMessage msg = new MQMessage();
msg.writeUTF("Hello, World!");
MQPutMessageOptions pmo = new MQPutMessageOptions();
System.out.println("Sending a message...");
queue.put(msg, pmo);
// Now get the message back again. First define a WebSphere MQ
// message
// to receive the data
// MQMessage rcvMessage = new MQMessage();
// Specify default get message options
// MQGetMessageOptions gmo = new MQGetMessageOptions();
// Get the message off the queue.
// System.out.println("...and getting the message back again");
// queue.get(rcvMessage, gmo);
// And display the message text...
//String msgText = rcvMessage.readUTF();
// System.out.println("The message is: " + msgText);
// Close the queue
System.out.println("Closing the queue");
queue.close();
// Disconnect from the QueueManager
System.out.println("Disconnecting from the Queue Manager");
qMgr.disconnect();
System.out.println("Done!");
}
catch (MQException ex) {
System.out.println("A WebSphere MQ Error occured : Completion Code " + ex.completionCode
+ " Reason Code " + ex.reasonCode);
ex.printStackTrace();
for (Throwable t = ex.getCause(); t != null; t = t.getCause()) {
System.out.println("... Caused by ");
t.printStackTrace();
}
}
catch (java.io.IOException ex) {
System.out.println("An IOException occured whilst writing to the message buffer: " + ex);
}
return;
}
}
but right now i am getting following error
Exception in thread "main" java.lang.UnsatisfiedLinkError: no mqjbnd05 in java.library.path
at java.lang.ClassLoader.loadLibrary(Unknown Source)
at java.lang.Runtime.loadLibrary0(Unknown Source)
at java.lang.System.loadLibrary(Unknown Source)
at com.ibm.mq.MQSESSION.loadLib(MQSESSION.java:872)
at com.ibm.mq.server.MQSESSION$1.run(MQSESSION.java:228)
at java.security.AccessController.doPrivileged(Native Method)
at com.ibm.mq.server.MQSESSION.<clinit>(MQSESSION.java:222)
at com.ibm.mq.MQSESSIONServer.getMQSESSION(MQSESSIONServer.java:70)
at com.ibm.mq.MQSESSION.getSession(MQSESSION.java:492)
at com.ibm.mq.MQManagedConnectionJ11.<init>(MQManagedConnectionJ11.java:168)
at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11._createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:179)
at com.ibm.mq.MQBindingsManagedConnectionFactoryJ11.createManagedConnection(MQBindingsManagedConnectionFactoryJ11.java:215)
at com.ibm.mq.StoredManagedConnection.<init>(StoredManagedConnection.java:84)
at com.ibm.mq.MQSimpleConnectionManager.allocateConnection(MQSimpleConnectionManager.java:168)
at com.ibm.mq.MQQueueManagerFactory.obtainBaseMQQueueManager(MQQueueManagerFactory.java:772)
at com.ibm.mq.MQQueueManagerFactory.procure(MQQueueManagerFactory.java:697)
at com.ibm.mq.MQQueueManagerFactory.constructQueueManager(MQQueueManagerFactory.java:657)
at com.ibm.mq.MQQueueManagerFactory.createQueueManager(MQQueueManagerFactory.java:153)
at com.ibm.mq.MQQueueManager.<init>(MQQueueManager.java:451)
at MQSample.main(MQSample.java:30)
when i refered about this everyone saying to put mqjbnd.dll in java.library i put that file in that path also still not working
IBM MQ CLIENT java.lang.UnsatisfiedLinkError: no mqjbnd05 in
java.library.path exception
Your title pretty much says it all. 'MQ Client' generally means that the queue manager is REMOTE to where you are running your application. But 'no mqjbnd05' means that you are attempting to connect to the queue manager in bindings mode, queue manager is running on the same server as your queue manager.
99% of the time an application gets that errors is because the application and queue manager are running on separate servers and the application is not specifying: channel name, hostname/IP address & port #.
Note: An application can connect in 2 ways to a queue manager:
(1) client mode - meaning the application and queue manager are running on separate servers and the application is not specifying: channel name, hostname/IP address & port #.
(2) bindings mode - meaning the application and queue manager are running on the same servers (no networking information is specified).
Note: Don't use MQEnvironment class but rather put the connection information in a Hashtable and pass it to the MQQueueManager class. MQEnvironment is NOT thread safe.
Here's a working sample MQ application that will connect (client mode) to a remote queue manager:
import java.io.IOException;
import java.util.Hashtable;
import com.ibm.mq.*;
import com.ibm.mq.constants.CMQC;
/**
* Java class to connect to MQ. Post and Retrieve messages.
*
* Sample Command Line Parameters
* -h 127.0.0.1 -p 1414 -c TEST.CHL -m MQA1 -q TEST.Q1 -u userid -x password
*/
public class MQClientTest
{
private Hashtable<String, String> params = null;
private Hashtable<String, Object> mqht = null;
private String qManager;
private String inputQName;
/**
* The constructor
*/
public MQClientTest()
{
super();
}
/**
* Make sure the required parameters are present.
*
* #return true/false
*/
private boolean allParamsPresent()
{
boolean b = params.containsKey("-h") && params.containsKey("-p") &&
params.containsKey("-c") && params.containsKey("-m") &&
params.containsKey("-u") && params.containsKey("-x") &&
params.containsKey("-q");
if (b)
{
try
{
Integer.parseInt((String) params.get("-p"));
}
catch (NumberFormatException e)
{
b = false;
}
}
return b;
}
/**
* Extract the command-line parameters and initialize the MQ variables.
*
* #param args
* #throws IllegalArgumentException
*/
private void init(String[] args) throws IllegalArgumentException
{
params = new Hashtable<String, String>();
if (args.length > 0 && (args.length % 2) == 0)
{
for (int i = 0; i < args.length; i += 2)
{
params.put(args[i], args[i + 1]);
}
}
else
{
throw new IllegalArgumentException();
}
if (allParamsPresent())
{
qManager = (String) params.get("-m");
inputQName = (String) params.get("-q");
mqht = new Hashtable<String, Object>();
mqht.put(CMQC.CHANNEL_PROPERTY, params.get("-c"));
mqht.put(CMQC.HOST_NAME_PROPERTY, params.get("-h"));
try
{
mqht.put(CMQC.PORT_PROPERTY, new Integer(params.get("-p")));
}
catch (NumberFormatException e)
{
mqht.put(CMQC.PORT_PROPERTY, new Integer(1414));
}
mqht.put(CMQC.USER_ID_PROPERTY, params.get("-u"));
mqht.put(CMQC.PASSWORD_PROPERTY, params.get("-x"));
// I don't want to see MQ exceptions at the console.
MQException.log = null;
}
else
{
throw new IllegalArgumentException();
}
}
/**
* Method to put then get a message to/from a queue.
*/
public void putAndGetMessage()
{
MQQueueManager qMgr = null;
MQQueue queue = null;
MQMessage putMessage = null;
MQMessage getMessage = null;
int openOptions = CMQC.MQOO_INPUT_AS_Q_DEF | CMQC.MQOO_OUTPUT + CMQC.MQOO_FAIL_IF_QUIESCING;
MQGetMessageOptions gmo = new MQGetMessageOptions();
gmo.options = CMQC.MQGMO_NO_WAIT + CMQC.MQGMO_FAIL_IF_QUIESCING;
MQPutMessageOptions pmo = new MQPutMessageOptions();
pmo.options = CMQC.MQPMO_FAIL_IF_QUIESCING;
String msg = "Hello World, WelCome to MQ.";
try
{
qMgr = new MQQueueManager(qManager, mqht);
queue = qMgr.accessQueue(inputQName, openOptions);
putMessage = new MQMessage();
putMessage.writeUTF(msg);
// put the message on the queue
queue.put(putMessage, pmo);
System.out.println("Message is put on MQ.");
// get message from MQ.
getMessage = new MQMessage();
// assign message id to get message.
getMessage.messageId = putMessage.messageId;
/*
* Tell the queue manager that we want a message with a specific MsgID.
*/
gmo.matchOptions = CMQC.MQMO_MATCH_MSG_ID;
// get message options.
queue.get(getMessage, gmo);
String retreivedMsg = getMessage.readUTF();
System.out.println("Message got from MQ: " + retreivedMsg);
}
catch (MQException e)
{
System.err.println("CC=" + e.completionCode + " : RC=" + e.reasonCode);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
try
{
if (queue != null)
queue.close();
}
catch (MQException e)
{
System.err.println("MQCLOSE CC=" + e.completionCode + " : RC="
+ e.reasonCode);
}
try
{
if (qMgr != null)
qMgr.disconnect();
}
catch (MQException e2)
{
System.err.println("MQDISC CC=" + e2.completionCode + " : RC="
+ e2.reasonCode);
}
}
}
public static void main(String[] args)
{
System.out.println("Processing Main...");
MQClientTest clientTest = new MQClientTest();
try
{
// initialize MQ.
clientTest.init(args);
// put and retrieve message from MQ.
clientTest.putAndGetMessage();
}
catch (IllegalArgumentException e)
{
System.out.println("Usage: java MQClientTest -h host -p port -c channel -m QueueManagerName -q QueueName -u userid -x password");
System.exit(1);
}
System.out.println("Done!");
}
}
There is nothing in the question to indicate which version of MQ client is being used, how it was installed, whether the IBM-provided code works, or whether the environment was set correctly. That makes this more of a "how do I configure and test MQ client?" kind of question so I'll answer it that way.
Back-level MQ clients were always installed using IBM's full-client install media. Newer clients can be installed using IBM's Java-only install media. It is always recommended to use one of these methods.
The one thing to not do (which coincidentally is the thing done most often) is to simply grab the jar files from the MQ Server installation. The reasons for this include:
The IBM installer lays down a known-good set of files.
Maintenance can be applied to an installation made using IBM's installer.
When using IBM's install methods, things like trace directories and the location of the MQClient.ini file are predictable.
So first thing is to make sure you are running from the latest IBM-provided full client or Java-only install media. Alternatively, install IBM MQ Advanced for Developers which delivers a full MQ install to the desktop, including all the client support. MQ Advanced for Developers is free for individual use.
Prior to launching code, set the environment. See:
Environment variables relevant to IBM MQ classes for Java
Environment variables used by IBM MQ classes for JMS
Per the docs:
On Windows, all the environment variables are set automatically during
installation. On any other platform, you must set them yourself. On a
UNIX system, you can use the script setjmsenv (if you are using a
32-bit JVM) or setjmsenv64 (if you are using a 64-bit JVM) to set the
environment variables. On AIX, HP-UX, Linux, and Solaris, these
scripts are in the MQ_INSTALLATION_PATH/java/bin directory.
IBM provides lots of sample code. On *nix systems this is in /opt/mqm/samp/. On windows it's in the [MQ install directory]\tools. If the full client is installed try the compiled C code first, like amqsgetc. This establishes whether basic connectivity is in place. Once you know the channel connectivity works, try the Java or JMS samples.
Getting back to the original post, before we can help we'd need to know which of the above steps had already been completed and something about the configuration. Otherwise what you get back generally leads you down a path to a setup in which configuration is almost guaranteed to be hosed: "Try adding this library here," or "try mucking about with your CLASSPATH like this..." Such trial-and-error approaches often work but
are unsupportable and lead to problems over time.

Sporadically getting javax.mail.FolderClosedException while reading gmail emails using javamail

I am trying to read javamail inbox and perform search. For this, I am fetching the latest 100 messages and then I am iterating through each to see if they have the sender for whom I am searching for. If its matching, I get its content via getContent().
Here is my javamail code snippet:
try {
Properties properties = new Properties();
properties.setProperty("mail.store.protocol", "imap");
properties.put("mail.imaps.starttls.enable", "true");
properties.put("mail.imap.socketFactory.port", "587");
System.setProperty("javax.net.debug", "ssl");
System.out.println("prop " + properties.getProperty("mail.smtp.port"));
Session session = Session.getDefaultInstance(properties, null);
// session.setDebug(true);
Store store = null;
store = session.getStore("imaps");
store.connect("imap.gmail.com", username, password);
Folder inbox;
inbox = store.getFolder("Inbox");
/* Others GMail folders :
* [Gmail]/All Mail This folder contains all of your Gmail messages.
* [Gmail]/Drafts Your drafts.
* [Gmail]/Sent Mail Messages you sent to other people.
* [Gmail]/Spam Messages marked as spam.
* [Gmail]/Starred Starred messages.
* [Gmail]/Trash Messages deleted from Gmail.
*/
inbox.open(Folder.READ_WRITE);
Message msgs[] = inbox.getMessages(inbox.getMessageCount() - lastHistory, inbox.getMessageCount());
System.out.println("MSgs.length " + msgs.length);
ArrayList<Message> aList = new ArrayList<Message>();
appendTextToConsole("Searching for appropriate messages!!");
for (int ii = msgs.length - 1; ii >= 0; ii--) {
Message msg = msgs[ii];
Address[] in = msg.getFrom();
String sender = InternetAddress.toString(in);
System.out.println((++index) + "Sender: " + sender);
boolean read = msg.isSet(Flags.Flag.SEEN);
if (sender.contains(googleId) && !read) {
//This line below gives FolderClosedException sporadically
Object content = msg.getContent();
if (content instanceof Multipart) {
Multipart mp = (Multipart) content;
for (int i = 0; i < mp.getCount(); i++) {
BodyPart bp = mp.getBodyPart(i);
if (Pattern
.compile(Pattern.quote("text/html"),
Pattern.CASE_INSENSITIVE)
.matcher(bp.getContentType()).find()) {
// found html part
String html = (String) bp.getContent();
Element element = Jsoup.parse(html);
List<Element> anchors = element.getElementsByTag("a");
for (Element e : anchors) {
if (e.attr("href").startsWith("https://www.google.com/url?rct=j&sa=t&url=")
&& !e.attr("style").equalsIgnoreCase("text-decoration:none")) {
String url = e.attr("href");
String title = e.text();
String agency = e.parent().parent().child(1).child(0).child(0).text();
String message = e.parent().parent().child(1).child(0).child(1).text();
String flagUrl = e.parent().parent().child(1).child(1).child(0).child(0).child(3).child(0).attr("href");
System.out.println("URL: " + url);
System.out.println("Title: " + title);
System.out.println("agency: " + agency);
System.out.println("Message: " + message);
System.out.println("flagURL: " + flagUrl);
AbstractMessage ams = new AbstractMessage(url, title, agency, message, flagUrl);
aMsgs.add(ams);
}
}
//System.out.println((String) bp.getContent());
} else {
// some other bodypart...
}
}
try {
inbox.close(true);
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
appendTextToConsole("Done searching for appropriate messages!!");
} catch (Exception mex) {
appendTextToConsole(mex.getMessage());
mex.printStackTrace();
}
But the most irritating thing is that after fetching a few messages, sporadically a javax.mail.FolderClosedException is thrown due to unknown reasons. Now my question is that how do I deal with this scenario? And how do ideal mail clients made using javamail deal with it?
Turn on session debugging and you might get more clues as to what's going on.
Note that the server will close the connection if you're not using it.
And of course all sorts of network failures are possible.

Retrieving all unread emails using javamail with POP3 protocol

I am trying to access my gmail account and retrieve the information of all unread emails from that.
I have written my code after referring many links. I am giving a few links for reference.
Send & Receive emails through a GMail account using Java
Java Code to Receive Mail using JavaMailAPI
To test my code, I created one Gmail account. So I received 4 messages in that from Gmail.
I run my application after checking number of mails. That showed correct result. 4 unread mails.
All the infomation was being displayed (e.g. date, sender, content, subject, etc.)
Then I logged in to my new account, read one of the emails and rerun my application.
Now the count of unread message should have been 3, but it displays "No. of Unread Messages : 0"
I am copying the code here.
public class MailReader
{
Folder inbox;
// Constructor of the calss.
public MailReader() {
System.out.println("Inside MailReader()...");
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
/* Set the mail properties */
Properties props = System.getProperties();
// Set manual Properties
props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.port", "995");
props.setProperty("mail.pop3.socketFactory.port", "995");
props.put("mail.pop3.host", "pop.gmail.com");
try
{
/* Create the session and get the store for read the mail. */
Session session = Session.getDefaultInstance(
System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect("pop.gmail.com", 995, "abc#gmail.com",
"paasword");
/* Mention the folder name which you want to read. */
// inbox = store.getDefaultFolder();
// inbox = inbox.getFolder("INBOX");
inbox = store.getFolder("INBOX");
/* Open the inbox using store. */
inbox.open(Folder.READ_ONLY);
/* Get the messages which is unread in the Inbox */
Message messages[] = inbox.search(new FlagTerm(new Flags(
Flags.Flag.SEEN), false));
System.out.println("No. of Unread Messages : " + messages.length);
/* Use a suitable FetchProfile */
FetchProfile fp = new FetchProfile();
fp.add(FetchProfile.Item.ENVELOPE);
fp.add(FetchProfile.Item.CONTENT_INFO);
inbox.fetch(messages, fp);
try
{
printAllMessages(messages);
inbox.close(true);
store.close();
}
catch (Exception ex)
{
System.out.println("Exception arise at the time of read mail");
ex.printStackTrace();
}
}
catch (MessagingException e)
{
System.out.println("Exception while connecting to server: "
+ e.getLocalizedMessage());
e.printStackTrace();
System.exit(2);
}
}
public void printAllMessages(Message[] msgs) throws Exception
{
for (int i = 0; i < msgs.length; i++)
{
System.out.println("MESSAGE #" + (i + 1) + ":");
printEnvelope(msgs[i]);
}
}
public void printEnvelope(Message message) throws Exception
{
Address[] a;
// FROM
if ((a = message.getFrom()) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("FROM: " + a[j].toString());
}
}
// TO
if ((a = message.getRecipients(Message.RecipientType.TO)) != null) {
for (int j = 0; j < a.length; j++) {
System.out.println("TO: " + a[j].toString());
}
}
String subject = message.getSubject();
Date receivedDate = message.getReceivedDate();
Date sentDate = message.getSentDate(); // receivedDate is returning
// null. So used getSentDate()
String content = message.getContent().toString();
System.out.println("Subject : " + subject);
if (receivedDate != null) {
System.out.println("Received Date : " + receivedDate.toString());
}
System.out.println("Sent Date : " + sentDate.toString());
System.out.println("Content : " + content);
getContent(message);
}
public void getContent(Message msg)
{
try {
String contentType = msg.getContentType();
System.out.println("Content Type : " + contentType);
Multipart mp = (Multipart) msg.getContent();
int count = mp.getCount();
for (int i = 0; i < count; i++) {
dumpPart(mp.getBodyPart(i));
}
} catch (Exception ex) {
System.out.println("Exception arise at get Content");
ex.printStackTrace();
}
}
public void dumpPart(Part p) throws Exception {
// Dump input stream ..
InputStream is = p.getInputStream();
// If "is" is not already buffered, wrap a BufferedInputStream
// around it.
if (!(is instanceof BufferedInputStream)) {
is = new BufferedInputStream(is);
}
int c;
System.out.println("Message : ");
while ((c = is.read()) != -1) {
System.out.write(c);
}
}
public static void main(String args[]) {
new MailReader();
}
}
I searched on google, but I found that you should use Flags.Flag.SEEN to read unread emails.
But thats not showing correct results in my case.
Can someone point out where I might be doing some mistake?
If you need whole code, I can edit my post.
Note: I edited my question to include whole code instead of snippet I had posted earlier.
Your code should work. You can also use the Folder.getUnreadMessageCount() method if all you want is the count.
JavaMail can only tell you what Gmail tells it. Perhaps Gmail thinks that all those messages have been read? Perhaps the Gmail web interface is marking those messages read? Perhaps you have another application monitoring the folder for new messages?
Try reading an unread message with JavaMail and see if the count changes.
You might find it useful to turn on session debugging so you can see the actual IMAP responses that Gmail is returning; see the JavaMail FAQ.
You cannot fetch unread messages with POP3. From JavaMail API:
POP3 supports no permanent flags (see Folder.getPermanentFlags()). In
particular, the Flags.Flag.RECENT flag will never be set for POP3
messages. It's up to the application to determine which messages in a
POP3 mailbox are "new".
You can use IMAP protocol and use the SEEN flag like this:
public Message[] fetchMessages(String host, String user, String password, boolean read) {
Properties properties = new Properties();
properties.put("mail.store.protocol", "imaps");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore();
store.connect(host, user, password);
Folder emailFolder = store.getFolder("INBOX");
// use READ_ONLY if you don't wish the messages
// to be marked as read after retrieving its content
emailFolder.open(Folder.READ_WRITE);
// search for all "unseen" messages
Flags seen = new Flags(Flags.Flag.SEEN);
FlagTerm unseenFlagTerm = new FlagTerm(seen, read);
return emailFolder.search(unseenFlagTerm);
}
Another thing to notice is that POP3 doesn't handle folders. IMAP gets folders, POP3 only gets the Inbox. More info at: How to retrieve gmail sub-folders/labels using POP3?
Change inbox.open(Folder.READ_ONLY); to inbox.open(Folder.READ_WRITE);
It will change your mail as read in inbox.
Flags seen = new Flags(Flags.Flag.RECENT);
FlagTerm unseenFlagTerm = new FlagTerm(seen, false);
messages = inbox.search(unseenFlagTerm);
The correct flag to use is
Flags.Flag.RECENT
please use this method to get the unread mails
getNewMessageCount()
refer below link:
https://javamail.java.net/nonav/docs/api/com/sun/mail/imap/IMAPFolder.html

closing MQ connection

Good afternoon, I wrote a project to Get Park Queue Info from the IBM MQ, it has producing an error when attempting to close the connection though. It is written in java.
Under application in Event Viewer on the MQ machine it displays two errors. They are:
“Channel program ended abnormally.
Channel program ‘system.def.surconn’ ended abnormally. Look at previous error messages for channel program ‘system.def.surconn’ in the error files to determine the cause of the failure.
The other message states:
“Error on receive from host rnanaj (10.10.12.34)
An error occurred receiving data from rnanaj (10.10.12.34) over tcp/ip. This may be due to a communications failure. The return code from tcp/ip recv() call was 10054 (X’2746’). Record these values.”
This must be something how I try to connect or close the connection, below I have my code to connect and close, any ideas??
Connect:
_logger.info("Start");
File outputFile = new File(System.getProperty("PROJECT_HOME"), "run/" + this.getClass().getSimpleName() + "." + System.getProperty("qmgr") + ".txt");
FileUtils.mkdirs(outputFile.getParentFile());
Connection jmsConn = null;
Session jmsSession = null;
QueueBrowser queueBrowser = null;
BufferedWriter commandsBw = null;
try {
// get queue connection
MQConnectionFactory MQConn = new MQConnectionFactory();
MQConn.setHostName(System.getProperty("host"));
MQConn.setPort(Integer.valueOf(System.getProperty("port")));
MQConn.setQueueManager(System.getProperty("qmgr"));
MQConn.setChannel("SYSTEM.DEF.SVRCONN");
MQConn.setTransportType(JMSC.MQJMS_TP_CLIENT_MQ_TCPIP);
jmsConn = (Connection) MQConn.createConnection();
jmsSession = jmsConn.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue jmsQueue = jmsSession.createQueue("PARK");
// browse thru messages
queueBrowser = jmsSession.createBrowser(jmsQueue);
Enumeration msgEnum = queueBrowser.getEnumeration();
commandsBw = new BufferedWriter(new FileWriter(outputFile));
//
String line = "DateTime\tMsgID\tOrigMsgID\tCorrelationID\tComputerName\tSubsystem\tDispatcherName\tProcessor\tJobID\tErrorMsg";
commandsBw.write(line);
commandsBw.newLine();
while (msgEnum.hasMoreElements()) {
Message message = (Message) msgEnum.nextElement();
line = dateFormatter.format(new Date(message.getJMSTimestamp()))
+ "\t" + message.getJMSMessageID()
+ "\t" + message.getStringProperty("pkd_orig_jms_msg_id")
+ "\t" + message.getJMSCorrelationID()
+ "\t" + message.getStringProperty("pkd_computer_name")
+ "\t" + message.getStringProperty("pkd_subsystem")
+ "\t" + message.getStringProperty("pkd_dispatcher_name")
+ "\t" + message.getStringProperty("pkd_processor")
+ "\t" + message.getStringProperty("pkd_job_id")
+ "\t" + message.getStringProperty("pkd_sysex_msg");
_logger.info(line);
commandsBw.write(line);
commandsBw.newLine();
}
}
Close:
finally {
IO.close(commandsBw);
if (queueBrowser != null) { try { queueBrowser.close();} catch (Exception ignore) {}}
if (jmsSession != null) { try { jmsSession.close();} catch (Exception ignore) {}}
if (jmsConn != null) { try { jmsConn.stop();} catch (Exception ignore) {}}
}
As per the Javadoc for the connection object, the function of the stop() method is...
Temporarily stops a connection's
delivery of incoming messages.
So stop() doesn't actually sever the connection. You want the close() method.

Categories

Resources