summaryrefslogtreecommitdiffstats
path: root/tdejava/koala/examples/simplemail/MailHelper.java
blob: 1d33a21cd3f32ba19bd7eea489a73927abe5434b (plain)
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
//package com.werpu.simplemail;


import java.util.Iterator;
import java.util.LinkedList;
import java.util.Properties;

import javax.mail.Address;
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;

/**
 *
 * @author  Werner Punz [email protected]
 * @version 1.0
 * This is a basic Mailer class built upon the JavaMail API
 * it only does SMTP.
 */
public class MailHelper
{

	private String message = "";

	private String recipient = "";

	private String server = "";

	private String password = "";

	private String sender = "";

	private String subject = "";

	private String username = "";

	private LinkedList ccAddresses = new LinkedList();

	/** Creates a new instance of LC_Mailer */
	public MailHelper()
	{
	}

	/** Getter for property message.
	 * @return Value of property message.
	 */
	public String getMessage()
	{
		return message;
	}

	/** Setter for property message.
	 * @param message New value of property message.
	 */
	public void setMessage(String message)
	{
		this.message = message;
	}

	/** Getter for property password.
	 * @return Value of property password.
	 */
	public String getPassword()
	{
		return password;
	}

	/** Setter for property password.
	 * @param password New value of property password.
	 */
	public void setPassword(String password)
	{
		this.password = password;
	}

	/** Getter for property recipient.
	 * @return Value of property recipient.
	 */
	public String getRecipient()
	{
		return recipient;
	}

	/** Setter for property recipient.
	 * @param recipient New value of property recipient.
	 */
	public void setRecipient(String recipient)
	{
		this.recipient = recipient;
	}

	/** Getter for property sender.
	 * @return Value of property sender.
	 */
	public String getSender()
	{
		return sender;
	}

	/** Setter for property sender.
	 * @param sender New value of property sender.
	 */
	public void setSender(String sender)
	{
		this.sender = sender;
	}

	/** Getter for property server.
	 * @return Value of property server.
	 */
	public String getServer()
	{
		return server;
	}

	/** Setter for property server.
	 * @param server New value of property server.
	 */
	public void setServer(String server)
	{
		this.server = server;
	}

	/** Getter for property subject.
	 * @return Value of property subject.
	 */
	public String getSubject()
	{
		return subject;
	}

	/** Setter for property subject.
	 * @param subject New value of property subject.
	 */
	public void setSubject(String subject)
	{
		this.subject = subject;
	}

	/**
	 * Method setUsername.
	 * @param username
	 */
	public void setUsername(String username)
	{
		this.username = username;
	}

	/**
	 * Method addCCAddress.
	 * @param ccAddresses
	 * Adds a single CC Adress to the current CC Addresses
	 */
	public void addCCAddress(String ccAddresses)
	{
		if (!ccAddresses.equalsIgnoreCase("null"))
			this.ccAddresses.add(ccAddresses);
	}

	/**
	 * Method addCCAddress.
	 * @param ccAddresses
	 * adds the ccAddresses to the current messaging parameters
	 */
	public void addCCAddress(Iterator ccAddresses)
	{

		while (ccAddresses.hasNext())
			addCCAddress((String) ccAddresses.next());
	}

	/**
	 * Method send.
	 * @throws MessagingException
	 * sends out the mail with the set messaging parameters
	 */
	public void send() throws MessagingException
	{
		Properties props = new Properties();
                Session session = Session.getDefaultInstance(props, null);


                Address to = new InternetAddress(getRecipient());
		Address from = new InternetAddress(getSender());

                //only one from address
                Address[] froms = new Address[1];
		froms[0] = from;

            	MimeMessage message = new MimeMessage(session);
		message.setText(getMessage());
		message.setSubject(getSubject());
		message.addRecipient(Message.RecipientType.TO, to);

		//Add CCs to the recipient list
		Iterator i = ccAddresses.iterator();
		while (i.hasNext())
		{
			to = new InternetAddress((String) i.next());
			message.addRecipient(Message.RecipientType.BCC, to);
		}

		message.addFrom(froms);
		message.saveChanges();


                //set smtp
		Transport myTransport = session.getTransport("smtp");

                //send mail
		myTransport.connect(getServer(), username.trim(), password.trim());

		myTransport.sendMessage(message,message.getAllRecipients());
		myTransport.close();
	}

}