package com.hx.mail; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Session; import javax.mail.PasswordAuthentication; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * MailServices 邮件接收发送接口实现类 * * @author 380595305@qq.com * Date 2010-05-11 * @version 1.0 */ public class KumMailServicesImpl implements KumMailServices { /** MailboxType 邮箱类型 */ private static Map<String,String> MailboxTypes = null; /** host 邮箱服务器类型 */ private String host = null; /** sender 邮件发送者 */ private String sender = null; /** addressee 邮件接收者 */ private String addressee = null; /** subject 邮件主题 */ private String subject = null; /** text 邮件消息 */ private String text = null; public static void init(){ MailboxTypes = new HashMap<String,String>(); MailboxTypes.put("163","smtp.163.com"); MailboxTypes.put("139","smtp.139.com"); MailboxTypes.put("126","smtp.126.com"); MailboxTypes.put("qq", "smtp.qq.com"); MailboxTypes.put("live","smtp.live.cn"); MailboxTypes.put("msn","smtp.msn.com"); MailboxTypes.put("kum", "mail.kum.net.cn"); MailboxTypes.put("hotmail","smtp.hotmail.cn"); } /** * initialization 实例化类成员变量 */ private void initialization(String sender,String addressee,String subject,String text){ this.sender = sender; this.addressee = addressee; this.subject = subject; this.text = text; this.host = getHost(sender); //System.out.println("sender->"+this.sender+" | addressee->"+this.addressee+" | subject->"+this.subject+" | text->"+this.text+" | host->"+this.host); } /** * getHost 获取目标邮箱服务器类型 * * @param sender 是String类型,传入邮件发送者邮箱地址信息 * @return String 返回目标邮箱服务器类型 */ private String getHost(String sender){ String _host,_host_ = null; _host = sender.substring(sender.indexOf("@")+1,sender.indexOf(".")); if(MailboxTypes==null){ init(); } _host_ = MailboxTypes.get(_host); // System.out.println(_host+" <--> "+_host_); if(_host_==null){ //MailboxTypes.put(_host,"smtp."+_host+".com"); MailboxTypes.put(_host,"smtp."+sender.substring(sender.indexOf("@")+1,sender.length())); } return MailboxTypes.get(_host); } public void sendMail(String sender, String password, String addressee, String subject, String text) throws Exception { initialization(sender,addressee,subject,text); Properties props = System.getProperties(); { props.put("mail.smtp.host",this.host); props.put("mail.smtp.auth","true"); } ValidateAuther auther = new ValidateAuther(this.sender,password); Session session = Session.getDefaultInstance(props,auther); MimeMessage msg = new MimeMessage(session); InternetAddress fromAddr = new InternetAddress(this.sender); // 发送者邮箱地址 InternetAddress toAddr = new InternetAddress(this.addressee); // 接收者邮箱地址 msg.setFrom(fromAddr); msg.addRecipient(Message.RecipientType.TO, toAddr); /** * Message.RecipientType.TO -- 接收者 * Message.RecipientType.CC -- 抄送 * Message.RecipientType.BCC --秘密抄送者 */ msg.setSubject(this.subject); // 邮件主题 msg.setText(this.text); // 邮件信息 Transport.send(msg); // 发送邮件 } } /** * ValidateAuther 邮件验证类,验证邮件发送者信息 */ class ValidateAuther extends Authenticator { /** 邮件发送者 */ private String sender; /** 邮件接受者 */ private String password; /** * ValidateAuther 验证邮件发送者信息 * * @param userName 是String类型,传入邮件发送者账户 * @param password 是String类型,传入邮件发送者账户密码 */ public ValidateAuther(String sender, String password) { this.sender = sender; this.password = password; } /** * getPasswordAuthentication 验证邮件发送者账户信息的函数 * * @param userName 邮件发送者账户信息 * @param password 邮件发送者账户密码 * @return PasswordAuthentication 返回邮件发送者账户验证信息 */ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender, password); } }
- 1
- 粉丝: 5
- 资源: 44
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助