package com.oa.modules.personnel.user.domain;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Hashtable;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
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.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.naming.Context;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;
public class MailSend {
public static String protocol = "smtp";
private String from = "hellojim@sina.com"; //发收人
private String to;
private String subject = "图文并茂的E-mail发送测试";
private String body = "图文并茂的E-mail发送测试<a href='http://hellojim.iteye.com'>Welcome access my blog my name is zhangxiaojun</a><br>" +
"<img src='cid:zxj'>";
private String server = "smtp.sina.com.cn";
public String getBody() {
return body;
}
public void setBody(String body) {
this.body = body;
}
public String getFrom() {
return from;
}
public void setFrom(String from) {
this.from = from;
}
public String getServer() {
return server;
}
public void setServer(String server) {
this.server = server;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public String getTo() {
return to;
}
public void setTo(String to) {
this.to = to;
}
public static void main(String[] args) throws Exception {
MailSend sender = new MailSend(); //实例化一个 MailSend 对象
sender.setTo("jim.zhang.js@gmail.com"); //设置一下收件人地址
boolean flg = sender.sendMessage(sender); //调用 sendMessage 就可以发送 E-mail
if(flg == true) {
System.out.println("哈哈...邮件发送成功");
} else {
System.out.println("呵呵...邮件发送失败");
}
}
//此方法根据传入的 session 创建一个E-mail 并返回
public MimeMessage createMessage(Session session,MailSend sender) throws Exception {
//创建一个邮件对象
MimeMessage message = new MimeMessage(session);
//设置 邮件 的发件人
message.setFrom(new InternetAddress(sender.from));
//设置 邮件 的收件人,及发送方法
message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(sender.to));
//设置 邮件 的主题
message.setSubject(sender.subject);
MimeMultipart multipart = new MimeMultipart("related");
//创建一个邮件主体的文字部分
MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart);
//创建一个邮件主体的图片部分
MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d:\\zxj.gif");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("zxj");
multipart.addBodyPart(gifBodyPart);
//设置 邮件 的主体部分(multipart 对象中包含了文字和图片两部分)
message.setContent(multipart);
//保存邮件这一步骤不能少
message.saveChanges();
return message;
}
//根据 收件人地址,发送 E-mail 如果发送成功返回 true ,否则返回 false
public boolean sendMessage(MailSend sender) {
Properties props = new Properties();
props.setProperty("mail.transport.protocol",MailSend.protocol);
props.setProperty("mail.smtp.auth","true");
props.setProperty("mail.host",sender.server);
MyAuthenticator myAuthenticator = new MyAuthenticator();
Session session = Session.getInstance(props,myAuthenticator);
session.setDebug(true);
try {
MimeMessage message = sender.createMessage(session,sender);
Transport transport = session.getTransport();
transport.connect();
transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO));
transport.close();
}catch(Exception ex) {
ex.printStackTrace();
System.out.println("邮件发送失败!");
return false;
}
return true;
}
//根据 发送方 e-mail 地址中的 @ 后面部分,获得 该 e-mail 地址所在的 smtp 服务器
//目前这个方法没有调用,好象得到的 MX 用在以上程序中有些问题
public static String getSmtpServer(String domain,String dnsServer) throws NamingException {
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL,"dns:" + dnsServer);
DirContext ctx = new InitialDirContext(env);
Attributes attrsAll = ctx.getAttributes(domain);
//Attributes attrsMx = ctx.getAttributes(domain,new String[]{"MX"});
Attribute attrMx = attrsAll.get("MX");
String firstValue = (String)attrMx.get();
return (firstValue.substring(firstValue.indexOf(" ") + 1));
}
}
针对上面的这个类文件,我有一个地方想请教大家一下,就是
private String server = "smtp.sina.com.cn"; 这句话不就是发送人的E-mail所属于的那个邮件服务器的地址吗?那么为什么我调用 getSmtpServer("sina.com",null); 这个方法(此方法的作用是得到MX记录啊) 得到的值是 freemx4.sinamail.sina.com.cn. 然后用此值给 server 变量 后,发送时就发送不成功,这是为什么???这地方我不一定以后就用 sina E-mail 去发邮件啊,也可能用 126 的,或 gmail 的,那么 server 的值又应给多少呢?或者从那里得到呢?请大家指教