技术开发 频道

JSF中JavaMail的应用

  【IT168 技术文档】JSF中JavaMail的应用

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
相关文章