[实践证明整理系列]CentOS 6中简单架设Apache James邮件服务器
参考
#unzip apache-james-2.3.2.zip -d /home/aclyyx/
#chmod 777 *.sh
#./run.sh
Using PHOENIX_HOME: /home/aclyyx/james-2.3.2 Using PHOENIX_TMPDIR: /home/aclyyx/james-2.3.2/temp Using JAVA_HOME: /usr/java/jdk1.7.0 Running Phoenix: Phoenix 4.2 James Mail Server 2.3.2 Remote Manager Service started plain:4555 POP3 Service started plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Disabled
#netstat -pan|grep 25
…… <postmaster>Postmaster@localhost</postmaster> …… <servernames autodetect="true" autodetectIP="true"> <servername>aclyyx.com</servername> </servernames> ……
把localhost该成你自己想要的邮箱域名,把自动探测IP属性设置为“false”这里假设改成zhaipuhong.com如果开了一个帐号zph,那么他的邮件地址就是[email protected](^_^)修改结果如下:
…… <postmaster>[email protected]</postmaster> …… <servernames autodetect="false" autodetectIP="false"> <servername>zhaipuhong.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-->
找到下面元素,去掉其注释
<authRequired>true</authRequired>
这样邮箱访问需要帐号验证,你不希望别人用你的帐号收发消息吧……^_^
如此,James服务配置已经完成。#vi /etc/sysconfig/iptables
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport110-jACCEPT
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport26-jACCEPT
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport4555-jACCEPT
-AINPUT-mstate--stateNEW-mtcp-ptcp--dport119-jACCEPT
保存和关闭文件,重启防火墙#service iptables restart
#./run.sh
#telnet 127.0.0.1 4555
listusersdisplayexistingaccounts显示现有账户
countusersdisplaythenumberofexistingaccounts显示现有账户数量
adduser[username][password]addanewuser添加一个新用户
verify[username]verifyifspecifieduserexist核实指定用户是否存在
deluser[username]deleteexistinguser删除存在的用户
setpassword[username][password]setsauser'spassword设置用户密码
setalias[user][alias]locallyforwardsallemailfor'user'to'alias'设置邮箱别名
showalias[username]showsauser'scurrentemailalias显示别名
unsetalias[user]unsetsanaliasfor'user'删除用户别名
setforwarding[username][emailaddress]forwardsauser'semailtoanotheremailaddress转发用户的电子邮件地址到另一个电子邮件地址
showforwarding[username]showsauser'scurrentemailforwarding显示用户的当前邮件转发
unsetforwarding[username]emovesaforward删除转发
user[repositoryname]changetoanotheruserrepository改变另一个用户库
shutdownkillsthecurrentJVM(convenientwhenJamesisrunasadaemon)关闭当前的服务
quit close connection 关闭连接#adduser acl1 111111 #adduser acl2 222222
package mail; 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.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Store; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeBodyPart; import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMultipart; import javax.mail.internet.MimeUtility; public class HelloJMail { //发送邮件 public static void sendMail() { //String host = "192.168.1.98"; // 指定的smtp服务器,本机的局域网IP String host = "192.168.11.200"; // 本机smtp服务器 String from = "[email protected]"; // 邮件发送人的邮件地址 String to = "[email protected]"; // 邮件接收人的邮件地址 final String username = "acl1"; //发件人的邮件帐户 final String password = "111111"; //发件人的邮件密码 // 创建Properties 对象 Properties props = System.getProperties(); // 添加smtp服务器属性 props.put("mail.smtp.host", host); props.put("mail.smtp.port", "26"); 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("我有自己的邮件服务器了")); Multipart mp = new MimeMultipart(); MimeBodyPart mbp = new MimeBodyPart(); mbp.setContent("rt,我有自己的邮件服务器了", "text/html;charset=gb2312"); mp.addBodyPart(mbp); //添加附件 // mbp=new MimeBodyPart(); // String filename= "G:\\hi.txt"; //选择出每一个附件名 // FileDataSource fds = new FileDataSource(filename); //得到数据源 // mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart // mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart // mp.addBodyPart(mbp); message.setContent(mp); // 发送消息 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 = "192.168.11.200"; final String username = "acl2"; final String password = "111111"; // 创建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(); } }
原本此处有一节是《结合MySQL数据库管理邮件用户》,2011-09-26 10:45删除,专门写续篇描述