启动 James 服务器
双击\james-2.3.1\bin 目录下的 run.bat文件,即可启动 James 服务器。
控制台显示如下:
Using PHOENIX_HOME: F:\项目\James\james-2.3.1rc1
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 ,那么他的邮件地址就是 [email protected] (^_^)修改结果如下:
- ……
- <postmaster>[email protected]</postmaster>
- ……
- <servernames autodetect="false" autodetectIP="false">
- <servername>tuping.com</servername>
- </servernames>
- ……
- ……
- <postmaster>[email protected]</postmaster>
- ……
- <servernames autodetect="false" autodetectIP="false">
- <servername>lixiaobo.com</servername>
- </servernames>
- ……
……
<postmaster>[email protected]</postmaster>
……
<servernames autodetect="false" autodetectIP="false">
<servername>tuping.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>
将其更改,结果如下:
<!-- update the configuration to include your own network/addresses. The matcher can be configured with a comma separated list of IP addresses,wildcarded IP subnets, and wildcarded hostname subnets. -->
<!-- e.g. "RemoteAddrNotInNetwork=127.0.0.1, abc.de.*, 192.168.0.*"-->
<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 = "smtp.163.com";
- String from = "[email protected]";
-
- String to = "[email protected]";
- final String username = "leo";
- final String password = "123";
-
-
- Properties props = System.getProperties();
-
-
- 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("I hava my own mail server");
- message.setText("From now, you have your own mail server, congratulation!");
-
-
- session.getTransport("smtp").send(message);
-
- 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 props = new Properties();
-
-
- Session session = Session.getDefaultInstance(props, new Authenticator(){
- @Override
- public PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
-
- });
-
-
- try {
-
- Store store = session.getStore("pop3");
- store.connect(host, username, password);
-
-
- 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();
-
- }
-
- }