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: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

  • 启动与停止

启动

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 [email protected] 123456

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

 

 

  • outlook接收邮件设置

James 邮件服务器安装,配置和Java Mail发送程序_第1张图片

James 邮件服务器安装,配置和Java Mail发送程序_第2张图片

 

 

  • 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 : [email protected];
     * @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", "[email protected]", "123456");    
        System.out.println("sending......");
        mail.send("[email protected]", "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 [email protected] on 111.74.1.123 for [[email protected]]
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 [email protected] for addresses [[email protected]]
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 [email protected] for [[email protected]]

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



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




你可能感兴趣的:(James 邮件服务器安装,配置和Java Mail发送程序)