Java 封装的邮件发送类,可直接调用(基于Maven)
2014-12-09 14:53
323 查看
maven的pom.xml中加入依赖
原封不动的一个工具类
AjavaSendMail.java
自己打包的,根据自己需要进行修改的第二个工具类
MailUtil.java
<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); // 使用验证,创建一个Authenticator Session session = Session.getDefaultInstance(props, auth);// 根据Properties,Authenticator创建Session try { 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; } @Override protected 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(); } }
相关文章推荐
- 数据结构中的栈,在解决很多问题都有用处,比如括号匹配,迷宫求解,表达式求值等等 java中有封装好的类,可以直接调用。
- 基于淘宝IP地址库的Java调用封装(Java)
- Java系列--第四篇 基于Maven的SSME之发送邮件
- jquery ajax再次封装,前台调用后台java方法直接返回数据
- 基于SMTP的JAVA邮件发送程序
- 在JAVA程序中直接执行JAVASCRIPT代码(简单调用实例)
- 基于Ajax和JSON从javascript中调用后台java方法的JsonGateway
- 基于Ajax和JSON从javascript中调用后台java方法的JsonGateway
- [JNI] Eclipse直接完成JAVA调用C/C++ (Eclipse上使用CDT结合MinGW)
- 【转】javamail-基于SMTP的JAVA邮件发送程序!
- 利用Spring框架封装的JavaMail现实同步或异步邮件发送
- C#(1.1)邮件发送类,功能全面,调用灵活、方便
- JAVA基于HTTPS的加密远程调用的实现
- [原创]Android 基于NDK的JNI开发 C调用java和java调用C的进阶教程
- oracle中直接调用java代码执行业务逻辑。。
- JSP中直接调用Spring接管的JAVABEAN
- 使用java调用存储过程的通用封装
- 在不知道excel安装路径下用 java 直接调用系统的excel
- JAVA调用Tuxedo服务——使用JNI封装Tuxedo客户端的一个例子
- java版的webservice,完全用java的正则表达式对websercice的xml进行解析,可以很快用到web中,只需要调用其中的封装好的方法