订阅所有JSP/Servlet的日志 订阅 | 这是最新一篇日志 上一篇 | 下一篇日志 下一篇 ]
Java

JSF中JavaMail的应用

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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package com.talgroup.web;
 
import java.io.Serializable;
import java.util.Properties;
 
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
 
import org.apache.commons.logging.Log;
 
/**
 * JavaServer Faces managed bean that is used to send mail to mail server.
 * 
 * @author suibianguangguang
 * @version $Id: SupportMailBean.java,v 1.1 2006/09/04 08:47:52 suibianguangguang Exp $
 */
public class SupportMailBean implements Serializable {
 
	/**
	 * Serial Version UID
	 */
	private static final long serialVersionUID = -6113167257687160916L;
 
	/**
	 * Logger
	 */
	private Log logger = LogEnabled.getLogger();
	
	/**
	 * Mail server authenticator for sending mail
	 */
	class MailAuthenticator extends Authenticator {
 
	    /**
		 * @return The user name at mail server
		 */
	    private String userName;
	    
	    /**
		 * @return The password for the user name
		 */
	    private String password;
 
		/**
		 * @return The password for the user name
		 */
		public String getPassword() {
			return password;
		}
 
		/**
		 * @param password The password for the user name
		 */
		public void setPassword(String password) {
			this.password = password;
		}
 
		/**
		 * @return The user name at mail server
		 */
		public String getUserName() {
			return userName;
		}
 
		/**
		 * @param userName The user name at mail server
		 */
		public void setUserName(String userName) {
			this.userName = userName;
		}
	}
 
	/**
	 * The mail server host name or ip
	 */
	private String serverName;
	
	/**
	 * The mail server port
	 */
	private Integer serverPort = new Integer(25);
 
	/**
	 * The mail server protocol: SMTP, IMAP ...
	 */
	private String protocol = "smtp";
 
	/**
	 * The String userName
	 */
	private String authUserName;
	
	/**
	 * The String password
	 */
	private String authPassword;
 
	/**
	 * String from
	 */
	private String fromMailAddress;
	
	/**
	 * The String to
	 */
	private String mailToAddresses;
	
	/**
	 * Mail cc
	 */
	private String mailCcAddresses;
	
	/**
	 * The String subject
	 */
	private String mailSubject;
	
	/**
	 * The String content
	 */
	private String mailContent;
	
	/**
	 * The extra messages that will follow after mail content
	 */
	private String extraMailContent;
	
	/**
	 * @return The mail server host name or IP address
	 */
	public String getServerName() {
		return serverName;
	}
	/**
	 * @param serverName The mail server host name or IP address
	 */
	public void setServerName(String serverName) {
		this.serverName = serverName;
	}
	
	/**
	 * @return The mail server protocol: SMTP, IMAP ...
	 */
	public String getProtocol() {
		return protocol;
	}
	/**
	 * @param protocol The mail server protocol: SMTP, IMAP ...
	 */
	public void setProtocol(String protocol) {
		this.protocol = protocol;
	}
 
	/**
	 * @return The mail server service port, default is 25
	 */
	public Integer getServerPort() {
		return serverPort;
	}
	/**
	 * @param serverPort The mail server service port, default is 25
	 */
	public void setServerPort(Integer serverPort) {
		this.serverPort = serverPort;
	}
 
	/**
	 * @return The user name in order to log in mail server. It is required if server need
	 */
	public String getAuthUserName() {
		return authUserName;
	}
	/**
	 * @param authUserName The user name in order to log in mail server. It is required if server need
	 */
	public void setAuthUserName(String authUserName) {
		this.authUserName = authUserName;
	}
 
	/**
	 * @return The password to log in mail server if mail account required
	 */
	public String getAuthPassword() {
		return authPassword;
	}
	/**
	 * @param authPassword The password to log in mail server if mail account required
	 */
	public void setAuthPassword(String authPassword) {
		this.authPassword = authPassword;
	}
	
