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("[email protected]")); Transport transport = session.getTransport(); transport.connect("smtp.sina.com", 25, "itcast_test", "123456"); transport.sendMessage(msg, new Address[]{new InternetAddress("[email protected]")}); //transport.send(msg,new Address[]{new InternetAddress("[email protected]")}); 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("[email protected]")); msg.setSubject("中文主题"); msg.setRecipients(RecipientType.TO, InternetAddress.parse("[email protected],[email protected]")); 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("[email protected]")); } }
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("传智播客") + "\" <[email protected]>")); msg.setSubject("你们的Java培训真的是最牛的吗?"); msg.setReplyTo(new Address[]{new InternetAddress("[email protected]")}); msg.setRecipients(RecipientType.TO,InternetAddress.parse(MimeUtility.encodeText("黎活明") + " <[email protected]>," + MimeUtility.encodeText("张孝祥") + " <[email protected]>")); 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("[email protected]")); InternetAddress to[] = new InternetAddress[1]; to[0] = new InternetAddress("[email protected]"); 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()); } } }
保存下来,以后需要用到的时候查一下。。。