`
dreamoftch
  • 浏览: 498133 次
  • 性别: 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());
		}
	}

}

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

分享到:
评论

相关推荐

    传智播客Android视频教程全套源码

    传智播客Android视频教程全套源码

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

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

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

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

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

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

    传智播客android全套源码.zip

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

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

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

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

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

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

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

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

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

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

    javaWeb传智播客网上书城项目源码(设计以及实现论文) 毕业设计、项目源码、管理系统、包含视频教程和源码以及数据库运行文件 javaWeb传智播客网上书城项目源码(设计以及实现论文) 毕业设计、项目源码、管理系统、...

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

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

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

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

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

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

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

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

    传智播客hibernate源码

    标题"传智播客hibernate源码"暗示了这是一个关于Hibernate框架的源代码学习资源,可能包含了对Hibernate框架内部机制的深入解析,以及如何在实际项目中应用Hibernate的相关示例。 描述中的内容重复,进一步确认了这...

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

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

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

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

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

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

Global site tag (gtag.js) - Google Analytics