	/**
	 * @return The mail address that send the mail. It is optional. Needs provide if mail server required  
	 */
	public String getFromMailAddress() {
		return fromMailAddress;
	}
	/**
	 * @param fromMailAddress The mail address that send the mail. It is optional.
	 */
	public void setFromMailAddress(String fromMailAddress) {
		this.fromMailAddress = fromMailAddress;
	}
	
	/**
	 * @return The mail will send to these addresses
	 */
	public String getMailToAddresses() {
		return mailToAddresses;
	}
	/**
	 * @param mailToAddresses The mail will send to these addresses
	 */
	public void setMailToAddresses(String mailToAddresses) {
		this.mailToAddresses = mailToAddresses;
	}
	
	/**
	 * @return the mailCcAddresses
	 */
	public String getMailCcAddresses() {
		return mailCcAddresses;
	}
	/**
	 * @param mailCcAddresses the mailCcAddresses to set
	 */
	public void setMailCcAddresses(String mailCcAddresses) {
		this.mailCcAddresses = mailCcAddresses;
	}
	
	/**
	 * @return The mail subject
	 */
	public String getMailSubject() {
		return mailSubject;
	}
	/**
	 * @param mailSubject The mail subject
	 */
	public void setMailSubject(String mailSubject) {
		this.mailSubject = mailSubject;
	}
	
	/**
	 * @return The mail content will be sent
	 */
	public String getMailContent() {
		return mailContent;
	}
	/**
	 * @param mailContent The mail content will be sent
	 */
	public void setMailContent(String mailContent) {
		this.mailContent = mailContent;
	}
	
	/**
	 * @return The extra mail content. Usually, it is created by program
	 */
	public String getExtraMailContent() {
		return extraMailContent;
	}
	/**
	 * @param extraMailContent The extra mail content. Usually, it is created by program
	 */
	public void setExtraMailContent(String extraMailContent) {
		this.extraMailContent = extraMailContent;
	}
	
	/**
	 * @return The full mail content that is used to send mail
	 */
	public String getFullContent() {
		StringBuffer buffer = new StringBuffer();
		
		buffer.append(java.util.Calendar.getInstance().getTime());
		buffer.append("\r\r");
		
		buffer.append(mailContent);
		buffer.append("\r\r");
		
		if ((extraMailContent != null) && (extraMailContent.length() > 0)) {
			buffer.append("\r\r");
			buffer.append(extraMailContent);
		}
		
		return buffer.toString();
	}
	
	/**
	 * @return The full mail content that is used to show at web page
	 */
	public String getHtmlFullContent() {
		return getFullContent().replaceAll("\r", "<br/>");
	}
	
	/**
	 * Send mail to user
	 * @return Whether send mail success
	 */
	public String onClickSendMail() {
		
        try {
            Properties props = new Properties();
            
            props.put("mail.transport.protocol", protocol);
            props.put("mail.smtp.host", serverName);
            props.put("mail.smtp.port", serverPort);
 
            Session session = null;
            
            if ((authUserName != null) && (authUserName.length() > 0)) {
            	MailAuthenticator authenticator = new MailAuthenticator();
            	
            	authenticator.setUserName(authUserName);
            	authenticator.setPassword(authPassword);
            	
            	session = Session.getInstance(props, authenticator);
            } else {
            	session = Session.getInstance(props);
            }
            	
 
            MimeMessage message = new MimeMessage(session);
            
            if ((fromMailAddress != null) && (fromMailAddress.length() > 0)) {
            	message.addFrom(InternetAddress.parse(fromMailAddress.trim()));
            }
            
            message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(mailToAddresses));
            
            if ((mailCcAddresses != null) && (mailCcAddresses.length() > 0)) {
            	message.addRecipients(Message.RecipientType.CC, InternetAddress.parse(mailToAddresses));
            }
            
            message.setSubject(mailSubject);
            
            Multipart multipart = new MimeMultipart();
            BodyPart bodyPartInfo = new MimeBodyPart();
            
            bodyPartInfo.setText(getFullContent());
            
            multipart.addBodyPart(bodyPartInfo);
 
            message.setContent(multipart);
            message.saveChanges();
 
            Transport.send(message);
            
            return "support.mail.success";
 
        } catch (Exception e) {
        	if (logger.isErrorEnabled()) {
        		logger.error(SupportMailMessages.getString(SupportMailMessages.FAIL_TO_SEND_MAIL, 
        				e.getMessage()), e);
        	}
        	SupportMailMessages.addFacesMessage(SupportMailMessages.FAIL_TO_SEND_MAIL, 
        			new String[] {e.getMessage()});
        	
            return null;
        }
    }
}


