How to check connection of javax.mail - java

properties.put("mail.imap.host", host);
properties.put("mail.imap.port", "993");
Session emailSession = Session.getDefaultInstance(properties);
Store store = emailSession.getStore("imaps");
store.connect(host, username, password);
This code is working well, but if the mail server is down, I got the expection:
javax.mail.MessagingException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?;
nested exception is:
com.sun.mail.iap.ConnectionException: * BYE JavaMail Exception: java.io.IOException: Connection dropped by server?
at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:731)
at javax.mail.Service.connect(Service.java:366)
at javax.mail.Service.connect(Service.java:246)
at eugene.neocortex.fm.mail.check.CheckingMails.fetch(CheckingMails.java:100)
So I need to check if server is available? And I read the documentation of javax.mail and cant find method, how do I can check if server is alive? Method isConnected() checks if currently connected, but I need to check connection to my mail server before this code or I got exception again on this code string:
Store store = emailSession.getStore("imaps");

Related

Can I use a Java code to send mail from Windows Server 2012 RS?

I initially tried to write a code to send mail from localhost, but I am not sure how to setup server. Went through help topics regarding that, didn't help much.
Is it possible to write a Java code to send mail from Windows Server 2012?
If not, please please help me setup localhost in my Windows Server 2012. I'm lost.
This is the code that I used:
public static void main(String[] args) throws AddressException, MessagingException {
Properties props = new Properties();
props.put("mail.smtp.host", "10.204.10.116");
props.put("mail.smtp.port", "443");
props.put("mail.debug", "true");
Session session = Session.getDefaultInstance(props);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("abc#gmail.com"));
message.setRecipient(RecipientType.TO, new InternetAddress("xyz#gmail.com"));
message.setSubject("Notification");
message.setText("Successful!", "UTF-8"); // as "text/plain"
message.setSentDate(new Date());
Transport.send(message);
}
}
This is the error that I got:
Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: 10.204.10.116, port: 443;
nested exception is:
java.net.ConnectException: Connection timed out: connect
Not sure what I'm supposed to make of this error.

Can't connect to IBMMQ MQRC_NOT_UNAUTHORIZED

I have installed IBMMQ 9.0 in windows running in virtualbox in my local and I have sample java code sending message to MQ.
here is the java code
MQQueueConnectionFactory cf = new MQQueueConnectionFactory();
cf.setHostName(MY_IP);
cf.setPort(1417);
cf.setTransportType(WMQConstants.WMQ_CM_CLIENT);
cf.setQueueManager("QM1_TEST");
cf.setChannel("QM1_TEST.CH");
MQQueueConnection connection = (MQQueueConnection)
cf.createQueueConnection();
MQQueueSession session = (MQQueueSession)
connection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
MQQueue queue = (MQQueue) session.createQueue("queue:///QUEUE_TEST");
MQQueueSender sender = (MQQueueSender) session.createSender(queue);
String message = "Test Message";
TextMessage textMessage = (TextMessage)
session.createTextMessage(message);
connection.start();
sender.send(textMessage);
The program receives this error.
com.ibm.msg.client.jms.DetailedJMSSecurityException: JMSWMQ2013: The
security authentication was not valid that was supplied for QueueManager
'QM1_TEST' with connection mode 'Client' and host name '192.168.0.24(1417)'.
Please check if the supplied username and password are correct on the
QueueManager to which you are connecting. at
com.ibm.msg.client.wmq.common.internal.Reason.reasonToException(Reason.java:531)
at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:215)
at com.ibm.msg.client.wmq.internal.WMQConnection.<init>(WMQConnection.java:424)
at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createV7ProviderConnection(WMQConnectionFactory.java:8475)
at com.ibm.msg.client.wmq.factories.WMQConnectionFactory.createProviderConnection(WMQConnectionFactory.java:7815)
at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl._createConnection(JmsConnectionFactoryImpl.java:303)
at com.ibm.msg.client.jms.admin.JmsConnectionFactoryImpl.createConnection(JmsConnectionFactoryImpl.java:236)
at com.ibm.mq.jms.MQConnectionFactory.createCommonConnection(MQConnectionFactory.java:6016)
at com.ibm.mq.jms.MQQueueConnectionFactory.createQueueConnection(MQQueueConnectionFactory.java:111)
at com.cwt.bpg.cbr.demo.ibm.mq.client.SendMessage.main(SendMessage.java:24)
Caused by: com.ibm.mq.MQException: JMSCMQ0001: IBM MQ call failed with compcode '2' ('MQCC_FAILED') reason '2035' ('MQRC_NOT_AUTHORIZED').
at com.ibm.msg.client.wmq.common.internal.Reason.createException(Reason.java:203)
... 8 more
Here is the log under my queue manager with significant details.
5/21/2018 16:42:46 - Process(4984.8) User(MUSR_MQADMIN) Program
(amqrmppa.exe) Host(MSEDGEWIN10) Installation(Installation1) VRMF(9.0.0.0)
QMgr(QM1_DEV) AMQ9776: Channel was blocked by userid
EXPLANATION:
The inbound channel 'QM1_DEV.CH' was blocked from address '10.xx.xx.xx'
because the active values of the channel were mapped to a userid which
should be blocked. The active values of the channel were 'MCAUSER(IEUser)
CLNTUSER(IEUser) ADDRESS(LWPHL1U012FXP)'
You are not providing a user and password, your are connecting with the default user identity.
QueueConnection createQueueConnection()
Creates a queue connection with default user identity.
You can either provide credentials:
QueueConnection createQueueConnection(String userid,String password)
By default the userid and password for any user in the mqm group on the MQ broker will have access to the queue managers.
The IBM MQ docs also have instructions for disabling security altogether.

