下载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();
}
}