วันจันทร์ที่ 8 กุมภาพันธ์ พ.ศ. 2559

[ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)

When we try to connect gmail imap server and it throws error as 


[ALERT] Please log in via your web browser: https://support.google.com/mail/accounts/answer/78754 (Failure)

Solution: Turn on 2-step verification then use App passwords instead the usual password

How to connect IMAPS gmail server

import java.util.Properties;


import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Store;
import com.sun.mail.imap.IMAPFolder;


// Define the used properties.
Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps");


Session session = Session.getDefaultInstance(props, null);


store = session.getStore("imaps"); store.connect("imap.googlemail.com", "username@gmail.com", "password");
folder = (IMAPFolder) store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
Message[] messages = folder.getMessages();
System.out.println("No of Messages : " + folder.getMessageCount());
System.out.println("No of Unread Messages : " + folder.getUnreadMessageCount());
System.out.println(messages.length);
for (int i = 0; i < messages.length; i++) {


System.out.println("*****************************************************************************");
System.out.println("MESSAGE " + (i + 1) + ":");
Message msg = messages[i];
// System.out.println(msg.getMessageNumber());
// Object String;
// System.out.println(folder.getUID(msg)


subject = msg.getSubject();


System.out.println("Subject: " + subject);


}
} catch (IndexOutOfBoundsException e) {
e.printStackTrace();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
try {
folder.close(true);
} catch (Exception e) {
// do nothing
}
try {
store.close();
} catch (Exception e) {
// do nothing
}
}

วันอาทิตย์ที่ 7 กุมภาพันธ์ พ.ศ. 2559

How to open OAF page from a link

http://thaioafdev.blogspot.com/2016/02/how-to-open-oaf-page-from-link.html

How to fix PKIX path building failed unable to find valid certification path to requested target

1.  get the self-signed certificate from the mail server via openssl:


echo | openssl s_client -connect your_imaps_server:port 2>&1 | sed -ne '/-BEGIN CERTIFICATE-/,/-END CERTIFICATE-/p' > exported_cert.pem


2. Check your current used JRE path, for example in Eclipse
Windows > Preferences > Java > Installed JREs


3. Open cmd as Administrator (for Windows users)


4. Copy file from step 1 into path_from_step2\lib\security


5. Open CMD as Admin (for Windows >> All Programs > Accessories > CMD > Run as admin )




6. Check certs for JRE, CD to path_from_step2\lib\security , run command


keytool -list -keystore cacerts


Password is changeit. The command will list all certs




7. Run command


For Windows>>  keytool -keystore cacerts -importcert -alias mycert -file exported_cert.pem
For Unix>> keytool -keystore cacerts -import -alias mycert -file exported_cert.pem
Password is changeit
If there is error please make sure that you log in as Administrator