前言:
最近写一个管理系统需要用到发送邮件的功能,曾经在javamail中使用过163的smtp服务,但是邮件要么被当成垃圾邮件,要么就是无法在一天之内发送超过20封,四天前我再次使用javamail时,发现我的163账号被冻结,允许我在厕所哭一会儿。然后换了qq的smtp服务,开启服务还需收短信费用,以为花了钱可以弄好,结果又发现,javamail和腾讯的smtp握手失败,已哭晕在厕所。最后忍无可忍只好自己搭一个邮件服务器。
牢骚发完了,开始进入主题,我使用的是centos6.5系统,以及和postfix邮件服务器,postfix邮件服务器的搭建需要使用到cyrus-sasl、postfix、dovecot,具体他们的功能我不在这里阐述,毕竟网上一大堆。
一、cyrus-sasl安装与配置
yum -y install cyrus-sasl
与jdk安装一样最好查看他的版本,如果有版本提示则就证明安装成功。
/usr/sbin/saslauthd -v
之后需要修改saslauthd文件,选择使用shadow方式认证。(温馨提示:可以在linux中使用/word去查找单词,如下面的MECH=shadow可以输入/MECH=查找)
vim /etc/sysconfig/saslauthd
SOCKETDIR=/var/run/saslauthd
MECH=shadow
FLAGS=
修改smtpd.conf
vim /etc/sasl2/smtpd.conf
添加如下代码
pwcheck_method: saslauthd
mech_list: plain login
log_level: 3
saslauthd_path:/var/run/saslauthd/mux
测试cyrus-sasl
创建系统用户root,并修改其密码,测试cyrus-sasl,如下:
useradd root &&echo 'rootmail'| passwd --stdin root
su - root
mkdir -p ~/mail/.imap/INBOX
启动cyrus-sasl
/etc/init.d/saslauthd start
chkconfig saslauthd on
现在来使用刚刚创建的用户进行认证测试,如下:
testsaslauthd -u root-p 'rootmail'
如果出现OK "Success"代表启动成功,否则请严格检查配置文件。
二、postfix安装与基本配置
yum -y install postfix
查看他的版本与配置
postconf -d | grep mail_version
修改main.cf文件
vim /etc/postfix/main.cf
并添加如下代码
queue_directory = /var/spool/postfix
command_directory = /usr/sbin
daemon_directory = /usr/libexec/postfix
data_directory = /var/lib/postfix
mail_owner = postfix
myhostname = mail.XXX.com (这里是写你的服务器的域名前加上一个前缀,一般我们都喜欢用mail,域名必须可访问)
mydomain = XXX.com (这里是写你的服务器的域名,域名必须可访问)
myorigin = $mydomain
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost
local_recipient_maps =
unknown_local_recipient_reject_code = 550
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
debug_peer_level = 2
debugger_command =
PATH=/bin:/usr/bin:/usr/local/bin:/usr/X11R6/bin
ddd $daemon_directory/$process_name $process_id & sleep 5
sendmail_path = /usr/sbin/sendmail.postfix
newaliases_path = /usr/bin/newaliases.postfix
mailq_path = /usr/bin/mailq.postfix
setgid_group = postdrop
html_directory = no
manpage_directory = /usr/share/man
sample_directory = /usr/share/doc/postfix-2.6.6/samples
readme_directory = /usr/share/doc/postfix-2.6.6/README_FILES
启动postfix并设置开机自启
/etc/init.d/postfix start
chkconfig postfix on
查看postfix的监听端口,如果发现是25端口证明postfix已经成功启动
netstat -tunlp
ps -ef |grep postfix
我们来测试postfix是否可以发邮件
telnet mail.XXX.com 25
mail from:root@XXXX.com
rcpt to:xxxx@qq.com
data
XXXXXXXX
quit
登录邮箱发现确实有一封邮件。
这个时候,我们来试试javamail。
import java.util.List;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.BodyPart;
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 MailUtils {
/**
* 向用户发送邮件
*/
private static final long serialVersionUID = 1L;
private MimeMessage mimeMsg; // MIME邮件对象
private Session session; // 邮件会话对象
private Properties props; // 系统属性
private boolean needAuth = false; // smtp是否需要认证
private String username; // smtp认证用户名和密码
private String password;
private Multipart mp; // Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成
private String log;
public MailUtils() {
}
public MailUtils(String smtp) {
setSmtpHost(smtp);
createMimeMessage();
}
public void setSmtpHost(String hostName) {
System.out.println("设置系统属性:mail.smtp.host = " + hostName);
if (props == null)
props = System.getProperties(); // 获得系统属性对象
props.put("mail.smtp.host", hostName); // 设置SMTP主机
props.put("mail.smtp.localhost", "localHostAdress");
}
public boolean createMimeMessage() {
try {
System.out.println("准备获取邮件会话对象!");
session = Session.getDefaultInstance(props, null); // 获得邮件会话对象
} catch (Exception e) {
log = "获取邮件会话对象时发生错误!" + e.toString();
System.err.println(log);
return false;
}
try {
mimeMsg = new MimeMessage(session); // 创建MIME邮件对象
mp = new MimeMultipart(); // mp 一个multipart对象
// Multipart is a container that holds multiple body parts.
return true;
} catch (Exception e) {
log = "创建MIME邮件对象失败!" + e;
System.err.println(log);
return false;
}
}
public void setNeedAuth(boolean need) {
System.out.println("设置smtp身份认证:mail.smtp.auth = " + need);
if (props == null)
props = System.getProperties();
if (need) {
props.put("mail.smtp.auth", "true");
} else {
props.put("mail.smtp.auth", "false");
}
}
public void setNamePass(String name, String pass) {
System.out.println("程序得到用户名与密码");
username = name;
password = pass;
}
public boolean setSubject(String mailSubject) {
System.out.println("设置邮件主题!");
try {
mimeMsg.setSubject(MimeUtility.encodeText(mailSubject,"utf-8","B"));
return true;
} catch (Exception e) {
log = "设置邮件主题发生错误!"+e;
System.err.println(log);
return false;
}
}
public boolean setBody(String mailBody) {
try {
System.out.println("设置邮件体格式");
BodyPart bp = new MimeBodyPart();
// 转换成中文格式
bp.setContent(
"<meta http-equiv=Content-Type content=text/html; charset=utf-8>"
+ mailBody, "text/html;charset=utf-8");
mp.addBodyPart(bp);
return true;
} catch (Exception e) {
log = "设置邮件正文时发生错误!" + e;
System.err.println(log);
return false;
}
}
public boolean setFiles(List<String> files){
try{
for(String s:files){
MimeBodyPart mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(s); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(fds.getName()); //得到文件名同样至入BodyPart
mp.addBodyPart(mbp);
}
return true;
}catch(Exception e){
log = "增加附件时出错:"+e;
e.printStackTrace();
return false;
}
}
public boolean addFile(String path, String name){
try{
MimeBodyPart mbp=new MimeBodyPart();
FileDataSource fds=new FileDataSource(path); //得到数据源
mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart
mbp.setFileName(MimeUtility.encodeText(name,"utf-8","B"));
mp.addBodyPart(mbp);
return true;
}catch(Exception e){
log = "增加附件时出错:"+e;
e.printStackTrace();
return false;
}
}
public boolean setFrom(String from) {
System.out.println("设置发信人!");
try {
mimeMsg.setFrom(new InternetAddress(from)); // 设置发信人
return true;
} catch (Exception e) {
log = "设置发信人出错:"+e;
return false;
}
}
public boolean setTo(String to) {
System.out.println("设置收信人");
if (to == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(to));
return true;
} catch (Exception e) {
return false;
}
}
public boolean setCopyTo(String copyto) {
if (copyto == null)
return false;
try {
mimeMsg.setRecipients(Message.RecipientType.CC,
(Address[]) InternetAddress.parse(copyto));
return true;
} catch (Exception e) {
return false;
}
}
public boolean sendout() {
try {
mimeMsg.setContent(mp);
mimeMsg.saveChanges();
System.out.println("正在发送邮件....");
Session mailSession = Session.getInstance(props, null);
Transport transport = mailSession.getTransport("smtp");
transport.connect((String) props.get("mail.smtp.host"), username,
password);
transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));
System.out.println("发送邮件成功!");
transport.close();
return true;
} catch (Exception e) {
log = "邮件发送失败!" + e;
System.err.println(log);
return false;
}
}
public String getLog(){
return log;
}
public static void main(String[] args) {
MailUtils utils = new MailUtils("mail.xxx.com");
utils.setFrom("root@xxx");
utils.setSubject("xxxx");
utils.setNamePass("xxx","xxx");
utils.setBody("main");
utils.setTo("xxxx@qq.com");
utils.sendout();
}
}
运行成功!
尾声
到此结束,但是在这里我并没有配置反垃圾策略,有需要的同学可以百度下,并且在这里,如果我发送大量的邮件的话,还是无法避免封ip,这也是为什么要购买邮件服务器的原因吧。