james邮件服务器下的邮件发送

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

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值