现在的位置: 首页 > 综合 > 正文

Javamail操作指南(一)

2013年08月23日 ⁄ 综合 ⁄ 共 4420字 ⁄ 字号 评论关闭
Bromon原创 请尊重版权

 怎样才算比较完整的Javamail操作指南?我想应该包括绝大多数基本的email操作,能够应付一般的应用。在本指南中打算囊括以下内容:

 ● 发送email:包括文本邮件、HTML邮件、带附件的邮件、SMTP验证
 ● 接收email:pop3远程连接、收取不同MIME的邮件、处理附件

 我想有了上述功能的介绍,应该可以应付很多email的相关应用了。所以请允许我给本文拟了一个比较狂妄的名字,这样才能保证收视率,。还是那句话,写这个post的原因就是没有在网上看到更全面的,你看过的话记得要告诉我。

 下面的所有例子都经过实际测试,你可以说它写得不够OO,不够plugable,但是它的确是可以参考的。自从有了javamail,发垃圾邮件就方便多了。本文代码多说明少,这倒不是我偷懒,而是很多东西都涉及pop3等协议的规范,如果不了解这些规范的话,由的东西我实在不知道怎么跟你解释;如果了解的话,那我基本上就不用再解释。所以本着实用的原则就省略了,由兴趣的话自己去翻翻协议规范。

 废话少说,首先需要配置环境。需要的包是mail.jar和activation.jar。高版本的J2SDK EE自带。地址嘛,再java.sun.com上搜索一下,很容易找到。放到classpath中就KO。

 一、 邮件的发送

 下面了弄个发邮件的Hello World,热热身:

 /*************
 Name:TextMailSender.java
 Author:Bromon
 Version:1.0
 Date:2004-4-26
 Note:发送email到bromon@163.com,需要安装SMTP服务器
 *************/
 package org.bromon.mail;
 import javax.mail.*;
 import javax.mail.internet.*;
 import java.util.*;
 public class TextMailSender
 {
 public static void main(String args[])
 {
  try
  {
   Properties prop=new Properties();
   //指定要使用的SMTP服务器为bromon2k
   prop.put("mail.smtp.host","bromon2k");
   Session mailSession=Session.getDefaultInstance(prop);

   //发件人地址
   InternetAddress from=new InternetAddress("bromon@bromon2k");
   //收件人地址
   InternetAddress to=new InternetAddress("bromon@163.com");
   
   MimeMessage msg=new MimeMessage(mailSession);
   msg.setFrom(from);
   msg.addRecipient(javax.mail.Message.RecipientType.TO,to);
   //发信日期
   msg.setSentDate(new java.util.Date());
   //title
   msg.setSubject("你好");
   //邮件正文
   msg.setText("hello,bromon");
   Transport.send(msg);
  }catch(Exception e)
  {
   System.out.println(e);
  }
 }
 } 

 程序很简单,但是它是不能运行的(倒)。除非你的机器上安装了一个SMTP服务器,而且你的机器还叫做bromon2k。写这么一段不能执行的程序不是为了找打,而是让各位对javamail有个基本印象,我就懒得改了。下面演示的是如何通过163、sohu等email服务商提供的免费邮箱来发邮件,基本操作和上面的一样,只是多一个SMTP验证而已:

 /*
 * Created on 2004-4-26
 */
 package org.bromon.mail;
 import javax.mail.*;
 import java.util.*;
 import javax.mail.internet.*;

 /**
 * @author Bromon
 */
 public class SenderWithSMTPVer
 {
 String host="";
 String user="";
 String password="";

 public void setHost(String host)
 {
  this.host=host;
 }

 public void setAccount(String user,String password)
 {
  this.user=user;
  this.password=password;
 }

 public void send(String from,String to,String subject,String content)
 {
  Properties props = new Properties();
  props.put("mail.smtp.host", host);//指定SMTP服务器
  props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
  try
  {
   Session mailSession = Session.getDefaultInstance(props);
   
   mailSession.setDebug(true);//是否在控制台显示debug信息
   
   Message message=new MimeMessage(mailSession);
   message.setFrom(new InternetAddress(from));//发件人
   message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));//收件人
   
   message.setSubject(subject);//邮件主题
   message.setText(content);//邮件内容
   message.saveChanges();
   
   Transport transport = mailSession.getTransport("smtp");
   transport.connect(host, user, password);
   transport.sendMessage(message, message.getAllRecipients());
   transport.close();
  }catch(Exception e)
  {
   System.out.println(e);
  }
  
 }

 public static void main(String args[])
 {
  SenderWithSMTPVer sm=new SenderWithSMTPVer();

  sm.setHost("smtp.163.com");//指定要使用的邮件服务器
  sm.setAccount("abc","123");//指定帐号和密码

  /*
 * @param String 发件人的地址
   * @param String 收件人地址
   * @param String 邮件标题
   * @param String 邮件正文
  */
  sm.send("abc@163.com","bromon@163.com","标题","内容");
 }

 } 

 这段程序好像也不需要解释了吧,把SMTP地址、帐号、密码等配置信息写到Properties里面,Java里面很多API都需要这么干,比如再程序中加入对代理服务器的支持等。

 上面的程序修改一下服务器地址、帐号、密码就可以使用,非常简单。

 如何发送一个HTML格式的Email呢?也很简单,再邮件正文中写入HTML代码,然后指定邮件的ContentType就OK,下面只给出关键代码:

 ………..
 MimeMessage msg=new MimeMessage(mailSession);
 msg.setContent(content,"text/html");
 msg.setText(“<html><body><h1>下面的,你们好吗?</body></html>”);
 ………..

 下面是发送带有附件的email,稍微复杂一点,而且和前面的程序有一些不同,请仔细一点,同时需要一点IO的知识。相同的代码就不在列出,只写关键部分,谁都想偷懒不是?

 import javax.mail.*;
 import javax.mail.internet.*;
 import javax.activation.*;
 import java.util.*;
 ……….
 MimeMessage msg=new MimeMessage(mailSession);
 msg.setSentDate(new Date());
 msg.setSubject("hello");

 MimeBodyPart textBodyPart=new MimeBodyPart();
 textBodyPart.setText(“邮件正文”);

 MimeBodyPart fileBodyPart=new MimeBodyPart();
 FileDataSource fds=new FileDataSource("GIS.rar");//要发送的附件
 fileBodyPart.setDataHandler(new DataHandler(fds));
 fileBodyPart.setFileName(fds.getName());
 Multipart container=new MimeMultipart();
 container.addBodyPart(textBodyPart);
 container.addBodyPart(fileBodyPart);
 msg.setContent(container);
 Transport.send(msg);
 ………… 

 这里的msg由两个MimeBodyPart构成,这个东西解释起来基本上比较难,如果不了解相关的规范就不太好解释,如果了解的话,我就不用解释了,这个这个………唉。

抱歉!评论已关闭.