javamai发送邮件

说明:把javamail jar包引入项目中,然后写1,2 这两个类,就可以简单时间发送邮件功能(可以是群发)。

代码现状及修改建议:发件人账号密码是在项目中写死的,需要提到配置文件中,这样需要根据不同发件人邮箱,设置不同的mail.smtp.host,说白了就是邮件服务器,163邮箱需要设置为smtp.163.com,qq邮箱需要设置为smtp.qq.com,这样就可以把邮件发送到邮件厂商服务器,然后邮件厂商服务器把邮件转发给收件人。

1 验证权限类:

public class EmailAuthenticatorbean extends Authenticator{ 
private String m_username = null; 
private String m_userpass = null; 


public void setUsername(String username){ 
m_username = username; 



public void setUserpass(String userpass){ 
m_userpass = userpass; 



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

public PasswordAuthentication getPasswordAuthentication(){ 
return new PasswordAuthentication(m_username,m_userpass); 


2 实现发送邮件类:

public class MailSenderBean {

private static final Logger logger = Logger.getLogger(MailSenderBean.class);
/**

* @param subject:要发送邮件的标题
* @param String[] to:目的邮箱的集合(要发送到邮箱地址集合)
* @param text:要发送邮件的内容
* @param String[] filenames:邮件附加的集合
*/
@SuppressWarnings("static-access")
public static void sendmail(String subject, String[] to,String text) throws Exception {
if(StringUtils.isBlank(subject)){
logger.debug("邮件主题不能为空");
return ;
}
if(null == to || to.length == 0){
logger.debug("邮件收件人不能为空");
return ;
}
if(StringUtils.isBlank(text)){
logger.debug("邮件内容不能为空");
return ;
}
//smtp: 设置发送邮件所用到的smtp(邮件协议)
// String stmpservice = "smtp.163.com"; 
String stmpservice = "smtp.spreadgroup.net"; 
//name: 发送者邮箱地址(用户名)
// String name = "[email protected]";
String name = "[email protected]";  
//password: 发送者邮箱密码
String password = "123QWE";
//from: 从那里发送
// String from = "[email protected]";
String from = "[email protected]";  

//发送邮件的格式
String mimeType = "text/html;charset=utf-8";

// ResourceBundle mailProps = ResourceBundle.getBundle("mail");
// 可以从配置文件读取相应的参数
Properties props = new Properties();
javax.mail.Session mailSession; // 邮件会话对象
javax.mail.internet.MimeMessage mimeMsg; // MIME邮件对象
props = java.lang.System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", stmpservice); // 设置SMTP主机
props.put("mail.smtp.auth", "true"); // 是否到服务器用户名和密码验证   
// 到服务器验证发送的用户名和密码是否正确
EmailAuthenticatorbean myEmailAuther = new EmailAuthenticatorbean(name, password);
// 设置邮件会话
mailSession = javax.mail.Session.getInstance(props,(Authenticator) myEmailAuther);
// 设置传输协议
javax.mail.Transport transport = mailSession.getTransport("smtp");
// 设置from、to等信息
mimeMsg = new javax.mail.internet.MimeMessage(mailSession);
if (!StringUtils.isEmpty(from)) {
InternetAddress sentFrom = new InternetAddress(from);
mimeMsg.setFrom(sentFrom); // 设置发送人地址
}
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
logger.debug("邮件收件人:" + to[i]);
sendTo[i] = new InternetAddress(to[i]);
}
mimeMsg.setRecipients(javax.mail.internet.MimeMessage.RecipientType.TO,sendTo);
mimeMsg.setSubject(subject, "utf-8");
MimeBodyPart messageBodyPart1 = new MimeBodyPart();
// messageBodyPart.setText(UnicodeToChinese(text));
messageBodyPart1.setContent(text, mimeType);
Multipart multipart = new MimeMultipart();// 附件传输格式
multipart.addBodyPart(messageBodyPart1);
mimeMsg.setContent(multipart);
// 设置信件头的发送日期
mimeMsg.setSentDate(new Date());
mimeMsg.saveChanges();
// 发送邮件
transport.send(mimeMsg);
transport.close();  
}
}

你可能感兴趣的:(javamai发送邮件)