您的位置:首页 > Web前端 > HTML

一个用于发送HTML格式邮件的类

2013-09-15 00:32 441 查看
以下类是在网上孙钰佳的版本上改写而来,主要变化了三点:1.去掉了附件部分;2.形式从纯Java类改成可注入方式;3.to,cc和bcc都变成了一堆人,以前是一个人。

以下是Java类的代码:

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.MessageFormat;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;

/**
* 报告邮件发送类
* 2013年9月13日22:28:54
*/
public class GerneralReportMailService{
private String smtpServer;
private String smtpUsername;
private String smtpPassword;
private String fromMailAddress;
private String toMailAddress;
private String ccMailAddress;
private String bccMailAddress;

/**
* 无参构造函数
*/
public GerneralReportMailService(){

}

/**
* 示例
*/
public boolean sendMail() throws Exception{
String title="Sample";// 标题
String content="<ul><li>1</li><li>A</li><li>+</li></ul>";// 这里组合HTML文本
return sendMail(title,sb.toString());
}

/**
* 发送邮件的关键函数
*
* @param title
* @param content
* @return
* @throws Exception
*/
private boolean sendMail(String title,String content) throws Exception{
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", smtpServer);
// 获得邮件会话对象
Session session = Session.getDefaultInstance(props,new SmtpAuthenticator(smtpUsername, smtpPassword));
/** *************************************************** */
// 创建MIME邮件对象
MimeMessage mimeMessage = new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress(fromMailAddress));// 发件人

mimeMessage.setRecipients(Message.RecipientType.TO, getInternetAddressArr(toMailAddress));// To收件人
mimeMessage.setRecipients(Message.RecipientType.CC, getInternetAddressArr(ccMailAddress));// Cc收件人
mimeMessage.setRecipients(Message.RecipientType.BCC, getInternetAddressArr(bccMailAddress));// Bcc收件人

mimeMessage.setSubject(title);
mimeMessage.setSentDate(new Date());// 发送日期
Multipart mp = new MimeMultipart("related");// related意味着可以发送html格式的邮件
/** *************************************************** */
BodyPart bodyPart = new MimeBodyPart();// 正文
bodyPart.setDataHandler(new DataHandler(content,"text/html;charset=utf8"));// 网页格式
mp.addBodyPart(bodyPart);
mimeMessage.setContent(mp);// 设置邮件内容对象
Transport.send(mimeMessage);// 发送邮件

return true;
}

private InternetAddress[] getInternetAddressArr(String mialAddr) throws Exception{
String[] arr=mialAddr.split(",");

InternetAddress[] retval=new InternetAddress[arr.length];

for(int i=0;i<arr.length;i++){
retval[i]=new InternetAddress(arr[i]);
}

return retval;
}

public String getSmtpServer() {
return smtpServer;
}
public void setSmtpServer(String smtpServer) {
this.smtpServer = smtpServer;
}
public String getSmtpUsername() {
return smtpUsername;
}
public void setSmtpUsername(String smtpUsername) {
this.smtpUsername = smtpUsername;
}
public String getSmtpPassword() {
return smtpPassword;
}
public void setSmtpPassword(String smtpPassword) {
this.smtpPassword = smtpPassword;
}
public String getFromMailAddress() {
return fromMailAddress;
}
public void setFromMailAddress(String fromMailAddress) {
this.fromMailAddress = fromMailAddress;
}
public String getToMailAddress() {
return toMailAddress;
}
public void setToMailAddress(String toMailAddress) {
this.toMailAddress = toMailAddress;
}
public String getCcMailAddress() {
return ccMailAddress;
}
public void setCcMailAddress(String ccMailAddress) {
this.ccMailAddress = ccMailAddress;
}
public String getBccMailAddress() {
return bccMailAddress;
}
public void setBccMailAddress(String bccMailAddress) {
this.bccMailAddress = bccMailAddress;
}
}

/**
* Smtp认证
*/
class SmtpAuthenticator extends Authenticator {
String username = null;
String password = null;

// SMTP身份验证
public SmtpAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.username, this.password);
}

}

class ByteArrayDataSource implements DataSource {

private final String contentType;
private final byte[] buf;
private final int len;

public ByteArrayDataSource(byte[] buf, String contentType) {
this(buf, buf.length, contentType);
}

public ByteArrayDataSource(byte[] buf, int length, String contentType) {
this.buf = buf;
this.len = length;
this.contentType = contentType;
}

public String getContentType() {
if (contentType == null)
return "application/octet-stream";
return contentType;
}

public InputStream getInputStream() {
return new ByteArrayInputStream(buf, 0, len);
}

public String getName() {
return null;
}

public OutputStream getOutputStream() {
throw new UnsupportedOperationException();
}
}

以下是配置此bean的XML:

<bean id="gerneralReportMailService" class="com.ibm.XXX.service.GerneralReportMailService">
<property name="smtpServer"><value>smtp.163.com</value></property>
<property name="smtpUsername"><value>unknownuser@163.com</value></property>
<property name="smtpPassword"><value>unknownuserPswd</value></property>
<property name="fromMailAddress"><value>unknownuser@163.com</value></property>
<property name="toMailAddress"><value>a@b.c,1@2.3</value></property>
<property name="ccMailAddress"><value>d@j.k</value></property>
<property name="bccMailAddress"><value>w@v.x,s@ufo.com</value></property>
</bean>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: