`
dreamoftch
  • 浏览: 496009 次
  • 性别: 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传智播客网上书城项目源码(设计以及...

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

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

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

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

    传智播客扫地僧视频讲义源码

    08_C动态库升级成框架案例_方法1动态库中直接添加回调函数_传智扫地僧 09_C动态库升级成框架案例_方法2把回调函数缓存到动态库_编写 10_C动态库升级成框架案例_方法2把回调函数混存到动态库_测试 11_C++基础课程day...

    15年9月传智播客视频struts2

    根据提供的文件信息,我们可以推断出这是一套关于Struts2框架的教学视频资料,由知名的教育机构传智播客在2015年9月发布。下面将对Struts2框架进行详细介绍,并基于该视频资料可能涵盖的核心知识点进行展开。 ### ...

    传智播客_Andorid_JNI视频_day01视频

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

    传智播客OA源码

    首先,OA系统的核心功能包括工作流管理、文档管理、人事管理、资产管理、项目管理等多个模块,这些在传智播客的视频教程中可能有详尽的讲解。源码中的实现可以帮助我们掌握如何在实际项目中搭建并集成这些功能。 ...

    传智播客俄罗斯方块游戏源码

    【标题】"传智播客俄罗斯方块游戏源码"是一个基于Java编程语言实现的经典游戏——俄罗斯方块的代码资源。这个项目可能是传智播客教育机构为了教学目的设计的,旨在帮助学员深入理解游戏开发的基本原理和Java编程技术...

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

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

    传智播客\Ajax\传智播客ajax课堂源码

    8. **jQuery与Ajax**:如果源码中涉及jQuery,会学习到如何使用jQuery简化Ajax操作,如$.ajax()、$.get()、$.post()等函数。 9. **Promise与Async/Await**:现代JavaScript中的异步编程模式,如Promise和async/...

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

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

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

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

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

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

    传智播客SpringMvc完整视频+完整笔记+源码+相关资源

    传智播客SpringMvc完整视频+完整笔记+源码+相关资源。

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

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

    传智播客Android视频教程-课程源码

    通过学习和分析这个【传智播客Android视频教程-课程源码】,开发者不仅可以掌握基本的Android视频播放器开发,还能深入理解Android的网络编程、UI设计、多媒体处理等多个重要领域,从而提升自身在Android开发中的...

    传智播客android全套源码.zip

    【传智播客Android全套源码】是一份包含大量Android应用开发源代码的学习资源,源自知名教育机构传智播客的教学课程。这份压缩包旨在帮助开发者深入理解Android应用的内部工作机制,提供了一个全面的实践平台,让...

Global site tag (gtag.js) - Google Analytics