James收发邮件
2013-11-12 17:05
543 查看
下载James
去apache的官网,找到James,下载。这里用的是2.3.2版本。启动James
将下载好的James解压,如下图:双击bin目录下的run.bat
已启动
修改配置文件
打开\apps\james\SAR-INF\config.xml建议先大致读一遍这个配置文件,大概了解一下都配置了什么东东。
修改
<postmaster>Postmaster@localhost</postmaster>
把localhost改为所需的域名如:chen.cn
修改
<servername>localhost</servername>
把localhost改为chen.cn
修改
<servernames autodetect="true" autodetectIP="true">
true都改为false
找到:
<mailet match="RemoteAddrNotInNetwork=127.0.0.1" class="ToProcessor"> <processor> relay-denied </processor> <notice>550 - Requested action not taken: relaying denied</notice> </mailet>
把它注释掉
创建用户
用telnet连接到james的管理界面telnet localhsot 4555
id和password都是root
敲help就知道怎么添加用户了,这里添加两个用户:
adduser chen chen
adduser uchen uchen
用javaMail收发邮件
代码:import java.io.IOException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Properties; import javax.mail.Address; 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; public class JamesJavaMail { public static void sendMail() throws Exception { String host = "localhost"; final String from = "chen@chen.cn"; final String password = "chen"; Properties properties = new Properties(); properties.setProperty("mail.smtp.host", host); properties.setProperty("mail.smtp.auth", "true"); properties.setProperty("mail.transport.protocol", "smtp"); Session session = Session.getDefaultInstance(properties, new Authenticator() { @Override public PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(from, password); } }); session.setDebug(true); Message msg = new MimeMessage(session); msg = new MimeMessage(session); msg.setFrom(new InternetAddress(from)); msg.setSubject("test"); msg.setText("I'm from " + from + ",just test java mail!" + new SimpleDateFormat("yyyy-MM-dd- HH:mm:ss") .format(new Date())); Transport transport = session.getTransport(); // 端口为25 transport.connect(host, 25, from, "chen"); transport.sendMessage(msg, new Address[] { new InternetAddress("623799957@qq.com"), new InternetAddress("uchen@chen.cn") }); transport.close(); System.out.println("发好了!"); } public static void receiveMail() throws Exception { String host = "localhost"; final String username = "uchen"; final String password = "uchen"; Properties properties = new Properties(); properties.setProperty("mail.pop3.host", "localhost"); properties.setProperty("mail.pop3.auth", "true"); properties.setProperty("mail.transport.protocol", "pop3"); Session session = Session.getDefaultInstance(properties, 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(); int n = message.length; System.out.println("一共有 " + n + " 封"); if (n > 0) { for (Message m : message) { System.out.println(m.getFrom()[0] + "-----------" + m.getSubject()); try { m.writeTo(System.out); } catch (IOException e) { e.printStackTrace(); } } } folder.close(false); store.close(); } catch (MessagingException e) { e.printStackTrace(); } System.out.println("收好了!!"); } public static void main(String[] args) throws Exception { sendMail(); // receiveMail(); } }
相关文章推荐
- james 收发外网邮件
- 局域网中搭建James服务器并实现与外网收发邮件
- 局域网中搭建James服务器并实现与外网收发邮件
- Apache James 收发内外网邮件的配置
- 用 Apache James 搭建邮件服务器来收发邮件实践
- 用 Apache James 搭建邮件服务器来收发邮件实践
- 局域网中搭建James服务器并实现与外网收发邮件
- 用Apache James搭建邮件服务器收发邮件
- 用 Apache James 搭建邮件服务器来收发邮件实践(一)(转)
- 用 Apache James 搭建邮件服务器来收发邮件实践
- 用 Apache James 搭建邮件服务器来收发邮件实践
- 《pro Spring》学习笔记之使用Spring+James邮件服务器发送普通文本邮件
- Delphi中,indy控件实现收发邮件的几点学习记录( 可以考虑加入多线程,用多个邮箱做一个邮箱群发器) 转
- James+Javamail构建邮件服务(五)
- 关于.NET邮件的收发问题总结
- Linux-mail的邮件收发及察看
- 一个基于qt的桌面聊天系统的学习笔记(1)-邮件模块(2)收发信处理流程
- exchange 2013限制收发邮件
- James发送外网邮件配置
- 设置 OutLook 2007 帐号收发邮件及修改默认保存邮件位置