View Javadoc
1   /*
2    * Copyright (c) MuleSoft, Inc.  All rights reserved.  http://www.mulesoft.com
3    * The software in this package is published under the terms of the CPAL v1.0
4    * license, a copy of which has been included with this distribution in the
5    * LICENSE.txt file.
6    */
7   package org.mule.transport.email;
8   
9   import com.icegreen.greenmail.user.GreenMailUser;
10  import com.icegreen.greenmail.user.UserManager;
11  import com.icegreen.greenmail.util.GreenMail;
12  
13  import java.net.Socket;
14  import java.util.Properties;
15  
16  import javax.mail.Message;
17  import javax.mail.MessagingException;
18  import javax.mail.Session;
19  import javax.mail.internet.InternetAddress;
20  import javax.mail.internet.MimeMessage;
21  
22  import org.apache.commons.logging.Log;
23  import org.apache.commons.logging.LogFactory;
24  
25  public class GreenMailUtilities
26  {
27      protected static Log logger = LogFactory.getLog(GreenMailUtilities.class);
28  
29      public static void storeEmail(UserManager userManager, String email, String user, String password,
30                                    MimeMessage message)
31              throws Exception
32      {
33          // note that with greenmail 1.1 the Servers object is unreliable
34          // and the approach taken in their examples will not work.
35          // the following does work, but may break in a later version
36          // (there is some confusion in the greenmail code about
37          // whether users are identified by email or name alone)
38          // in which case try retrieving by EMAIL rather than USER
39          userManager.createUser(email, user, password);
40          GreenMailUser gmUser = userManager.getUser(user);
41          assert null != gmUser;
42          gmUser.deliver(message);
43      }
44  
45      public static MimeMessage toMessage(String text, String email, String charset) throws MessagingException
46      {
47          MimeMessage message = new MimeMessage(Session.getDefaultInstance(new Properties()));
48          if (charset != null) 
49          {
50              message.setContent(text, "text/plain; charset=" + charset);
51          } 
52          else 
53          {
54              message.setContent(text, "text/plain");
55          }
56          message.setRecipient(Message.RecipientType.TO, new InternetAddress(email));
57          return message;
58      }
59  
60      public static void waitForStartup(String host, int port, int count, long wait) throws InterruptedException
61      {
62          for (int i = 0; i < count; ++i)
63          {
64              Thread.sleep(wait);
65              try {
66                  Socket socket = new Socket(host, port);
67                  socket.close();
68                  logger.info("Successful connection made to port " + port);
69                  return;
70              }
71              catch (Exception e)
72              {
73                  logger.warn("Could not connect to server on " + host + ":" + port + " - " + e.getMessage());
74              }
75          }
76          throw new RuntimeException("Server failed to start within " + (count * wait) + "ms");
77      }
78  
79      public static void robustStartup(GreenMail servers, String host, int port, int startMax, int testMax, long wait)
80              throws InterruptedException
81      {
82          for (int start = 0; start < startMax; ++start)
83          {
84              try
85              {
86                  servers.start();
87                  waitForStartup(host, port, testMax, wait);
88                  return;
89              }
90              catch (Exception e)
91              {
92                  try
93                  {
94                      servers.stop();
95                  }
96                  catch (Throwable t)
97                  {
98                      // ignore
99                  }
100             }
101             Thread.sleep(wait);
102         }
103     }
104 }