平均得分
(0 次评分)





文章来自: 本站原创
标签:
评论: 75 | 查看次数: 5720
  • 共有 75 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
游客 [2008-09-03 10:34:36]
游客 [2008-09-02 11:08:11]
游客 [2008-08-28 17:55:33]
游客 [2008-08-27 13:18:39]
游客 [2008-08-25 23:07:32]
游客 [2008-08-22 21:41:21]
游客 [2008-08-19 15:57:48]
游客 [2008-08-18 15:11:02]
游客 [2008-08-14 16:45:46]
游客 [2008-08-14 14:22:16]
游客 [2008-08-08 12:02:14]
游客 [2008-08-06 14:22:18]
游客 [2008-08-06 10:46:11]
精油
论文发表
上海翻译公司
上海翻译
英语培训
英语口语
神经性皮炎
皮炎
湿疹
荨麻疹
慢性荨麻疹
藏獒
液压缸
油缸
破碎机
北京旅游
北京旅行社
条码机
条码打印机
条形码打印机
阴茎增大
伟哥
发酵罐
肠炎
结肠炎
直肠炎
慢性肠炎
慢性结肠炎
结肠炎的治疗
溃疡性结肠炎
慢性结肠炎的治疗
大豆床上用品
保健内衣
羊绒内衣
大豆纤维面料
团购礼品
移民
热电偶插头
测温线
热电阻
煤气发生炉
两段式煤气发生炉
环保节能型煤气发生炉
硅碳棒
吸塑机
纸管机
无缝管
合金管
无缝管
无缝钢管
高血压
产品设计
无纸记录仪
红外测温仪
无纸记录仪
韩国服装
韩版服装
韩国服饰
丝锥
挤压丝锥
非标丝锥
梯形丝锥
螺纹环规
节能胶带机
胶带机价格
太阳能
太阳能热水器
分体式太阳能
二手车
北京二手车
北京二手车交易
北京二手汽车
二手车汽车
山特ups电源
山特ups
山特
山特ups报价
ups电源
ups
全自动表面张力仪/界面张力仪
inflatable
bouncer
men spa beijing
men massage beijing
pearl jewelry
Beijing Tour
china Tour
beijing Tour
china Tour
beijing Tour
China Necklace Wholesale
China Bracelet Wholesale
China Ring wholesale
China gemstone beads wholesale
China Jewelry Accessories wholesale
China Semiprecious beads

wholesale

replica handbag
replica tiffany
replica watches
louis vuitton replica
chanel replica
gucci replica
Chinese language
Chinese learn
learning Chinese
learn mandarin
ecosway
gasifier
coal gas
coal gasification
pro dj cases
beijing tour
beijing tours
beijing travel
beijing tours
china tour
beijing
china tours
china travel
beijing china
china beijing
beijing hotel
beijing hotels
China Flights
carved fireplace
stone bathtub
marble fountain
marble bench
marble fireplace
marble sculpture
marble columns
marble lions
marble doorway
marble gazebo
marble pillar
marble fireplace surround
marble statue
marble bathtub
韩版服装
时尚衣橱
韩派服装
韩国流行服装
韩国流行服饰
流行服饰
流行服装
时尚起义
游客 [2008-08-04 10:02:45]
游客 [2008-08-01 16:55:44]
  • 共有 75 条评论
  • 1
  • 2
  • 3
  • 4
  • 5
  • |
  • >>
发表评论
昵 称:  登录
内 容:
选 项:
字数限制 1000 字 | UBB代码 开启 | [img]标签 开启