java - Can't access properties of email received by GreenMail -


i'm trying use greenmail mockup mail server unit testing. it's job await imap/pop3 request , deliver dummy mail, can't work.

public class emailmonitoringtest { private static final string user_password = "test"; private static final string user_name = "jdownloadertestuser"; private static final string email_user_address = "jdtest@localhost"; private static final string email_to = "someone@localhost.com"; private static final string email_subject = "test e-mail"; private static final string email_text = "this test e-mail."; private static final string localhost = "127.0.0.1"; private greenmail mailserver;  @before public void setup() {     mailserver = new greenmail(serversetuptest.all);     mailserver.start(); }  @after public void teardown() {     mailserver.stop(); }  @test public void getmails() throws addressexception, messagingexception, folderexception, userexception, ioexception {     // create user on mail server     greenmailuser user = mailserver.setuser(email_user_address, user_name, user_password);      // create e-mail message using javax.mail ..     mimemessage message = new mimemessage((session) null);     message.setfrom(new internetaddress(email_to));     message.addrecipient(message.recipienttype.to, new internetaddress(email_user_address));     message.setsubject(email_subject);     message.settext(email_text);      // use greenmail store message     user.deliver(message);      // check received messages     message[] messages = mailserver.getreceivedmessages();     (message m : messages) {         system.out.println("*** class: " + m.getclass() + " ***");         system.out.println("from: " + m.getfrom()[0]);         system.out.println("to: " + m.getrecipients(message.recipienttype.to)[0]);         system.out.println("subject: " + m.getsubject());         system.out.println("content: " + m.getcontent());     } 

no problem here, output far like:

** class: class javax.mail.internet.mimemessage **

from: someone@localhost.com

to: jdtest@localhost

subject: test e-mail

content: test e-mail.

after that, try mail greenmail imap server:

    // fetch e-mail via imap using javax.mail ..     properties props = new properties();     session session = session.getinstance(props);     urlname urlname = new urlname("imap", localhost,     serversetuptest.imap.getport(), null, user.getlogin(),     user.getpassword());     store store = session.getstore(urlname);     store.connect();      folder folder = store.getfolder("inbox");     folder.open(folder.read_only);     messages = folder.getmessages();     system.out.println("size of messages: " + messages.length);     (message m : messages) {         system.out.println("** class: " + m.getclass() + " **");         system.out.println("from: " + m.getfrom()[0].tostring());         system.out.println("to: " + m.getrecipients(message.recipienttype.to)[0].tostring());         system.out.println("subject: " + m.getsubject().tostring());         system.out.println("content: " + m.getcontent().tostring());     } 

and reward a

size of messages: 1

** class: class com.sun.mail.imap.imapmessage **

and folderclosedexception:

javax.mail.folderclosedexception: * bye javamail exception: java.io.ioexception @ com.sun.mail.imap.imapmessage.loadenvelope(imapmessage.java:1217) @ com.sun.mail.imap.imapmessage.getfrom(imapmessage.java:252) @ jd.controlling.emailmonitoringtest.test(emailmonitoringtest.java:85) @ sun.reflect.nativemethodaccessorimpl.invoke0(native method) @ sun.reflect.nativemethodaccessorimpl.invoke(nativemethodaccessorimpl.java:57) @ sun.reflect.delegatingmethodaccessorimpl.invoke(delegatingmethodaccessorimpl.java:43) @ java.lang.reflect.method.invoke(method.java:601) @ org.junit.runners.model.frameworkmethod$1.runreflectivecall(frameworkmethod.java:44) @ org.junit.internal.runners.model.reflectivecallable.run(reflectivecallable.java:15) @ org.junit.runners.model.frameworkmethod.invokeexplosively(frameworkmethod.java:41) @ org.junit.internal.runners.statements.invokemethod.evaluate(invokemethod.java:20) @ org.junit.internal.runners.statements.runbefores.evaluate(runbefores.java:28) @ org.junit.internal.runners.statements.runafters.evaluate(runafters.java:31) @ org.junit.runners.blockjunit4classrunner.runnotignored(blockjunit4classrunner.java:79) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:71) @ org.junit.runners.blockjunit4classrunner.runchild(blockjunit4classrunner.java:49) @ org.junit.runners.parentrunner$3.run(parentrunner.java:193) @ org.junit.runners.parentrunner$1.schedule(parentrunner.java:52) @ org.junit.runners.parentrunner.runchildren(parentrunner.java:191) @ org.junit.runners.parentrunner.access$000(parentrunner.java:42) @ org.junit.runners.parentrunner$2.evaluate(parentrunner.java:184) @ org.junit.runners.parentrunner.run(parentrunner.java:236) @ org.eclipse.jdt.internal.junit4.runner.junit4testreference.run(junit4testreference.java:50) @ org.eclipse.jdt.internal.junit.runner.testexecution.run(testexecution.java:38) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:467) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.runtests(remotetestrunner.java:683) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.run(remotetestrunner.java:390) @ org.eclipse.jdt.internal.junit.runner.remotetestrunner.main(remotetestrunner.java:197)

trying access folder.getmessage(0) results in indexoutofboundsexception, there 1 message of class imapmessage stored in array. knows this?

ah yes, tried examples on hashcode, fail same reason... folderclosedexception when accessing properties of received imap message...

gratefully accept hints.

when using imap, configure localhost never told greenmail use localhost when startup/create greenmail.

use binding address of greenmail server serversetuptest.imap.getbindaddress() instead of localhost:

// fetch e-mail via imap using javax.mail .. // hint: create session via greenmailutil session session = greenmailutil.getsession(serversetuptest.imap); // use configured host address instead of supplying  urlname urlname = new urlname("imap", serversetuptest.imap.getbindaddress(),    serversetuptest.imap.getport(), null, user.getlogin(),    user.getpassword()); store store = session.getstore(urlname); store.connect();` 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -