闲来没事研究了一下javamail发邮件。虽然比Spring框架的mail复杂一些,但是还是值得研究一下的。
mail.jar和本程序的jar包都会在附件中上传,供大家使用学习。
第一个类,用于密码验证,注意看注释,很重要。
package com.xian.gmail; import javax.mail.Authenticator; import javax.mail.PasswordAuthentication; /** * JavaMail的密码验证器 * @author 贾小仙 */ public class MyAuther extends Authenticator{ String userName=null; String password=null; public MyAuther() { } public MyAuther(String userName, String password) { this.userName = userName; this.password = password; } //getPasswordAuthentication 名字必须对。 源码中是return null。相当于重写 protected PasswordAuthentication getPasswordAuthentication(){ return new PasswordAuthentication(userName,password); } }
第二个类是实体类,做一些基本参数处理
package com.xian.gmail; public class MailSenderInfo { // 发送邮件的服务器的IP和端口 private String host; private String port="25"; // 邮件发送者的地址 private String address; // 邮件接收者的地址 private String toAddress; // 登陆邮件发送服务器的用户名和密码 private String userName; private String password; // 是否需要身份验证 private Boolean Validate=true; // 邮件主题 private String subJect; // 邮件的文本内容 private String context; // 邮件附件的文件名 private String[] fileName; /** * 获得邮件会话属性 */ /* public Properties getProperties(){ Properties pro=new Properties(); pro.put("mail.smtp.host",this.host); pro.put("mail.smtp.port", this.port); pro.put("mail.smtp.auth", Validate ? "true":"false"); return pro; }*/ public String getHost() { return host; } public void setHost(String host) { this.host = host; } public String getPort() { return port; } public void setPort(String port) { this.port = port; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String getToAddress() { return toAddress; } public void setToAddress(String toAddress) { this.toAddress = toAddress; } public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Boolean IsValidate() { return Validate; } public void setValidate(Boolean validate) { this.Validate=validate; } public String getSubJect() { return subJect; } public void setSubJect(String subJect) { this.subJect = subJect; } public String getContext() { return context; } public void setContext(String context) { this.context = context; } public String[] getFileName() { return fileName; } public void setFileName(String[] fileName) { this.fileName = fileName; } }
第三个类就是实现类了,发送邮件的具体操作都在这里
package com.xian.gmail; import java.util.Date; import java.util.Properties; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; 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.Address; import javax.mail.Multipart; /** * 简单邮件(不带附件的邮件)发送器 */ public class MailSender { public boolean sendTextMail(MailSenderInfo mailInfo){ // 判断是否需要身份认证 MyAuther authenticator = null; if (mailInfo.IsValidate()) { // 如果需要身份认证,则创建一个密码验证器 authenticator = new MyAuther(mailInfo.getUserName(), mailInfo.getPassword()); } Properties pro=getProperties(mailInfo); // 根据邮件会话属性和密码验证器构造一个发送邮件的session Session sendMailSession = Session.getDefaultInstance(pro,authenticator); try { // 根据session创建一个邮件消息 Message mailMessage = new MimeMessage(sendMailSession); // 创建邮件发送者地址 Address from = new InternetAddress(mailInfo.getAddress()); // 设置邮件消息的发送者 mailMessage.setFrom(from); // 创建邮件的接收者地址,并设置到邮件消息中 Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO,to); // 设置邮件消息的主题 mailMessage.setSubject(mailInfo.getSubJect()); // 设置邮件消息发送的时间 mailMessage.setSentDate(new Date()); // 设置邮件消息的主要内容 String mailContent = mailInfo.getContext(); mailMessage.setText(mailContent); // 发送邮件 Transport.send(mailMessage); return true; } catch (MessagingException ex) { ex.printStackTrace(); } return false; } /** * 用HTML格式发送邮件 * @param mailInfo 邮件基本信息 * @return 成功 * @author 贾小仙 */ public boolean sendHtmlMail(MailSenderInfo mailInfo){ MyAuther auth=null; if(mailInfo.IsValidate()){ auth=new MyAuther(mailInfo.getUserName(), mailInfo.getPassword()); } Properties pro=getProperties(mailInfo); try { Session sendMailSession = Session.getDefaultInstance(pro, auth); Message msg = new MimeMessage(sendMailSession); Address address = new InternetAddress(mailInfo.getAddress()); msg.setFrom(address); Address toAddress=new InternetAddress(mailInfo.getToAddress()); msg.setRecipient(Message.RecipientType.TO, toAddress); msg.setSubject(mailInfo.getSubJect()); msg.setSentDate(new Date()); // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 Multipart mailPart=new MimeMultipart(); BodyPart html =new MimeBodyPart(); html.setContent(mailInfo.getContext(),"text/html;charset=utf-8"); mailPart.addBodyPart(html); // 将MiniMultipart对象设置为邮件内容 msg.setContent(mailPart); // 发送邮件 Transport.send(msg); return true; } catch (MessagingException e) { // TODO: handle exception e.printStackTrace(); } return false; } public Properties getProperties(MailSenderInfo mailInfo){ Properties pro = new Properties(); String mailName=mailInfo.getUserName(); String mailType=mailName.substring(mailName.indexOf('@')+1, mailName.length()); if(mailType.indexOf("gmail")!=-1){ pro.setProperty("mail.smtp.host", "smtp.gmail.com"); pro.setProperty("mail.smtp.port", "465"); pro.setProperty("mail.smtp.auth", "true"); pro.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); pro.setProperty("mail.smtp.socketFactory.fallback", "false"); pro.setProperty("mail.smtp.socketFactory.port", "465"); pro.setProperty("mail.smtp.ssl", "true"); } if(mailType.equals("163.com")||mailType.equals("sohu.com")||mailType.equals("126.com")){ String mailserver = "smtp."+mailType; pro.setProperty("mail.smtp.host", mailserver); pro.setProperty("mail.smtp.port", "25"); pro.setProperty("mail.smtp.auth", "true"); } if (mailType.indexOf("sina")!=-1){ pro.setProperty("mail.smtp.host", "smtp.sina.com.cn"); pro.setProperty("mail.smtp.port", "25"); pro.setProperty("mail.smtp.auth", "true"); } return pro; } }
第四个类 相当于一个接口类,用来传参数。
package com.xian.gmail; public class Send { /** * 发送邮件所需要的参数 用户名,密码,收件人,主题,内容 * @param userName * @param password * @param toAddress * @param subJect * @param context */ public static void sendMail(String userName,String password,String toAddress,String subJect,String context){ MailSenderInfo mailInfo=new MailSenderInfo(); mailInfo.setUserName(userName); mailInfo.setPassword(password); mailInfo.setAddress(userName); mailInfo.setToAddress(toAddress); mailInfo.setSubJect(subJect); mailInfo.setContext(context); MailSender send=new MailSender(); //这个类主要来发送邮件 boolean result=send.sendTextMail(mailInfo);//发送文体格式 if(result){ System.out.println("发送成功"); }else{ System.out.println("发送失败"); } }; }
第五个类 具体调用
import com.xian.gmail.Send; public class GoMail extends Send{ public void main(){ } public void main(String[] args) { Send.sendMail("*****@163.com", "*****", "***@163.com", "你好呀", "Can you speek English?Mr 小贾"); } }
简单的实现,通用126,163,sohu,sina,gmail。 qq忘记了自己添加
51cto上jar包非法文件,已上传csdn
下载链接: http://download.csdn.net/detail/jiazhipeng12/8578161