Apache James 简称 James, 是 Java Apache Mail Enterprise Server的缩写。James 是100%基于Java的电子邮件服务器。它是一种独立的邮件服务器,并提供了一个完整的电子邮件解决方案,用来收、发电子邮件。
James2.3.1下载(文后提供下载)
javamail-1_4_1.zip(文后提供下载)
启动 James 服务器
双击\james-2.3.1\bin 目录下的 run.bat文件,即可启动 James 服务器。
控制台显示如下:
Using PHOENIX_HOME: F:\项目\James\james-2.3.1rc1
Using PHOENIX_TMPDIR: F:\项目\James\james-2.3.1rc1\temp
Using JAVA_HOME: C:\Program Files\Java\jdk1.6.0_03
Phoenix 4.2
James Mail Server 2.3.1rc1
Remote Manager Service started plain:4555
POP3 Service started plain:110
SMTP Service started plain:25
NNTP Service started plain:119
FetchMail Disabled
启动成功。关闭Ctrl + C
说明:启动前请确保您的JDK环境变量如JAVA_HOME等已经设置好;James 启动时,其SMTP 服务默认在 25 端口启动,POP3 服务默认在 110 端口启动, NNTP 服务默认在 119 端口启动, 请确保这些端口未被占用。
服务配置:
打开F:\项目\James\james-2.3.1rc1\apps\james\SAR-INF 下的 config.xml 文件,初次启动James之前,不会有这个文件,只有当James服务启动一次之后才自动构件该文件。
需要修改的地方:
- ……
- <postmaster>Postmaster@localhost</postmaster>
- ……
- <servernames autodetect="true" autodetectIP="true">
- <servername>localhost</servername>
- </servernames>
- ……
……
<postmaster>Postmaster@localhost</postmaster>
……
<servernames autodetect="true" autodetectIP="true">
<servername>localhost</servername>
</servernames>
……
把localhost该成你自己想要的邮箱域名, 把自动探测IP属性设置为“false”这里假设改成 lixiaobo.com 如果开了一个帐号 leo ,那么他的邮件地址就是 leo@lixiaobo.com (^_^)修改结果如下:
- ……
- <postmaster>Postmaster@lixiaobo.com</postmaster>
- ……
- <servernames autodetect="false" autodetectIP="false">
- <servername>lixiaobo.com</servername>
- </servernames>
- ……
…… <postmaster>Postmaster@lixiaobo.com</postmaster> …… <servernames autodetect="false" autodetectIP="false"> <servername>lixiaobo.com</servername> </servernames> ……
找到
- <mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor">
- <processor> relay-denied </processor>
- <notice>550 - Requested action not taken: relaying denied</notice>
- </mailet>
<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet>
将其注释,结果如下:
- <!--mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor">
- <processor> relay-denied </processor>
- <notice>550 - Requested action not taken: relaying denied</notice>
- </mailet-->
<!--mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet-->
找到下面元素,去掉其注释
- <authRequired>true</authRequired>
<authRequired>true</authRequired>
这样邮箱访问需要帐号验证,你不希望别人用你的帐号收发消息吧……^_^
如此,James服务配置已经完成。
6。创建邮件帐号
创建邮件帐号后,就可以用来收发邮件了。James的账号管理是通过基于Telnet客户机的远程管理器,这点颇为不爽,尤其是我的操作系统下的命令行控制台是不显示telnet命令输入字符的,经常出错。
现在进入命令行控制台,在telnet localhsot 4555 进入James管理器,操作如下:
- C:\Documents and Settings>telnet localhost 4555
C:\Documents and Settings>telnet localhost 4555
将进入
- JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id:
JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id:
默认的登陆id 为root 密码也为 root ,登陆成功后结果如下:
- JAMES Remote Administration Tool 2.3.1
- Please enter your login and password
- Login id:
- Password:
- Welcome root. HELP for a list of commands
JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id: Password: Welcome root. HELP for a list of commands
创建新用户的命令是:adduser username password
这里创建了两个账户来作为演示使用: leo/123,crb/123
代码示例:
import java.io.IOException;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;
public class HelloJMail {
//发送邮件
public static void sendMail() {
//String host = "192.168.10.191"; // 指定的smtp服务器,本机的局域网IP
// String host = "localhost"; // 本机smtp服务器
String host = "smtp.163.com"; // 163的smtp服务器
String from = "leo@lixiaobo.com"; // 邮件发送人的邮件地址
// String to = "crb@lixiaobo.com"; // 内网邮件接收人的邮件地址
String to = "lixiaobo618@163.com"; // 外网邮件接收人的邮件地址
final String username = "leo"; //发件人的邮件帐户
final String password = "123"; //发件人的邮件密码
// 创建Properties 对象
Properties props = System.getProperties();
// 添加smtp服务器属性
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
// 创建邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 定义邮件信息
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(
to));
//message.setSubject(transferChinese("我有自己的邮件服务器了"));
message.setSubject("I hava my own mail server");
message.setText("From now, you have your own mail server, congratulation!");
// 发送消息
session.getTransport("smtp").send(message);
//Transport.send(message); //也可以这样创建Transport对象发送
System.out.println("SendMail Process Over!");
} catch (MessagingException e) {
e.printStackTrace();
}
}
//接受邮件
public static void getMail(){
String host = "localhost";
final String username = "crb";
final String password = "123";
// 创建Properties 对象
Properties props = new Properties();
// 创建邮件会话
Session session = Session.getDefaultInstance(props, new Authenticator(){
@Override
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
try {
// 获取邮箱的pop3存储
Store store = session.getStore("pop3");
store.connect(host, username, password);
// 获取inbox文件
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY); //打开,打开后才能读取邮件信息
// 获取邮件消息
Message message[] = folder.getMessages();
for (int i=0, n=message.length; i<n; i++) {
System.out.println(i + ": " + message[i].getFrom()[0]
+ "\t" + message[i].getSubject());
try {
message[i].writeTo(System.out);
} catch (IOException e) {
e.printStackTrace();
}
}
// 关闭资源
folder.close(false);
store.close();
} catch (MessagingException e) {
e.printStackTrace();
}
System.out.println("GetMail Process Over!");
}
//邮件主题中文字符转换
public static String transferChinese(String strText){
try{
strText = MimeUtility.encodeText(new String(strText.getBytes(), "GB2312"), "GB2312", "B");
}catch(Exception ex){
ex.printStackTrace();
}
return strText;
}
public static void main(String[] args) {
HelloJMail.sendMail();
// HelloJMail.getMail();
}
}