java.rmi.ConnectException: Connection refused to host: 10.0.0.57 [duplicate]

This question already has answers here:
Java RMI : connection refused
(2 answers)
Closed 5 years ago.
Why I continue to receive following error? I tried to fix a problem by following posts on similar issue, however nothing seems to work.
java.rmi.ConnectException: Connection refused to host: 10.0.0.57; nested exception is:
java.net.ConnectException: Operation timed out
at .....
javax.rmi.ssl.SslRMIClientSocketFactory.createSocket(SslRMIClientSocketFactory.java:120)
at sun.rmi.transport.tcp.TCPEndpoint.newSocket(TCPEndpoint.java:613)
... 5 more
public static void main(String[] args) throws UnknownHostException {
// Bind to registry
System.setProperty(
"javax.net.ssl.keyStore",
"/Users/xxxx/keys/printserver.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty(
"javax.net.ssl.trustStore",
"//Users/xxxx/keys/printserver.jks");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
System.setProperty("javax.net.ssl.debug", "all");
//System.out.println(java.net.InetAddress.getLocalHost());
try {
System.setProperty("java.rmi.server.hostname", "191.234.6.152");
Registry r = LocateRegistry.getRegistry(null, 5099, new SslRMIClientSocketFactory());
r.bind("LoginServer", new LoginServer());
System.out.println("--- Login Server ready ---");
}
No I have not started the Registry.
Stop right there. You're not going to get anywhere until you start the Registry. You can't connect to something that isn't there.
It's strange that you're getting a connection timeout rather than 'connection refused', but there is nothing else here that needs explaining.
Setting java.rmi.server.hostname won't fix this problem. None of the questions you have cited here is a similar issue. They are all connection refusals in the client when executing a remote method. This is a connection refusal in the server when binding to the Registry.

why my java mail code doesn't work?

I'm a beginner in socket programming. So I can't solve my project's errors, I can't even understand where the problem is!
here is my code:
public static void main(String[] args) {
String to = "parisa.roodheleh#gmail.com";
String from = "parisainfinity#yahoo.com";
String host = "localhost";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host",host);
Session session = Session.getDefaultInstance(properties);
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("This is the subject Line!");
message.setText("This is autual message!");
Transport.send(message);
System.out.println("sent message successfully...");
}catch(MessagingException mex){
mex.printStackTrace();
}
}
and here is some errors:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2054)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:697)
any idea?
Seems like you are using an invalid smtp host since you are trying to send an email using a yahoo id. Try using smtp.mail.yahoo.com as the host.
properties.setProperty("mail.smtp.host","smtp.mail.yahoo.com");
Although a bit different question, a similar solution is provided in Sending mail from yahoo id to other email ids using Javamail API.
Also please note that once you set this up, if you keep sending emails continuously, eventually (and quite soon), yahoo (or the smtp host) will terminate/hold the sender's account since you are spamming an account imitating the actions of a potential bot.
check if your port 25 is block or not by :
https://mediatemple.net/community/products/dv/204404564/checking-your-outgoing-mail-server-(is-port-25-blocked)

.JSchException: Packet corrupt

I am using Jsch 0.1.51 on RHEl 6 with Jdk 1.7_51. While making session to a remote machine I am getting exception that is :
com.jcraft.jsch.JSchException: Packet corrupt
at com.jcraft.jsch.Session.start_discard(Session.java:1049)
at com.jcraft.jsch.Session.read(Session.java:919)
at com.jcraft.jsch.UserAuthNone.start(UserAuthNone.java:56)
at com.jcraft.jsch.Session.connect(Session.java:389)
at com.jcraft.jsch.Session.connect(Session.java:183)
at TestSFTP.checkException(TestSFTP.java:130)
at TestSFTP.moveFileToDir(TestSFTP.java:78)
at TestSFTP.main(TestSFTP.java:73)
Same code was working fine with RHEL 5. Can any body provide some suggestions .. Thanks
code used is :
Session session = null;
ChannelSftp channelSftp = null;
JSch jsch = new JSch();
session = jsch.getSession(this.sftpUser,this.sftpHost,this.sftpPort);
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.setPassword(this.sftpPassword);
session.connect();
channelSftp = (ChannelSftp)session.openChannel("sftp");
channelSftp.connect();
I am getting error at session.connect();
This exception can occur if you are trying to connect on an already existent session. A work around is closing the session and then starting a new session. This helped me. Found some help from this site:
http://flyingjxswithjava.blogspot.com/2015/03/comjcraftjschjschexception-packet.html
Quoting essential points from the site to understand the problem:
This exception occurs when the Session is reused repeatedly in a loop where the session is disconnected intentionally or due to time out and needs to reconnect again.
The reason that such an exception is thrown is that the first time the Session is connected to the remote site, a random number called Packet is generated for the session.
When the thread is having its 1 hour sleep, the session gets automatically disconnected due to no activity for a certain period of time.
When the Session is disconnected, the Packet is lost.
When the Session is trying to reconnect, it could not find the Packet, thus the exception is thrown.

Categories

Resources