- 浏览: 484609 次
- 性别:
- 来自: xiamen
文章分类
最新评论
-
陈乐鹏:
你这个下载要怎么玩?我这里老提示未安装!
[转]使用ScriptX控件进行Web打印总结 -
zqb666kkk:
博主有示例代码吗
cxf ws-security 加密和签名的实现 -
mclin0704:
你好,请问WSDL2Java 可以用java实现吗?
Java2WSDL之java实现 -
hubiao0629:
楼主,这个文件会有不兼容的问题把。
mysql5忘记密码后的处理方法 -
zhaoyubetter:
OK,可以试试!多谢楼主
后台管理界面收集
java 发送mail,很久之前整过,没做记录,这次整理下做个记录。
java 发送mail
import java.awt.List; import java.io.IOException; import java.util.Date; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message.RecipientType; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import com.biokee.dlafis.util.GetProperties; // http://www.cnblogs.com/snoopy/articles/129932.html // http://blog.csdn.net/zlz3907/article/details/3958081 // http://www.4ucode.com/Study/Topic/445710 public class JavaMail { String host; String userName; // 邮箱用户名 String password ; // 密码 String from; // 发送的邮箱名称 String contentType; // 邮件内容格式(文本或html) String replyTo; // 回复地址,默认是发送地址 String to; // 发给谁 String cc; // 抄送 String bcc; // 暗送 String subject; // 邮件议题 String body; // 内容 String fileName; // 附件路径(注意绝对路径和相对路径的选择) // 用于保存发送附件的文件名列表 List arrFileName = new List(); // 用于收集附件名 public void attachFile(String fileName) { this.arrFileName.add(fileName); } // 开始发送 @SuppressWarnings("static-access") public boolean sendMail(String to,String cc ,String bcc, String subject, String body) throws IOException { // 处理中文乱码 subject = new String(subject.getBytes(),"gb2312"); body = new String(body.getBytes(),"gb2312"); // 创建Properties对象 Properties props = System.getProperties(); // 创建信件服务器 props.put("mail.smtp.host", this.host); // props.put("mail.smtp.host", "smtp.163.com"); props.put("mail.smtp.auth", "true"); // 通过验证 props.put("mail.smtp.connectiontimeout", "10000"); // props.put("mail.smtp.timeout", "10000"); // // 得到默认的对话对象 Session session = Session.getDefaultInstance(props, null); try { // 创建一个消息,并初始化该消息的各项元素 MimeMessage msg = new MimeMessage(session); msg.setFrom(new InternetAddress(this.from)); if (to.trim().length() > 0 || cc.trim().length()>0 || bcc.trim().length()>0) { if(to!=null && to.trim().length() > 0 ){ sendAddress(to, msg,Message.RecipientType.TO); } if(cc!=null && cc.trim().length()>0){ sendAddress(cc, msg,Message.RecipientType.CC); } if(bcc!=null && bcc.trim().length()>0){ sendAddress(bcc, msg,Message.RecipientType.BCC); } } else { System.out.println("None receiver! The program will be exit!"); System.exit(0); } // 添加议题 msg.setSubject(subject); // 后面的BodyPart将加入到此处创建的Multipart中 Multipart mp = new MimeMultipart(); // 获取附件 int FileCount = this.arrFileName.getItemCount(); if (FileCount > 0) { for (int i = 0; i < FileCount; i++) { MimeBodyPart mbp = new MimeBodyPart(); // 选择出附件名 fileName = arrFileName.getItem(i).toString(); // 得到数据源 FileDataSource fds = new FileDataSource(new String(fileName.getBytes(),"UTF-8")); // 得到附件本身并至入BodyPart mbp.setDataHandler(new DataHandler(fds)); // 得到文件名同样至入BodyPart mbp.setFileName(fds.getName()); mp.addBodyPart(mbp); } MimeBodyPart mbp = new MimeBodyPart(); mbp.setText(body); mbp.setContent(body,"text/html;charset=gb2312"); mp.addBodyPart(mbp); // 移走集合中的所有元素 arrFileName.removeAll(); // Multipart加入到信件 msg.setContent(mp); } else { // 设置邮件正文 if (contentType == null || contentType.equals("text")) { msg.setText(body); } else if (contentType.equals("html")) { // 未处理图片 System.out.println(11); MimeBodyPart mbp = new MimeBodyPart(); // mbp.setText(body); mbp.setContent(body,"text/html;charset=gb2312"); mp.addBodyPart(mbp); // Multipart加入到信件 msg.setContent(mp); } } // 设置回复的地址 InternetAddress[] add = new InternetAddress[1]; add[0] = new InternetAddress("xxxx@126.com"); msg.setReplyTo(add); // 设置信件头的发送日期 msg.setSentDate(new Date()); msg.saveChanges(); // 发送信件 Transport transport = session.getTransport("smtp"); transport.connect(this.host, this.userName, this.password); transport.sendMessage(msg, msg.getAllRecipients()); transport.close(); // msg.writeTo(System.out); System.out.println("send mail ok"); } catch (MessagingException mex) { System.out.println("send mail error"+mex.getMessage()); mex.printStackTrace(); Exception ex = null; if ((ex = mex.getNextException()) != null) { ex.printStackTrace(); } return false; } return true; } // 处理发送地址, to /cc /bcc 未校验邮件格式 private void sendAddress(String sendAddress, MimeMessage msg,RecipientType type)throws AddressException, MessagingException { String[] arr = sendAddress.split(","); // int ReceiverCount=1; int receiverCount = arr.length; if (receiverCount > 0) { InternetAddress[] address = new InternetAddress[receiverCount]; for (int i = 0; i < receiverCount; i++) { address[i] = new InternetAddress(arr[i]); } msg.addRecipients(type, address); } } /** * 读取配置文件,并设置对应参数 */ public void setSendMailParam(){ GetProperties g = new GetProperties(); this.host=g.getPropertiesValue("host").trim(); this.userName=g.getPropertiesValue("userName").trim(); this.password=g.getPropertiesValue("password").trim(); this.from=g.getPropertiesValue("from").trim(); this.replyTo=g.getPropertiesValue("replyTo").trim(); this.contentType=g.getPropertiesValue("contentType").trim(); } /** * @param args */ public static void main(String[] args) { String content="测试来了 Hello world!<br/> " + "<tabl border=\"1\" align=\"center\" cellpadding=\"1\" cellspacing=\"1\">" + "<tr><td>name</td><td>address</td></tr>" + "<tr><td>aaa</td><td>d:\\1.txt</td></tr></table>";//邮件内容 String subject="测试2";//邮件主题 String mailTo1="xxxx@163.com"; //接收邮件的地址1 String mailTo2=""; // String mailTo2="xxxx@126.com"; //接收邮件的地址2 String attach1="d:\\1.txt"; //附件一地址 String attach2="d:\\a.txt"; //附件二地址 JavaMail mail=new JavaMail(); mail.setSendMailParam(); try { // mail.attachFile(attach1); // mail.attachFile(attach2); mail.sendMail(mailTo1+","+mailTo2, null,null, subject,content); } catch (Exception e) { e.printStackTrace(); } } }
apache Commons-Email
import org.apache.commons.mail.EmailAttachment; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; import org.apache.commons.mail.MultiPartEmail; import org.apache.commons.mail.SimpleEmail; import com.biokee.dlafis.util.GetProperties; public class CommonsEmail { String host; String userName; // 邮箱用户名 String password ; // 密码 String from; // 发送的邮箱名称 String contentType; // 邮件内容格式(文本或html) String replyTo; // 回复地址,默认是发送地址 String to; // 发给谁 String cc; // 抄送 String bcc; // 暗送 String subject; // 邮件议题 String body; // 内容 public static void main(String[] args) { CommonsEmail emails = new CommonsEmail(); try { emails.setSendMailParam(); emails.send("xxx@163.com",null,null); // emails.sendAttachMail(); } catch (EmailException e) { // TODO Auto-generated catch block e.printStackTrace(); } } // http://wisekingokok.iteye.com/blog/456655 // http://www.blogjava.net/bulktree/archive/2008/04/07/191170.html // http://apps.hi.baidu.com/share/detail/23398910 public void send(String to,String cc,String bcc) throws EmailException { // SimpleEmail email = new SimpleEmail(); HtmlEmail email = new HtmlEmail(); //设置发送主机的服务器地址 email.setHostName(host); //设置收件人邮箱 ,群发需要多次添加 email.addTo(to); //发件人邮箱 email.setFrom(from); //如果要求身份验证,设置用户名、密码,分别为发件人在邮件服务器上注册的用户名和密码 email.setAuthentication(userName, password); //设置邮件的主题 email.setSubject("Hello, This is My First Email Application"); //邮件正文消息 // email.setMsg("I am bulktree This is JavaMail Application"); email.setHtmlMsg("I am bulktree This is JavaMail Application"); email.send(); System.out.println("The SimpleEmail send sucessful!!!"); } public void sendAttachMail() throws EmailException{ // 创建一个Email附件 ,多附件多个处理 EmailAttachment emailattachment = new EmailAttachment(); emailattachment.setPath("d:/1.txt"); // emailattachment.setURL(new URL("http://www.blogjava.net/bulktree/picture/bulktree.jpg")); emailattachment.setDisposition(EmailAttachment.ATTACHMENT); emailattachment.setDescription("This is Smile picture"); emailattachment.setName("1.txt"); // 创建一个email MultiPartEmail multipartemail = new MultiPartEmail(); multipartemail.setHostName(host); multipartemail.addTo("xxxx@163.com", "josn"); multipartemail.setFrom(from, "feng"); multipartemail.setAuthentication(userName, password); multipartemail.setSubject("This is a attachment Email"); multipartemail.setMsg("this a attachment Eamil Test"); //添加附件 multipartemail.attach(emailattachment); //发送邮件 multipartemail.send(); System.out.println("The attachmentEmail send sucessful!!!"); } /** * 读取配置文件,并设置对应参数 */ public void setSendMailParam(){ GetProperties g = new GetProperties(); this.host=g.getPropertiesValue("host").trim(); this.userName=g.getPropertiesValue("userName").trim(); this.password=g.getPropertiesValue("password").trim(); this.from=g.getPropertiesValue("from").trim(); this.replyTo=g.getPropertiesValue("replyTo").trim(); this.contentType=g.getPropertiesValue("contentType").trim(); } }
GetProperties:
import java.io.InputStream; import java.net.URL; import java.util.Properties; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; public class GetProperties { private final Log log = LogFactory.getLog(getClass()); /** * 获取Properties 某个键 -值 * @param name * @return */ public String getPropertiesValue(String name){ String propertiesFile="application.properties"; URL url=getClass().getResource("/" + propertiesFile); String value=null; try { InputStream in = url.openStream(); Properties p = new Properties(); p.load(in); in.close(); value = p.getProperty(name); } catch (Exception e) { log.error(e); e.printStackTrace(); } return value; } }
spring 发送mail 就不写了,给个参考链接
http://huqilong.blog.51cto.com/53638/133210
附件是需要的jar 包!
- activation.jar (44.3 KB)
- 下载次数: 2
- commons-email-1.2.jar (31.6 KB)
- 下载次数: 2
- mail.jar (362.6 KB)
- 下载次数: 4
发表评论
-
Mac 安装maven
2017-03-08 23:05 420Mac OS X 安装Maven: 下载 Maven ... -
MAC 安装eclipse svn 插件
2017-03-08 22:48 612参考此链接: http://www.cnblogs.com ... -
tomcat 单机多实例
2015-07-08 09:06 798tomcat 部署单机多实例,实现每个实例都是跟 ... -
java 分块上传
2014-05-30 14:56 1374java 分块下载 java多线程分块上传并 ... -
bat class to java Jad反编译
2013-12-04 14:58 1137反编译所使用的工具是jad.exe 有时候需要批量 ... -
ant svn 自动更新和发布
2012-05-23 16:42 2307团队管理中,当项目非常多,而且发布频繁,手动发布往往非常繁琐, ... -
DatabaseMetaData getExportedKey() 无法获取信息
2012-03-14 17:26 1127在jdbc DatabaseMetaData.getExpo ... -
Column 'IS_AUTOINCREMENT' not found
2012-03-14 10:41 2415java.sql.SQLException: Column ' ... -
java 递归加减法
2012-01-12 17:30 1539有时候设计到list map 做交集,并集等操作。 可以参考 ... -
java ant 压缩与解压缩
2011-09-15 16:05 2405以下本文来自: http://blog.csdn.net/l ... -
Java源码中的发现:快速判断一个int值是几位数
2011-07-22 15:57 3991判断一个int值是几位数,要是我自己实现,估计又会想到除法和模 ... -
Weblogic 部署SSL
2011-07-15 16:14 887weblogic 部署SSL 整理网路上的 一部分由 ... -
CXF客户端配置请求超时限制
2011-07-13 17:14 5076在用cxf 开发webservice应用时,客户端老报 ... -
Weblogic的基础优化!
2011-07-04 16:43 1241由于项目需要,最近研究了一下系统优化。 系统性能优化是系 ... -
判断偶数几种方法性能
2011-06-24 17:06 1310判断一个数是否偶数,在编程中经常用的,大部分人都用的是 ... -
java读取文件夹下的所有文件夹和文件
2011-05-18 10:07 1374import java.io.FileNotFoundExce ... -
ThreadLocal的使用场景
2011-04-11 17:24 1054在Java的多线程编程中,为保证多个线程对共享变量的安全访问, ... -
Java 进制之间互相转换
2011-03-30 13:34 1237public static void main(String[ ... -
B/S架构下applet,JNI实现对客户端硬件的读取
2011-03-26 10:20 1480昨天整个Applet 调用JNI 实现读取指纹的Demo,郁闷 ... -
Jocky--Java混淆编译器
2010-10-13 15:58 1906一、前言 1.1 什么是Jocky? 我们知道,Ja ...
相关推荐
java发送mail使用的jar (附带类子代码) 解压缩后,可以得到jar和类子代码 解压缩后,可以得到jar和类子代码 https://blog.csdn.net/sxzlc/article/details/104216877
java发送附件mail, 包阔多附件,文本mail,以及网页mail.
用java开发邮件发送程序,总会出现这样或那样的错误,今天真正用到了这块,写一个demo给需要的同事一个参考,里面包括: 1. 程序报错及解决方案.txt 2. MailUtils.java(程序源代码) 3. activation.jar 4. mail.jar
java 发送邮件 spring发送邮件Mail
java实现Mail发送
java mail 邮件发送 代码文档 java_mail_发送邮件代码 发送邮件及邮件附件java代码实例(支持多附件_群发送)
java实现的根据html模版内容发送邮件,这是一个完整的eclipse项目,包含jar包,导入即可运行
java mail,java发送邮件,java收发邮件,简单易用,容易上手
java发送邮件 java发送邮件 java发送邮件 java发送邮件 java发送邮件 java发送邮件 java发送邮件 java发送邮件
java mail发送qq邮件的一个例子,谢谢大家的支持
java 发送邮件sdk ,实测有效,快速发送邮件,java 发送邮件sdk
使用mail.jar包进行发送邮件支持多接收人,多附件,但是附件不可过大。
java.mail.txt java实现邮件发送的jar包,版本是1.6.0
基于javax.mail的邮件发送测试工具
JAVA_Mail_邮件_发送_接收_抄送_密送_附件 该资源是一个Java项目,可用Eclipse导入即可运行。 共4个类: 1:ReceiveMail.java -接收普通邮件 2:ReciveMailWithAttachMent.java --接收含有附件的邮件 3:SendMail....
mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送mail发送...
javax mail,带sun的jar包
java mail 发送邮件源代码以及所需要的两个架包。 附件代码以腾讯企业邮箱发送邮件为例子,其它邮箱只要支持SMTP服务即可。只要懂点JAVA就能看懂。
使用JavaMailSender包,结合Java8和springboot框架,使用公司阿里企业邮箱免费发送邮件,因为公司邮件不限数量,所以邮件测试发送无上限。对于QQ或163邮箱账户可能需要开通SMTP服务,具体信息请百度教程,有问题可以...
JAVA 发送文本格和HTML格式并带图片链接的邮件实例,图片可以为附件可以为邮件正文显示,可以为图片添加超链接