从
http://james.apache.org/下载最新的james服务器
修改james-2.3.1\bin\run.bat文件,设置java_home环境变量
运行run.bat启动mail服务器
用下列命令可以注册为windows服务:james-2.3.1\bin\Wrapper -i james-2.3.1\conf\wrapper.conf
用telnet 登录4555端口管理jamesmail用户
用下面两个类测试:
import java.util.Date;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public String sendMail(String from, String to, String userName,
String password, String subject, String body) {
Properties props = System.getProperties();
// 设置SMTP邮件服务器:
props.put("mail.smtp.host", "localhost");
// SMTP服务器需要验证:
props.put("mail.smtp.auth", "true");
// 传入用户名和口令:
Session session = Session.getDefaultInstance(props,
new PasswordAuthenticator(userName, password));
// 创建新邮件:
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(from));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(
to, false));
msg.setSubject(subject);
msg.setText(body);
msg.setSentDate(new Date());
} catch (Exception e1) {
e1.printStackTrace();
return "fail";
}
// 发送:
try {
Transport.send(msg);
return "success";
} catch (Exception e) {
return "fail";
}
}
/** *//**
* @param args
*/
@SuppressWarnings("static-access")
public static void main(String[] args) throws Exception{
String from = "test@123.com";
String to = "wang.ming@excelerate-tech.com";
String subject = new String("Test mail 中文".getBytes(),"utf-8");
String body = new String("Test mail 中文".getBytes(),"utf-8");
String userName = "smgxt";
String password = "smgxt";
SendMail sendMail = new SendMail();
String result = sendMail.sendMail(from, to, userName, password, subject, body);
if("success".equals(result)){
System.out.println("Success!");
}
}
}
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class PasswordAuthenticator extends Authenticator {
private String username;
private String password;
public PasswordAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}