title: Mock email using Green mail - Error-Couldn't connect to host, port: localhost, 3025; timeout -1;
body: <p>i'm trying to mock email test using greenmail and testNG. so i got code from greenmail website. i tried to implement it but i got connection refused. 
   this is my GreenEmail.java class</p>
  
  <pre><code>import java.util.Properties;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  
  public class GreenEmail {
  
  private final static Properties mailProperties = new Properties();
  private final String imapHost;
  private final int imapPort;
  
  public GreenEmail(String smtpHost, int smtpPort,
                      String imapHost, int imapPort,
                      String user, String password) {
      this.imapHost = imapHost;
      this.imapPort = imapPort;
      mailProperties.setProperty("mail.smtp.host", smtpHost);
      mailProperties.setProperty("mail.smtp.port", Integer.toString(smtpPort));
      mailProperties.setProperty("mail.user", "bar@example.com");
      mailProperties.setProperty("mail.password", "secret-pwd");
      mailProperties.setProperty("mail.store.protocol", "imap");
  }
  
  /*sendEmail method which takes following parameters
   * to- email to which we send an email
   * from-email from which we send an email
   * subject-subject of the email we send
   * content-message that we send in our email*/
  public void sendMail(String to, String from, String subject,
                       String content) throws MessagingException {
  
      Session session = Session.getDefaultInstance(mailProperties);
  
      MimeMessage message = new MimeMessage(session);
      message.setFrom(new InternetAddress(from));
      message.setRecipients(Message.RecipientType.TO, to);
      message.setSubject(subject);
      message.setText(content);
  
      Transport.send(message);
  }`
  </code></pre>
  
  <p>I have a testNg class to test this sending email test.</p>
  
  <pre><code>import java.io.IOException;
  import javax.mail.Folder;
  import javax.mail.Message;
  import javax.mail.MessagingException;
  import javax.mail.Session;
  import javax.mail.Store;
  import javax.mail.Transport;
  import javax.mail.internet.InternetAddress;
  import javax.mail.internet.MimeMessage;
  import org.testng.Assert;
  import org.testng.annotations.Test;
  import com.icegreen.greenmail.user.GreenMailUser;
  import com.icegreen.greenmail.util.GreenMail;
  import com.icegreen.greenmail.util.ServerSetupTest;
  import com.sun.mail.imap.IMAPStore;
  public class TestMail { 
  @Test
  public void test() throws MessagingException, IOException{
      GreenMail greenMail = new GreenMail(ServerSetupTest.SMTP_IMAP);
      GreenMailUser greenMailUser = greenMail.setUser("bar@example.com", "bar@example.com", "secret-pwd");
       GreenEmail mailingClass = new GreenEmail(
                  "localhost", ServerSetupTest.SMTP.getPort(),
                  "localhost", ServerSetupTest.IMAP.getPort(),
                  greenMailUser.getLogin(), greenMailUser.getPassword());
  String to = "receiver@testmailingclass.net";
      String from = greenMailUser.getEmail();
      String subject = "Sending test";
      String content = "This content should be sent by the user.";
  
      Session smtpSession = greenMail.getSmtp().createSession();
  
      Message msg = new MimeMessage(smtpSession);
      msg.setFrom(new InternetAddress("foo@example.com"));
      msg.addRecipient(Message.RecipientType.TO, new InternetAddress("bar@example.com"));
      msg.setSubject("Email sent to GreenMail via plain JavaMail");
      msg.setText("Fetch me via IMAP");
      mailingClass.sendMail(to, from, subject, content);
  
      // let GreenMail create and configure a store:
      IMAPStore imapStore = greenMail.getImap().createStore();
      imapStore.connect("bar@example.com", "secret-pwd");
      Folder inbox = imapStore.getFolder("INBOX");
      inbox.open(Folder.READ_ONLY);
      Message msgReceived = inbox.getMessage(1);
      Assert.assertEquals(msg.getSubject(), msgReceived.getSubject());
  
      imapStore.close();
  </code></pre>
  
  <p>After running my TestNg class i got the stack trace like this.</p>
  
  <pre><code>[TestNG] Running:
  C:\Users\leela krishna\AppData\Local\Temp\testng-eclipse-1553107636\testng-     customsuite.xml
  
  [Utils] Attempting to create D:\workspace\MockEmail\test-output\Default   suite\Default test.html
  [Utils]   Directory D:\workspace\MockEmail\test-output\Default suite exists: true
  [Utils] Attempting to create D:\workspace\MockEmail\test-output\Default suite\Default test.xml
  [Utils]   Directory D:\workspace\MockEmail\test-output\Default suite exists: true
  FAILED: test
  com.sun.mail.util.MailConnectException: Couldn't connect to host, port:   localhost, 3025; timeout -1;
  nested exception is:
  java.net.ConnectException: Connection refused: connect
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2100)
  at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:699)
  at javax.mail.Service.connect(Service.java:366)
  at javax.mail.Service.connect(Service.java:246)
  at javax.mail.Service.connect(Service.java:195)
  at javax.mail.Transport.send0(Transport.java:254)
  at javax.mail.Transport.send(Transport.java:124)
  at com.qpair.qp.GreenEmail.sendMail(GreenEmail.java:46)
  at com.qpair.qp.TestMail.test(TestMail.java:88)
  at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  at java.lang.reflect.Method.invoke(Method.java:497)
  at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:86)
  at org.testng.internal.Invoker.invokeMethod(Invoker.java:646)
  at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:823)
  at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1131)
  at   org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
  at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
  at org.testng.TestRunner.privateRun(TestRunner.java:778)
  at org.testng.TestRunner.run(TestRunner.java:632)
  at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)
  at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)
  at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)
  at org.testng.SuiteRunner.run(SuiteRunner.java:268)
  at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
  at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
  at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)
  at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)
  at org.testng.TestNG.runSuites(TestNG.java:1075)
  at org.testng.TestNG.run(TestNG.java:1047)
  at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126)
  at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137)
  at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
  Caused by: java.net.ConnectException: Connection refused: connect
  at java.net.DualStackPlainSocketImpl.connect0(Native Method)
  at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79)
  at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
  at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
  at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
  at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
  at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
  at java.net.Socket.connect(Socket.java:589)
  at java.net.Socket.connect(Socket.java:538)
  at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:331)
  at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:238)
  at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2066)
  ... 33 more
  </code></pre>
  
  <p>So i didn't understand the problem. if anybody can clear this that would be a great help for me. Thanks in advance.</p>