1. 环境搭建
下载JDK
下载james 到apache网站下载:http://apache.freelamp.com/james/server/apache-james-2.3.2.zip
下载javamail相关jar包
2. 启动james
进入james安装目录:C:\james-2.3.2\bin
运行run.bat
默认我们不更改C:\james-2.3.2\apps\james\SAR-INF 下的config.xml
这样邮件服务器的域名默认为localhost
3. 建立用户
telnet localhost 4555
用roor/root登入
执行
adduser VerRanLiu 123456
adduser dove 123456
这样我们建立的两个用户邮箱地址为:VerRanLiu@locahost dove@localhost
4. 编写发送邮件客户端类
让VerRanLiu 给dove 发送一封邮件
package com.spring.mail; import javax.mail.*; import java.util.*; import javax.mail.internet.*; public class MyFirstMail { protected Session mailSession; public MyFirstMail() throws Exception { init(); } public static void main(String[] args) { try { new MyFirstMail().sendMail(); System.out.print("邮件已发"); } catch (Exception e) { e.printStackTrace(); } } // 初始化服务器环境 public void init() throws Exception { Properties props = new Properties(); props.put("mail.transport.protocol", "smtp"); props.put("mail.smtp.host", "localhost"); props.put("mail.smtp.port", "25"); mailSession = Session.getDefaultInstance(props, null); ; } public void sendMail() throws Exception { try { Message msg = new MimeMessage(mailSession); // 从哪里发的邮件 msg.setFrom(new InternetAddress("VerRanLiu@localhost")); // 发送到目标邮件 // msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse("wang@localhost")); // 抄送的接收者 // msg.setRecipients(Message.RecipientType.CC,InternetAddress.parse("wang@localhost")); // 暗送的接收者 msg.setRecipients(Message.RecipientType.BCC, InternetAddress .parse("dove@localhost")); // 设置发送时间 msg.setSentDate(new java.util.Date()); // 设置邮件标题 msg.setSubject("a test mail"); // 设置邮件内容 msg.setText("this is the email content"); // 指定协议发送消息的对像 Transport transport = mailSession.getTransport("smtp"); // 发送消息 Transport.send(msg); } catch (Exception e) { throw e; } } }
5. 查看发送的邮件信息
C:\james-2.3.2\apps\james\var\mail\inboxes\dove
查看 4D61696C313238303330373733313339302D31.Repository.FileStreamStore 文件
Return-Path: <VerRanLiu@localhost> Delivered-To: dove@localhost Received: from localhost ([127.0.0.1]) by 1c6b13dd5c124b1 (JAMES SMTP Server 2.3.2) with SMTP ID 1010 for <dove@localhost>; Wed, 28 Jul 2010 17:02:11 +0800 (CST) Date: Wed, 28 Jul 2010 17:02:10 +0800 (CST) From: VerRanLiu@localhost Message-ID: <11850709.0.1280307730828.JavaMail.Administrator@1c6b13dd5c124b1> Subject: a test mail MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit this is the email content