下載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 = "[email protected]";
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("[email protected]"),
new InternetAddress("[email protected]")
});
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();
}
}