项目源码下载:http://download.csdn.net/detail/liangrui1988/6720047
效果图:
相关代码:
test1
package com.mail; import java.util.Properties; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * com.mail.reference下面的是在网上找的,还没来得级看 * Test 是本人写的,并都测试成功发送了的 邮箱和密码换成你自已的, * 可能会遇到一个问题, 就是要把你自已myeclipse的javaee.jar文件中的mail包给删掉 * 他们会冲突,自已到网上找一个具体的操作吧! * @author liangrui * */ public class Test1 { public static void main(String[] args) throws Exception { System.out.println("hello...."); Properties perps=new Properties(); //认证 perps.setProperty("mail.smtp.auth", "true"); //协议 perps.setProperty("mail.transport.protocol", "smtp"); Session session=Session.getInstance(perps); Message mes=new MimeMessage(session); //收件 内容 、 人 mes.setText(" hello worldddddddddddddddddd"); //设置发件人地址 mes.setFrom(new InternetAddress("liangrui_1988@126.com")); //发件信息 Transport transport=session.getTransport(); transport.connect("smtp.126.com",25,"liangrui_1988","******"); //收件人 transport.sendMessage(mes, new Address[]{new InternetAddress("liangrui_1988@sina.com")}); transport.close(); System.out.println("-----"); } }
test2
package com.mail; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class Test2 { /** * @param args * @throws MessagingException */ public static void main(String[] args) throws Exception { //邮件Properties设置============================================ Properties pro=new Properties(); //认证 pro.setProperty("mail.smtp.auth", "true"); //协议 pro.setProperty("mail.transport.protocol", "smtp"); //主机 指定服务器 端口默认 pro.setProperty("mail.host", "smtp.sina.com"); //邮件Session设置============================================ Session session= Session.getInstance(pro, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { //用户名密码 return new PasswordAuthentication("liangrui_1988","******"); } }); //邮件信息设置============================================ Message ms=new MimeMessage(session); //地址 发件人 ms.setFrom(new InternetAddress("liangrui_1988@sina.com")); //主题 ms.setSubject("你好吗!这是邮件主题"); //地址 收件人 ms.setRecipients(RecipientType.TO, InternetAddress.parse("liangrui_1988@126.com,liangrui_1988@sohu.com,liangrui_1988@sohu.com")); //内容 ms.setContent("<span style='color:red'>文本内容 呆呆呆呵呵</span>","text/html;charset=gbk"); //发射================================= Transport.send(ms); System.out.println("okkk--------------"); } }
test2222demo
package com.mail; import java.io.FileInputStream; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage.RecipientType; public class Test2Demo { /** * @param args * @throws MessagingException */ public static void main(String[] args) throws Exception { //邮件Properties设置============================================ Properties pro=new Properties(); //认证 pro.setProperty("mail.smtp.auth", "true"); //协议 pro.setProperty("mail.transport.protocol", "smtp"); //主机 指定服务器 端口默认 pro.setProperty("mail.host", "smtp.sina.com"); //邮件Session设置============================================ Session session= Session.getInstance(pro, new Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { //用户名密码 return new PasswordAuthentication("liangrui_1988","******"); } }); //邮件信息设置============================================ /*Message ms=new MimeMessage(session); //地址 发件人 ms.setFrom(new InternetAddress("liangrui_1988@sina.com")); //主题 ms.setSubject("你好吗!这是邮件主题"); //地址 收件人 ms.setRecipients(RecipientType.TO, InternetAddress.parse("liangrui_1988@126.com,liangrui_1988@sina.com,liangrui_1988@sohu.com")); //内容 ms.setContent("<span style='color:red'>文本内容 呆呆呆呵呵</span>","text/html;charset=gbk"); //发射================================= Transport.send(ms);*/ //加载生成好的邮件 Test3 并发送 Message me=new MimeMessage(session,new FileInputStream("F:/juxunDocument/email/demo3.eml")); Transport.send(me,InternetAddress.parse("liangrui_1988@sohu.com") ); System.out.println("okkk--------------"); } }
test3
package com.mail; import java.io.FileOutputStream; import java.io.OutputStream; import java.util.Properties; import javax.activation.DataHandler; import javax.activation.DataSource; import javax.activation.FileDataSource; import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.MessagingException; import javax.mail.Session; import javax.mail.Message.RecipientType; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; import com.sun.mail.util.MimeUtil; public class Test3 { public static void main(String[] args) throws Exception { Session session=Session.getInstance(new Properties()); MimeMessage message=new MimeMessage(session); //发件人 指定别名 message.setFrom(new InternetAddress("\""+MimeUtility.encodeText("国务院")+"\" <liangrui_1988@sina.com>")); message.setSubject("subject Hello world");//标题 //如果收到该邮件 点击回复时,地址是这个邮箱地址 而不是发件人 message.setReplyTo(new Address[]{new InternetAddress("liangrui_1988@sohu.com")});//回复 //收件人 message.setRecipients(RecipientType.TO, //把字符串转为数组 //转码 InternetAddress.parse( MimeUtility.encodeText("刘备")+" <liangrui_1988@126.com>," +MimeUtility.encodeText("关羽")+ "<liangrui_1988@suho.com>")); //复杂格试文件内容 多部件的/混合 MimeMultipart mutimart=new MimeMultipart("mixed"); //文本 message.setContent(mutimart); //多用途互联网邮件扩展 mime 体部分 MimeBodyPart content =new MimeBodyPart(); MimeBodyPart attach1 =new MimeBodyPart(); MimeBodyPart attach2 =new MimeBodyPart(); //添加文本 和附近 mutimart.addBodyPart(content); mutimart.addBodyPart(attach1); mutimart.addBodyPart(attach2); //附近1 DataSource ds=new FileDataSource("E:\\1y2\\context.xml"); DataHandler dh1=new DataHandler(ds); attach1.setDataHandler(dh1); attach1.setFileName(MimeUtility.encodeText("中文context.xml"));//附件名称 //附近2 DataSource ds2=new FileDataSource("F:\\ipho\\IMG_0266.JPG"); DataHandler dh2=new DataHandler(ds2); attach2.setDataHandler(dh2); attach2.setFileName( MimeUtility.encodeText("图片img_0266.jpg"));//附件名称 转为中文 //文本 是一个复杂体 MimeMultipart bodyMultipart=new MimeMultipart("related"); content.setContent(bodyMultipart); //构造 体 有两个部分组成 图片和文本 MimeBodyPart thmlPart=new MimeBodyPart(); MimeBodyPart imgPart=new MimeBodyPart(); bodyMultipart.addBodyPart(thmlPart);//html bodyMultipart.addBodyPart(imgPart);//img //图片数据来源 DataSource dsImg=new FileDataSource("F:/ipho/IMG_0332.JPG"); DataHandler dhImg=new DataHandler(dsImg); imgPart.setDataHandler(dhImg); //这个图片代表的位置 imgPart.setHeader("Content-location","http://www.aaa.com/img_0332.jpg"); //文本 thmlPart.setContent("<h1>你好呀 </h1><span style=color='red'>yesterday once more</sapn>" + " <img src='http://www.aaa.com/img_0332.jpg'/>", "text/html;charset=gbk"); //生成 message.saveChanges(); //保存在本地 测试 输出到本地 OutputStream os=new FileOutputStream("F:\\juxunDocument\\email\\demo3.eml"); message.writeTo(os); os.close(); } }
servelt
package com.mail.web.servlet; import java.io.IOException; import java.io.PrintWriter; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.naming.Context; import javax.naming.InitialContext; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import com.mail.Test2Demo; public class SendMail extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request,response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); try { //调用生成好的Test2Demo.jar 类发送邮件 方式一 //Test2Demo.main(new String[]{}); /** * JNDI 配制方式发送邮件 方式二 * META-INF文件夹中配context.xml信息 * 在web.xml配置相关信息 * 发布到tomcat时 * 需要把lib 的mail.jar拷贝到Tomcat的lib下 * 并且删掉项目中的mail.jar */ Context initCtx = new InitialContext(); Context envCtx = (Context) initCtx.lookup("java:comp/env"); Session session = (Session) envCtx.lookup("mail/hello"); Message message = new MimeMessage(session); //发件人 message.setFrom(new InternetAddress("liangrui_1988@sina.com")); //收件人地址 InternetAddress to[] = new InternetAddress[1]; to[0] = new InternetAddress("liangrui_1988@sohu.com"); message.setRecipients(Message.RecipientType.TO, to); message.setSubject("测试主题");//主题 message.setText("test email ok---"); //Transport Transport tran=session.getTransport(); tran.connect("smtp.sina.com","liangrui_1988","*****");//获得连接 tran.sendMessage(message, to); tran.close(); out.print("ok----------------"); } catch (Exception e) { e.printStackTrace(out); } out.flush(); out.close(); } }
META-INF
context.xml
<Context> <Resource name="mail/hello" auth="Container" type="javax.mail.Session" mail.smtp.host="smtp.sina.com" mail.smtp.auth="true" mail.transport.protocol="smtp" /> </Context>
作者:liangrui1988 发表于2013-12-16 0:15:14 原文链接
阅读:84 评论:0 查看评论