安装James mail
1)下载apache-james-3.0-beta3-app.tar.gz
2)解压:tar -zxf apache-james-3.0-beta3-app.tar.gz
启动与停止
启动
1)./james start
2)./run.sh &
停止
1)./james stop
2) kill run PID, 也许不用,杀之前检查是否有apache-james的进程
配置域名
需要在james启动后执行
1)cd bin
2)./james-cli.sh -h localhost adddomain bellinms.com
添加默认域名
vi conf/domainlist.xml
true
true
asbinms.com
添加用户
需要在james启动后执行
1)cd bin
2)./james-cli.sh -h localhost adduser admin@bellinms.com 123456
邮件必须是允许的域名范围内。
端口修改
服务监听端口不是在/etc/service,而是在james的conf下文件配置,
imapserver.xml:0.0.0.0:143
lmtpserver.xml:127.0.0.1:24
pop3server.xml:0.0.0.0:110
smtpserver.xml:0.0.0.0:25
outlook接收邮件设置
java mail邮件发送程序
package mail;
import java.util.Date;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
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 SendMail {
private String mailServer, mailPort,username, password;
private Session mailSession;
private Properties prop;
private Message message;
/**
* // 设置邮件服务器相关
*/
public SendMail(String mailServer, String mailPort, String username, String password) {
this.mailServer = mailServer;
this.mailPort = mailPort;
this.username = username;
this.password = password;
}
/**
*
* @param to : 18955175547@189.cn;
* @param mailSubject:
* @param mailContent
*/
public void send(String to, String mailSubject, String mailContent) {
EmailAuthenticator mailauth = new EmailAuthenticator(username, password);
// 设置邮件服务器
prop = System.getProperties();
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.host", mailServer);
prop.put("mail.smtp.port", mailPort);
// 产生新的Session服务
mailSession = mailSession.getDefaultInstance(prop, (Authenticator) mailauth);
message = new MimeMessage(mailSession);
try {
message.setFrom(new InternetAddress(username)); // 设置发件人
message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));// 设置收件人
message.setSubject(mailSubject);// 设置主题
// message.setContent(mailContent, "text/plain");// 设置内容,中文乱码,用下面的方法
// 设置内容
Multipart multipart = new MimeMultipart();
MimeBodyPart mimeBodyPart = new MimeBodyPart();
mimeBodyPart.setText(mailContent);
// mimeBodyPart.setFileName(MimeUtility.encodeText(mailContent, "GBK", "B")); //对邮件头采用base64方式编码
multipart.addBodyPart(mimeBodyPart);
message.setContent(multipart);
// 设置日期
message.setSentDate(new Date());
Transport tran = mailSession.getTransport("smtp");
tran.connect(mailServer, username, password);
tran.send(message, message.getAllRecipients());
tran.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
SendMail mail;
mail = new SendMail("222.74.xxx.xxx", "25", "liuyou@bellinms.com", "123456");
System.out.println("sending......");
mail.send("15375287539@189.cn", "Date:" + new Date().toLocaleString(), "eess1234");
System.out.println("finished!");
try{
Thread.sleep(5*1000L);
}
catch (Exception e) {
e.printStackTrace();
}
}
}