Javamail邮件发送程序(smtp)


import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.util.Date;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
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 javax.mail.internet.MimeUtility;

import org.apache.commons.lang.StringUtils;

/**
* 邮件发送器,方法调用顺序:<code>connect->send->close</code>。
*/
public class MailSender {
public static final String DEFAULT_MIME_TYPE = "text/html;charset=gb2312";

private String smtp;
private String serverName;
private String serverPasswd;
private String from;

private Session session;
private Transport transport;

/**
* 构造器
*
* @param smtp
* SMTP服务地址
* @param serverName
* 服务用户名
* @param serverPasswd
* 服务密码
* @param from
* 发送邮件的Email
*/
public MailSender(String smtp, String serverName, String serverPasswd,
String from) {
super();
this.smtp = smtp;
this.serverName = serverName;
this.serverPasswd = serverPasswd;
this.from = from;
}

/**
* 连接邮件服务器
*/
public void connect() throws NoSuchProviderException {
// ResourceBundle mailProps = ResourceBundle.getBundle("mail");
// 可以从配置文件读取相应的参数
Properties props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", smtp); // 设置SMTP主机
props.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证
// 到服务器验证发送的用户名和密码是否正确
MailAutherticator emailAuther = new MailAutherticator(this.serverName,
this.serverPasswd);
// 设置邮件会话
this.session = Session.getInstance(props, emailAuther);
// 设置传输协议
this.transport = session.getTransport("smtp");
}

/**
* 发送邮件
*
* @param toEmails
* 邮件接收地址列表
* @param subject
* 主题
* @param content
* 正文
* @param fileNames
* 附件列表(文件名,显示名称). <code>null</code>表示无附件.
* @param mimeType
* MIME 类型
* @throws MessagingException
* @throws UnsupportedEncodingException
* 如果读取附件或发送失败
*/
public void send(String[] toEmails, String subject, String content,
String[] fileNames, String mimeType) throws MessagingException,
UnsupportedEncodingException {
// 设置from、to等信息
MimeMessage mimeMsg = new MimeMessage(this.session);
if (!StringUtils.isEmpty(this.from)) {
InternetAddress sentFrom = new InternetAddress(this.from);
mimeMsg.setFrom(sentFrom); // 设置发送人地址
}

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

mimeMsg.setRecipients(MimeMessage.RecipientType.TO, sendTo);
mimeMsg.setSubject(subject, Charset.defaultCharset().name());

MimeBodyPart messageBodyPart1 = new MimeBodyPart();
messageBodyPart1.setContent(content, mimeType);

Multipart multipart = new MimeMultipart();// 附件传输格式
multipart.addBodyPart(messageBodyPart1);
if (fileNames != null) {
for (int i = 0; i < fileNames.length; i++) {
MimeBodyPart messageBodyPart2 = new MimeBodyPart();
String[] strings = fileNames[i].split(",");
// 选择出每一个附件名
String fileName = strings[0];
String displayName = strings[1];
// 得到数据源
FileDataSource fds = new FileDataSource(fileName);
// 得到附件本身并至入BodyPart
messageBodyPart2.setDataHandler(new DataHandler(fds));
// 得到文件名同样至入BodyPart
messageBodyPart2.setFileName(MimeUtility
.encodeText(displayName));
multipart.addBodyPart(messageBodyPart2);
}
}
mimeMsg.setContent(multipart);
// 设置信件头的发送日期
mimeMsg.setSentDate(new Date());
mimeMsg.saveChanges();
// 发送邮件
Transport.send(mimeMsg);
}

/**
* 关闭连接
*/
public void close() {
try {
transport.close();
} catch (MessagingException e) {
throw new MailException("关闭连接出错",e);
}
}

/**
* 发送邮件(DEFAULT_MIME_TYPE)
*
* @param toEmails
* 邮件接收地址列表
* @param subject
* 主题
* @param content
* 正文
* @param fileNames
* 附件列表(文件名,显示名称). <code>null</code>表示无附件.
*
* @throws MessagingException
* @throws UnsupportedEncodingException
* 如果读取附件或发送失败
*/
public void send(String[] toEmails, String subject, String content,
String[] fileNames) throws MessagingException,
UnsupportedEncodingException {
this.send(toEmails, subject, content, fileNames, DEFAULT_MIME_TYPE);
}

/**
* 发送邮件(无附件,DEFAULT_MIME_TYPE)
*
* @param toEmails
* 邮件接收地址列表
* @param subject
* 主题
* @param content
* 正文
*
* @throws MessagingException
* @throws UnsupportedEncodingException
* 如果读取附件或发送失败
*/
public void send(String[] toEmails, String subject, String content)
throws MessagingException, UnsupportedEncodingException {
this.send(toEmails, subject, content, null, DEFAULT_MIME_TYPE);
}

/**
* 发送邮件(无正文,无附件,DEFAULT_MIME_TYPE)
*
* @param toEmails
* 邮件接收地址列表
* @param subject
* 主题
*
* @throws MessagingException
* @throws UnsupportedEncodingException
* 如果读取附件或发送失败
*/
public void send(String[] toEmails, String subject)
throws MessagingException, UnsupportedEncodingException {
this.send(toEmails, subject, "", null, DEFAULT_MIME_TYPE);
}
}



import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;

/**
* 邮件服务器验证
*/
public class MailAutherticator extends Authenticator {
private String username;
private String userpass;

public void setUsername(String username) {
this.username = username;
}

public void setUserpass(String userpass) {
this.userpass = userpass;
}

public MailAutherticator(String username, String userpass) {
super();
setUsername(username);
setUserpass(userpass);
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, userpass);
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值