`
dreamoftch
  • 浏览: 496581 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

周末看传智播客发邮件视频中的源码

阅读更多
package cn.itcast.javamail2;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
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.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 javax.mail.internet.MimeUtility;




import sun.misc.BASE64Encoder;

public class Base64Util {

	/**
	 * @param args add by zxx ,Dec 30, 2008
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {
		// TODO Auto-generated method stub
		BASE64Encoder encoder = new BASE64Encoder();
		System.out.println("please input user name:");
		String username = new BufferedReader(
					new InputStreamReader(System.in))
					.readLine();
		System.out.println(encoder.encode(username.getBytes()));
		System.out.println("please input password:");
		String password = new BufferedReader(
				new InputStreamReader(System.in))
				.readLine();		
		System.out.println(encoder.encode(password.getBytes()));
	}
	

}

  2:

package cn.itcast.javamail2;

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;

public class Demo1 {

	/**
	 * @param args add by zxx ,Feb 5, 2009
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		Session session = Session.getInstance(props);
		session.setDebug(true);
		
		Message msg = new MimeMessage(session);
		msg.setText("你好吗?");
		msg.setFrom(new InternetAddress("lili@sohu.com"));
	
		Transport transport = session.getTransport();
		transport.connect("smtp.sina.com", 25, "itcast_test", "123456");
		transport.sendMessage(msg,
				new Address[]{new InternetAddress("itcast_test@sohu.com")});

		//transport.send(msg,new Address[]{new InternetAddress("itcast_test@sohu.com")});
		transport.close();
	}

}

 3:

package cn.itcast.javamail2;

import java.io.FileInputStream;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class Demo2 {

	/**
	 * @param args add by zxx ,Feb 5, 2009
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		
		Properties props = new Properties();
		props.setProperty("mail.smtp.auth", "true");
		props.setProperty("mail.transport.protocol", "smtp");
		props.setProperty("mail.host", "smtp.sina.com");
		Session session = Session.getInstance(props,
				new Authenticator()
				{
					protected PasswordAuthentication getPasswordAuthentication()
					{
						return new PasswordAuthentication("itcast_test","123456");
					}
				}
		);
		session.setDebug(true);
		
		/*Message msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("itcast_xxx@sina.com"));
		msg.setSubject("中文主题");
		msg.setRecipients(RecipientType.TO, 
				InternetAddress.parse("itcast_test@sina.com,itcast_test@sohu.com"));
		msg.setContent("<span style='color:red'>中文呵呵呵</span>", "text/html;charset=gbk");
		
		
		Transport.send(msg);*/
		
		Message msg = new MimeMessage(session,new FileInputStream("resouce\\demo3.eml"));
		Transport.send(msg,InternetAddress.parse("itcast_test@sohu.com"));
	}

}

 4:

package cn.itcast.javamail2;

import java.io.FileOutputStream;
import java.io.InputStream;
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.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;

public class Demo3 {

	/**
	 * @param args add by zxx ,Feb 5, 2009
	 */
	public static void main(String[] args) throws Exception{
		// TODO Auto-generated method stub
		Session session = Session.getInstance(new Properties());
		MimeMessage msg = new MimeMessage(session);
		msg.setFrom(new InternetAddress("\"" + MimeUtility.encodeText("传智播客") + "\" <itcast_test@sina.com>"));
		msg.setSubject("你们的Java培训真的是最牛的吗?");		
		msg.setReplyTo(new Address[]{new InternetAddress("lili@126.com")});
		msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("黎活明") + " <llm@itcast.cn>," + MimeUtility.encodeText("张孝祥") + " <zxx@itcast.cn>"));
		MimeMultipart msgMultipart = new MimeMultipart("mixed");
		msg.setContent(msgMultipart);

		MimeBodyPart attch1 = new MimeBodyPart();		
		MimeBodyPart attch2 = new MimeBodyPart();		
		MimeBodyPart content = new MimeBodyPart();
		msgMultipart.addBodyPart(attch1);		
		msgMultipart.addBodyPart(attch2);		
		msgMultipart.addBodyPart(content);

		DataSource ds1 = new FileDataSource(
				"resource\\Java培训.txt"	
			);
		DataHandler dh1 = new DataHandler(ds1 );
		attch1.setDataHandler(dh1);
		attch1.setFileName(
				MimeUtility.encodeText("java培训.txt")
				);
		
		DataSource ds2 = new FileDataSource(
				"resource\\slogo.gif"		
			);
		DataHandler dh2 = new DataHandler(ds2 );
		attch2.setDataHandler(dh2);		
		attch2.setFileName("slogo.gif");
		
		MimeMultipart bodyMultipart = new MimeMultipart("related");
		content.setContent(bodyMultipart);
		MimeBodyPart htmlPart = new MimeBodyPart();		
		MimeBodyPart gifPart = new MimeBodyPart();		
		bodyMultipart.addBodyPart(htmlPart);
		bodyMultipart.addBodyPart(gifPart);		

		DataSource gifds = new FileDataSource(
				"resource\\logo.gif"	
			);
		DataHandler gifdh = new DataHandler(gifds);		
		gifPart.setDataHandler(gifdh);
		gifPart.setHeader("Content-Location", "http://www.itcast.cn/logo.gif");
		
		htmlPart.setContent("你们的Java培训真的是最牛的吗?大家都这么说,我想跟你们比试一下!这可是我自己用程序生成和发送的邮件哦!<img src='http://www.itcast.cn/logo.gif'>"
					, "text/html;charset=gbk");
		
		msg.saveChanges();
		
		OutputStream ips = new FileOutputStream("resource\\demo3.eml");
		msg.writeTo(ips);
		ips.close();
		
		
	}

}

 5:

package cn.itcast.javamail3.web.mail.servlets;

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 cn.itcast.javamail2.Demo2;

public class SendMailServlet extends HttpServlet {

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		try {
			//Demo2.main(new String[]{});
			
			Context initCtx = new InitialContext();
			Context envCtx = (Context) initCtx.lookup("java:comp/env");
			Session session = (Session) envCtx.lookup("mail/Dog");

			Message message = new MimeMessage(session);
			message.setFrom(new InternetAddress("itcast_test@sina.com"));
			InternetAddress to[] = new InternetAddress[1];
			to[0] = new InternetAddress("itcast_test@sina.com");
			message.setRecipients(Message.RecipientType.TO, to);
			message.setSubject("ha");
			message.setText("test");
			//Transport.send(message);
			Transport transport = session.getTransport();
			transport.connect("smtp.sina.com", "itcast_test", "123456");
			transport.sendMessage(message, to);
			transport.close();
			response.getWriter().print("ok!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace(response.getWriter());
		}
	}

}

 保存下来,以后需要用到的时候查一下。。。

分享到:
评论

相关推荐

    javaWeb传智播客网上书城项目源码(设计以及实现论文).zip

    javaWeb传智播客网上书城项目源码(设计以及实现论文).zipjavaWeb传智播客网上书城项目源码(设计以及实现论文).zipjavaWeb传智播客网上书城项目源码(设计以及实现论文).zipjavaWeb传智播客网上书城项目源码(设计以及...

    传智播客 springmvc 视频 源代码 文档

    在这个"传智播客 springmvc 视频 源代码 文档"的资源包中,我们可以深入学习SpringMVC的精髓。 首先,视频教程很可能是讲解SpringMVC的基本概念、配置和使用方法,包括: 1. **环境搭建**:介绍如何配置Java开发...

    javaWeb传智播客网上书城项目源码(设计以及实现论文)

    javaWebjavaWeb传智播客网上书城项目源码(设计以及实现论文)javaWeb传智播客网上书城项目源码(设计以及实现论文)javaWeb传智播客网上书城项目源码(设计以及实现论文)javaWeb传智播客网上书城项目源码(设计以及实现...

    基于javaWeb传智播客网上书城项目源码设计以及实现.zip

    基于javaWeb传智播客网上书城项目源码设计以及实现.zip基于javaWeb传智播客网上书城项目源码设计以及实现.zip基于javaWeb传智播客网上书城项目源码设计以及实现.zip基于javaWeb传智播客网上书城项目源码设计以及实现...

    传智播客_Andorid_JNI视频_day01视频

    通过"传智播客_Andorid_JNI视频_day01视频"的学习,你将了解到如何设置开发环境,编写和调用JNI方法,以及理解JNI在实际项目中的应用场景。掌握JNI不仅可以提升应用性能,还能帮助开发者充分利用已有的C/C++库,为...

    传智播客汤阳光OA项目静态页面、项目源码和上课资料

    【传智播客汤阳光OA项目】是一个针对企业办公自动化(Office Automation)的教育项目,由知名教育机构传智播客的资深讲师汤阳光主导。这个项目旨在教授学员如何构建一个完整的OA系统,涵盖前端静态页面设计、后端...

    某宝获得传智播客视频和源码

    【标题】:“某宝获得传智播客视频和源码” 【描述】:“某宝获得传智播客视频和源码 javaee javaweb 全部教学资料” 【标签】:“javaee” “javaweb” “传智播客” 在上述信息中,我们涉及到了几个重要的IT...

    javaWeb传智播客网上书城项目源码(设计以及实现LW).zip

    javaWeb传智播客网上书城项目源码(设计以及实现LW)javaWeb传智播客网上书城项目源码(设计以及实现LW)javaWeb传智播客网上书城项目源码(设计以及实现LW)javaWeb传智播客网上书城项目源码(设计以及实现LW)javaWeb传智...

    [HeyJava][传智播客]全文检索Lucene源码

    通过深入学习HeyJava和传智播客提供的Lucene源码教程,你将能掌握如何在实际项目中运用Lucene,创建高效、精准的全文搜索引擎。同时,了解源码也有助于定制化需求,优化性能,或者解决遇到的问题。

    传智播客.net视频种子全集

    【标题】"传智播客.net视频种子全集"涵盖了.NET技术栈的广泛知识,旨在为初学者和进阶者提供全面的编程学习资源。传智播客是一家知名的IT教育机构,他们的视频教程通常深入浅出,易于理解,且内容涵盖广泛。 【描述...

    基于javaSSM架构实现传智播客网上书城系统设计源码和论文文档分享

    同时,《传智播客网上图书商城》更容易开拓全球市场从而大大提高了效率,对推动《传智播客网上图书商城》未来的发展有着一定的实际意义。 鉴于互联网的优势以及对国内外相关网上书城网站现状的研究分析,我决定以...

    2018传智播客web前端开发 39 期视频教程(完整版)

    本套视频为传智2018web前端开发全套视频教程基础班+就业班,视频+源码+案例笔记,全套高清不加密~2018最新传智播客视频! 本教程是实战派课程!为传智最新web前端39期,挑战全网最全视频,没有之一.课程由教学视频+...

    传智播客邮件开发例子源码

    itcast javamail demo 传智播客邮件开发源码 java实现两种发邮件方式,1、java代码取的session;2、jndi配session。 讲解了邮件收发原理、邮件文件的讲解、实现了简单邮件和复杂邮件的发送

    巴巴运动网源码(传智播客)

    【巴巴运动网源码(传智播客)】是一套基于Java编程语言开发的网站源码,主要用于构建体育运动类的在线服务平台。这套源码在IT教育领域,特别是由传智播客这样的知名教育机构中被用作教学案例,帮助学员理解和实践...

    传智播客在线支付易宝支付源码 和 易宝支付产品(HTML版)通用接口文档3.0 3.0.pdf

    在传智播客提供的这个源码中,我们可以看到易宝支付的接入流程、参数处理、回调处理等核心功能的实现细节。这对于开发者来说是极其宝贵的,因为通过阅读源码,他们可以直接学习到如何与易宝支付API进行交互,从而在...

    (JavaWeb基于SSM框架的毕业设计)传智播客网上书城项目源码(设计以及实现论文).zip

    (JavaWeb基于SSM框架的毕业设计)传智播客网上书城项目源码(设计以及实现论文).zip(JavaWeb基于SSM框架的毕业设计)传智播客网上书城项目源码(设计以及实现论文).zip(JavaWeb基于SSM框架的毕业设计)传智播客网上书城...

Global site tag (gtag.js) - Google Analytics