项目源码下载:/detail/liangrui1988/677
效果图:
相关代码:
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@"));//发件信息Transport transport=session.getTransport();transport.connect("",25,"liangrui_1988","******");//收件人transport.sendMessage(mes, new Address[]{new InternetAddress("liangrui_1988@")});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", "");//邮件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@"));//主题ms.setSubject("你好吗!这是邮件主题");//地址 收件人ms.setRecipients(RecipientType.TO,InternetAddress.parse("liangrui_1988@,liangrui_1988@,liangrui_1988@"));//内容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", "");//邮件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@"));//主题ms.setSubject("你好吗!这是邮件主题");//地址 收件人ms.setRecipients(RecipientType.TO,InternetAddress.parse("liangrui_1988@,liangrui_1988@,liangrui_1988@"));//内容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@") );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@>"));message.setSubject("subject Hello world");//标题//如果收到该邮件 点击回复时,地址是这个邮箱地址 而不是发件人message.setReplyTo(new Address[]{new InternetAddress("liangrui_1988@")});//回复//收件人message.setRecipients(RecipientType.TO,//把字符串转为数组 //转码InternetAddress.parse(MimeUtility.encodeText("刘备")+" <liangrui_1988@>,"+MimeUtility.encodeText("关羽")+ "<liangrui_1988@>"));//复杂格试文件内容 多部件的/混合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);//附近1DataSource ds=new FileDataSource("E:\\1y2\\context.xml");DataHandler dh1=new DataHandler(ds);attach1.setDataHandler(dh1);attach1.setFileName(MimeUtility.encodeText("中文context.xml"));//附件名称//附近2DataSource 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);//htmlbodyMultipart.addBodyPart(imgPart);//img//图片数据来源DataSource dsImg=new FileDataSource("F:/ipho/IMG_0332.JPG");DataHandler dhImg=new DataHandler(dsImg);imgPart.setDataHandler(dhImg);//这个图片代表的位置imgPart.setHeader("Content-location","/img_0332.jpg");//文本thmlPart.setContent("<h1>你好呀 </h1><span style=color='red'>yesterday once more</sapn>" +" <img src='/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@"));//收件人地址InternetAddress to[] = new InternetAddress[1];to[0] = new InternetAddress("liangrui_1988@");message.setRecipients(Message.RecipientType.TO, to);message.setSubject("测试主题");//主题message.setText("test email ok---");//TransportTransport tran=session.getTransport();tran.connect("","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=""mail.smtp.auth="true"mail.transport.protocol="smtp"/></Context>