今天嘗試使用james,嘗試了幾個版本特別是apache-james-2.3.2.1,可是apache-james-2.3.2.1始終啓動不了,啓動老是報異常,換到apache-james-2.3.2的時候,正常啓動。點擊run.bat啓動 如圖: 若是須要關閉ctrl+cjava
A.啓動完成後,進入E:\java\james-2.3.2\apps\james\SAR-INF\config.xml作配置文件修改 1. <postmaster>Postmaster@localhost</postmaster> <servernames autodetect="true" autodetectIP="true">
<servername>localhost</servername>
</servernames> 修改成:
<postmaster>Postmaster@xxxx(任意名字)</postmaster> <servernames autodetect="false" autodetectIP="false">
<servername>xxxx</servername>
</servernames>apache
2.<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> --> 3.去掉<authRequired>true</authRequired>註釋
修改完畢服務器
B.cmd進入命令行,輸入 telnet localhsot 4555 進入James管理器,進入後顯示 JAMES Remote Administration Tool 2.3.1 Please enter your login and password Login id: login id爲root,密碼也是root C.建立新用戶 建立新用戶的命令是:adduser username password 例如 adduser hj hj 帳號建立成功後,使用quit退出管理器。如今咱們能夠開始部署咱們的JMail應用了。session
發送郵件測試代碼: ``` package com.xmaven.mail;app
import java.io.IOException; import java.util.Properties;maven
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.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeUtility;ide
public class HelloJMail {post
//發送郵件 public static void sendMail() { //String host = "192.168.1.98"; // 指定的smtp服務器,本機的局域網IP String host = "localhost"; // 本機smtp服務器 //String host = "smtp.163.com"; // 163的smtp服務器 String from = "hj@basjans.com"; // 郵件發送人的郵件地址 String to = "pro_jerry@163.com"; // 郵件接收人的郵件地址 final String username = "hj"; //發件人的郵件賬戶 final String password = "hj"; //發件人的郵件密碼 // 建立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 = "zph"; final String password = "zph"; // 建立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(); }
}測試
但願對同志們有所幫助,必定要選對版本!!!