启动 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>
……
把localhost该成你自己想要的邮箱域名, 把自动探测IP属性设置为“false”这里假设改成 tuping.com 如果开了一个帐号 leo ,那么他的邮件地址就是 leo@tuping.com (^_^)修改结果如下:
- ……
- <postmaster>Postmaster@tuping.com</postmaster>
- ……
- <servernames autodetect="false" autodetectIP="false">
- <servername>tuping.com</servername>
- </servernames>
- ……
- ……
- <postmaster>Postmaster@lixiaobo.com</postmaster>
- ……
- <servernames autodetect="false" autodetectIP="false">
- <servername>lixiaobo.com</servername>
- </servernames>
- ……
…… <postmaster>Postmaster@tuping.com</postmaster> …… <servernames autodetect="false" autodetectIP="false"> <servername>tuping.com</servername> </servernames> ……
找到
- <processor> relay-denied </processor>
- <notice>550 - Requested action not taken: relaying denied</notice>
- </mailet>
将其更改,结果如下:
<mailet match="RemoteAddrNotInNetwork=127.0.0.1,192.168.61.135" class="ToProcessor">
<processor> relay-denied </processor>
<notice>550 - Requested action not taken: relaying denied</notice>
</mailet-->
在原来的127.0.0.1后面加上本机的IP地址
找到下面元素,去掉其注释
- <authRequired>true</authRequired>
- <authRequired>true</authRequired>
<authRequired>true</authRequired>
这样邮箱访问需要帐号验证,你不希望别人用你的帐号收发消息吧……^_^
如此,James服务配置已经完成。
找到:
<!-- Check for delivery from a known spam server -->
<!-- This set of matchers/mailets redirect all emails from known -->
<!-- black holes, open relays, and spam servers to the spam processor -->
<!-- For this set to function properly, the spam processor must be configured.
该段代码是我注释掉的
<mailet match="InSpammerBlacklist=dnsbl.njabl.org."
class="ToProcessor">
<processor> spam </processor>
<notice>550 Requested action not taken: rejected - see http://njabl.org/ </notice>
</mailet>
这段代码表示dnsbl.njabl.org组织将会检查本机地址,如果包括在黑名单中,那么该邮件服务器将邮件作为垃圾邮件处理,我就是在这卡了半天。注释掉该段代码就OK了
6。创建邮件帐号
创建邮件帐号后,就可以用来收发邮件了。James的账号管理是通过基于Telnet客户机的远程管理器,这点颇为不爽,尤其是我的操作系统下的命令行控制台是不显示telnet命令输入字符的,经常出错。
现在进入命令行控制台,在telnet localhsot 4555 进入James管理器,操作如下:
- C:\Documents and Settings>telnet localhost 4555
- 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:
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();
- }
- }