forked from linagora/Greenmail
-
Notifications
You must be signed in to change notification settings - Fork 0
/
examples.html
175 lines (148 loc) · 6.26 KB
/
examples.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>GreenMail - Email Test Servers</title>
<link rel="stylesheet" href="stylesheet.css" type="text/css" />
</head>
<body>
<div id="header">
<img src="pics/greenmail.gif" title="GreenMail - Testing SMTP, POP3, IMAP">
Latest Release GreenMail v1.3.1b - May 27th, 2009
</div>
<div class="pages">
<span class="page"><a href="readme.html">About</a></span>
| <span class="page"><a href="examples.html">Examples</a></span>
| <span class="page"><a href="jboss-service.html">JBoss</a></span>
| <span class="page"><a href="faq.html">FAQ</a></span>
| <span class="page"><a href="javadocs/index.html">Javadocs</a></span>
| <span class="page"><a href="http://www.icegreen.com/articles">Blog</a></span>
| <span class="page"><a href="http://sourceforge.net/project/showfiles.php?group_id=159695">Download</a></span>
| <span class="page"><a href="feedback.html">Feedback</a></span>
</div>
<br>
<table width="100%">
<tr><td>
<h2>Testing Your Sending Code - Example 1</h2>
<div class="codeSample"><pre>
public void testYourSendingCode() throws Exception {
GreenMail greenMail = new GreenMail(); //uses test ports by default
greenMail.start();
GreenMailUtil.sendTextEmailTest("to@localhost.com", "from@localhost.com", "subject", "body");
assertEquals("subject", greenMail.getReceivedMessages()[0].getSubject());
greenMail.stop();
//<b>That's it!</b>
}
</pre>
</div>
<h2>Testing Your Sending Code - Example 2</h2>
This example does basically the same as the one above but is more complex as it's purpose is to demonstrate additional features and utility methods.
<div class="codeSample"><pre>
GreenMail greenMail;
protected void tearDown() throws Exception {
if (null!=greenMail) {
greenMail.stop();
}
super.tearDown();
}
public void testYourSendingCode() throws Exception {
//start all email servers using non-default ports.
//See <a href="javadocs/com/icegreen/greenmail/util/ServerSetupTest.html">Javadoc</a> for details
greenMail = new GreenMail(ServerSetupTest.ALL);
greenMail.start();
//use random content to avoid potential residual lingering problems
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
<b><PLACE YOUR SEND CODE HERE></b>
//wait for max 5s for 1 email to arrive
//waitForIncomingEmail() is useful if you're sending stuff asynchronously in a separate thread
assertTrue(greenMail.waitForIncomingEmail(5000, 1));
//Retrieve using GreenMail API
Message[] messages = greenMail.getReceivedMessages();
assertEquals(1, messages.length);
assertEquals(subject, messages[0].getSubject());
assertEquals(body, GreenMailUtil.getBody(messages[0]).trim());
//if your send content as a 2 part multipart...
assertTrue(messages[0].getContent() instanceof MimeMultipart);
MimeMultipart mp = (MimeMultipart) messages[0].getContent();
assertEquals(2, mp.getCount());
BodyPart bp;
assertEquals(multipart_body1, GreenMailUtil.getBody(mp.getBodyPart(0)).trim());
assertEquals(multipart_body2, GreenMailUtil.getBody(mp.getBodyPart(1)).trim());
}
</pre>
</div>
<h2>Test Your Retrieving Code</h2>
<div class="codeSample"><pre>
protected void tearDown() throws Exception {
if (null!=greenMail) {
greenMail.stop();
}
super.tearDown();
}
/**
* Places an email in a users inbox directly for testing your retrieval code
*/
public void testYourRetrievingCode_by_insertion() throws Exception {
//start all email servers using non-default ports.
//See <a href="javadocs/com/icegreen/greenmail/util/ServerSetupTest.html">Javadoc</a> for details
greenMail = new GreenMail(ServerSetupTest.ALL);
greenMail.start();
//use random content to avoid potential residual lingering problems
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
MimeMessage message = <b><CONSTRUCT MESSAGE ></b>
User user = greenMail.setUser("wael@localhost.com", "waelc", "soooosecret");
user.deliver(message);
assertEquals(1, greenMail.getReceivedMessages().length);
<b><PLACE YOUR RETRIVE AND VERIFICATION CODE HERE></b>
}
/**
* Sends an email using SMTP for testing your retrieval code
*/
public void testYourRetrievingCodeBySending() throws Exception {
//start all email servers using non-default ports.
//See <a href="javadocs/com/icegreen/greenmail/util/ServerSetupTest.html">Javadoc</a> for details
greenMail = new GreenMail(ServerSetupTest.ALL);
greenMail.start();
//use random content to avoid potential residual lingering problems
final String subject = GreenMailUtil.random();
final String body = GreenMailUtil.random();
GreenMailUtil.sendTextEmailTest("test@localhost.com", "from@localhost.com", subject, body);
//wait for max 5s for 1 email to arrive
assertTrue(greenMail.waitForIncomingEmail(5000, 1));
<b><PLACE YOUR RETRIVE AND VERIFICATION CODE HERE></b>
}
</pre>
</div>
</td>
<td class="gads">
<script type="text/javascript"><!--
google_ad_client = "pub-2104788530662674";
google_ad_width = 160;
google_ad_height = 600;
google_ad_format = "160x600_as";
google_ad_type = "text";
google_ad_channel ="2021857531";
google_color_border = "FDEFD2";
google_color_bg = "FDEFD2";
google_color_link = "0000CC";
google_color_url = "008000";
google_color_text = "000000";
//--></script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</td>
</tr>
</table>
<br>
<br>
<br>
<div class="footer">
Copyright © 2009 <a href="http://www.icegreen.com">Icegreen Technologies</a>
<br><br>
<a href="http://sourceforge.net/donate/index.php?group_id=159695"><img src="http://images.sourceforge.net/images/project-support.jpg" width="88" height="32" border="0" alt="Support This Project" /> </a>
<a href="http://sourceforge.net"><img src="http://sflogo.sourceforge.net/sflogo.php?group_id=159695&type=1" width="88" height="31" border="0" alt="SourceForge.net Logo" /></a>
</div>
</body>
</html>