Java 封装的邮件发送类,可直接调用(基于Maven)

来源:互联网 发布:apache自定义404页面 编辑:程序博客网 时间:2024/05/17 06:44

maven的pom.xml中加入依赖

<span style="white-space: pre;"></span><dependency><groupId>com.sun.mail</groupId><artifactId>javax.mail</artifactId><version>1.5.2</version></dependency>

原封不动的一个工具类


AjavaSendMail.java

import java.util.Properties;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.mail.Authenticator;import javax.mail.BodyPart;import javax.mail.Message;import javax.mail.MessagingException;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;public class AjavaSendMail {private String from = "";// 发信邮箱public String to = "";// 收信邮箱public String subject = "";// 邮件主题private String username = "";private String password = "";public String text = "";// 邮件内容private Multipart mp; // 存放邮件的title 内容和附件private Properties props; // Properties对象private Message message; // Message存储发送的电子邮件信息public AjavaSendMail(String stmp) {setSmtpHost(stmp);}public void setSmtpHost(String smtpHostName) {System.out.println("mail.stmp.host= " + smtpHostName);if (props == null) {props = new Properties();// 创建Properties对象}props.setProperty("mail.transport.protocol", "smtp");// 设置传输协议props.put("mail.smtp.host", smtpHostName);// 设置发信邮箱的smtp地址"smtp.sina.com"props.setProperty("mail.smtp.auth", "true"); // 验证}public boolean createMimeMessage() {Authenticator auth = new AjavaAuthenticator(username, password); // 使用验证,创建一个AuthenticatorSession session = Session.getDefaultInstance(props, auth);// 根据Properties,Authenticator创建Sessiontry {if (message == null) {message = new MimeMessage(session);// Message存储发送的电子邮件信息}message.setFrom(new InternetAddress(from));message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));// 设置收信邮箱message.setSubject(subject);// 设置主题return true;} catch (MessagingException e) {e.printStackTrace();return false;}}public boolean setOut() {try {message.setContent(mp);message.saveChanges();// add by andy 2012-03-08// System.out.println("[INFO] text="+text);// message.setText(text);// 设置内容System.out.println("[INFO] 开始发送...");Transport.send(message);// 发送System.out.println("[INFO] 发送完成!");return true;} catch (Exception e) {System.out.println("邮箱验证出错");//e.printStackTrace();return false;}}public void setNamePass(String name, String pass) {username = name;password = pass;}public boolean setSubject(String mailSubject) {System.out.println("set title begin.");try {if (!mailSubject.equals("") && mailSubject != null) {subject = mailSubject;}return true;} catch (Exception e) {System.out.println("set Title faild!");return false;}}public boolean setFrom(String from) {System.out.println("Set From .");try {this.from = from;return true;} catch (Exception e) {return false;}}public boolean setTo(String to) {System.out.println("Set to.");if (to == null || to.equals("")) {return false;}try {this.to = to;return true;} catch (Exception e) {return false;}}public boolean setText(String text) {System.out.println("set text begin.");try {BodyPart bp = new MimeBodyPart();// bp.setContent("<meta http-equiv=Context-Type context=text/html;charset=gb2312>"+text,"text/html;charset=GB2312");bp.setContent("<meta http-equiv=Context-Type context=text/html;charset=utf-8>"+ text, "text/html;charset=UTF-8");if (mp == null) {mp = new MimeMultipart();}mp.addBodyPart(bp);return true;} catch (Exception e) {System.out.println("Set context Faild! " + e);return false;}}/** * 添加附件.. *  * @param filename * @return */public boolean addFileAffix(String filename) {System.out.println("增加附件..");if (mp == null) {mp = new MimeMultipart();}if (filename.equals("") || filename == null) {return false;}String file[];file = filename.split(";");System.out.println("你有 " + file.length + " 个附件!");try {for (int i = 0; i < file.length; i++) {BodyPart bp = new MimeBodyPart();FileDataSource fileds = new FileDataSource(file[i]);bp.setDataHandler(new DataHandler(fileds));bp.setFileName(fileds.getName());mp.addBodyPart(bp);}return true;} catch (Exception e) {System.err.println("增加附件: " + filename + "--faild!" + e);return false;}}// 创建传入身份验证信息的 Authenticator类class AjavaAuthenticator extends Authenticator {private String user;private String pwd;public AjavaAuthenticator(String user, String pwd) {this.user = user;this.pwd = pwd;}@Overrideprotected PasswordAuthentication getPasswordAuthentication() {return new PasswordAuthentication(user, pwd);}}}

自己打包的,根据自己需要进行修改的第二个工具类

MailUtil.java

import org.junit.Test;//JavaMail发送例子public class MailUtil {public static final String _adminMail = "";public static final String _password = "";public static final String _stmp = "";public static boolean sendMail(String toMail,String title,String text){AjavaSendMail sm = new AjavaSendMail(_stmp);sm.setNamePass(_adminMail, _password);sm.setSubject(title);sm.setFrom(_adminMail);sm.setTo(toMail);sm.setText(text);sm.createMimeMessage();return sm.setOut();}}


0 0
原创粉丝点击