信仰,追求

导航

James 邮件服务器安装,配置和Java Mail发送程序

 

  • 安装James mail

1)下载apache-james-3.0-beta3-app.tar.gz

2)解压:tar -zxf apache-james-3.0-beta3-app.tar.gz

 

  • 端口修改

服务监听端口不是在/etc/service,而是在james的conf下文件配置,

imapserver.xml:<bind>0.0.0.0:143</bind>

lmtpserver.xml:<bind>127.0.0.1:24</bind>

pop3server.xml:<bind>0.0.0.0:110</bind>

smtpserver.xml:<bind>0.0.0.0:25</bind>

  • 启动与停止

启动

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

<domainlist class="org.apache.james.domainlist.jpa.JPADomainList">
  <autodetect>true</autodetect>
  <autodetectIP>true</autodetectIP>
  <defaultDomain>asbinms.com</defaultDomain>
</domainlist>

  • 添加用户

需要在james启动后执行

1)cd bin

2)./james-cli.sh  -h localhost adduser admin@bellinms.com 123456

邮件必须是允许的域名范围内。

 

 

  • outlook接收邮件设置

image

image

 

 

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



==============================================================
INFO   | jvm 1    | 2013/04/27 20:26:42 | INFO  20:26:42,250 | james.smtpserver | ID=69002665 Connection established from 111.74.1.123 (111.74.1.123)
INFO   | jvm 1    | 2013/04/27 20:26:42 | INFO  20:26:42,954 | james.smtpserver | ID=2015966036 Connection established from 111.74.1.123 (111.74.1.123)
INFO   | jvm 1    | 2013/04/27 20:26:43 | INFO  20:26:43,616 | james.smtpserver | ID=2015966036 Successfully spooled mail Mail1367065603612-64544d5b-d063-42db-8d5e-9cd85ed1cffa from admin@liuyou.com on 111.74.1.123 for [you.liu@189.cn]
INFO   | jvm 1    | 2013/04/27 20:26:43 | INFO  20:26:43,699 | james.smtpserver | ID=2015966036 Connection closed for 111.74.1.123 (111.74.1.123)
INFO   | jvm 1    | 2013/04/27 20:26:43 | INFO  20:26:43,752 | james.mailetcontext | Attempting delivery of Mail1367065603612-64544d5b-d063-42db-8d5e-9cd85ed1cffa-to-189.cn to host mta-189.21cn.com. at 121.14.53.136 from admin@liuyou.com for addresses [you.liu@189.cn]
INFO   | jvm 1    | 2013/04/27 20:26:43 | INFO  20:26:43,772 | james.smtpserver | ID=69002665 Connection closed for 111.74.1.123 (111.74.1.123)
INFO   | jvm 1    | 2013/04/27 20:26:51 | INFO  20:26:51,601 | james.mailetcontext | Mail (Mail1367065603612-64544d5b-d063-42db-8d5e-9cd85ed1cffa-to-189.cn) sent successfully to mta-189.21cn.com. at 121.14.53.136 from admin@liuyou.com for [you.liu@189.cn]

注意:bin/wrapper.log,成功的发送邮件,必须出现红色部分的log日志。



============================================
1. 默认的邮件存储位置file://var/mail/spam,var是james安装目录的var;
2. 如果/var/spool/clientmqueue下有很多文件,是因为系统的sendmail服务停止运行,由crontab产生大量邮件堆积发送不出去导致.可以直接删掉.
3. 启动系统的sendmail命令:root启动,service sendmail start



posted on 2013-02-07 04:01  百事乐  阅读(8683)  评论(0编辑  收藏  举报