Foxmail有个邮件特快专递的功能,其实原理很简单,就是直接通过目标邮件地址查找到该邮箱所在的服务器的地址,然后直接通过SMTP发送邮件到这台服务器上。下面代码演示了如何在Java中实现该功能
/*
* 项目:TOOLBOX
* 所在包:com.clickcom.mail
* 文件名:MailHostLookup.java
* 创建时间:2004-11-25
* 创建者:Winter Lau
*/
package com.clickcom.mail;
import java.util.*;
import javax.mail.Message;
import javax.mail.MessagingException;
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.naming.*;
import javax.naming.directory.*;
/**
* 实现邮件的特快专递功能
* @author Winter Lau
*/
public class Mailer {
public static void main(String[] args)
throws NamingException, MessagingException
{
// DNS服务器,看看本机的DNS配置
String dns = "dns://61.144.56.101";
String email = "liudong@mo168.com";
if(args.length0)
email = args[0];
String domain = email.substring(email.indexOf('@')+1);
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY,
"com.sun.jndi.dns.DnsContextFactory");
env.put(Context.PROVIDER_URL, dns);
DirContext ctx = new InitialDirContext(env);
Attributes attr = ctx.getAttributes(domain, new String[]{"MX" });
NamingEnumeration servers = attr.getAll();
// 列出所有邮件服务器:
while(servers.hasMore()) {
Attribute hosts = (Attribute)servers.next();
for(int i=0;i
String host = (String)hosts.get(i);
host = host.substring(host.indexOf(' ')+1);
System.out.print("Send mail to " + host + " ...");
sendMail(host, email);
System.out.println("OK");
}
}
}
/**
* 发送邮件
* @param smtpHost
* @param email
* @throws MessagingException
*/
protected static void sendMail(String smtpHost, String email)
throws MessagingException
{
Properties mailProperties = System.getProperties();
mailProperties.put("mail.smtp.host", smtpHost);
mailProperties.put("mail.smtp.port", "25");
mailProperties.put("mail.smtp.auth", "false");
Session mailSession = Session.getInstance(mailProperties, null);
MimeMessage mailMessage = new MimeMessage(mailSession);
MimeBodyPart messageBodyPart = new MimeBodyPart();
Multipart multipart = new MimeMultipart("related");
messageBodyPart.setText("这里是邮件内容");
multipart.addBodyPart(messageBodyPart);
mailMessage.setContent(multipart);
mailMessage.setSentDate(new Date());
mailMessage.setFrom(new InternetAddress("javayou@gmail.com"));
mailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(email));
mailMessage.setSubject("你好,邮件发送测试");
Transport.send(mailMessage);
}
}
经过测试发现有少数邮件服务器会对发件者进行地址和域名的匹配验证,例如21CN,也就是说如果是要给21CN发邮件必须保证发件人邮箱的后缀与所在的机器IP地址想匹配,否则会出现如下的异常:
Send mail to mta.21cn.com. ...Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 554 Connection refused(mx). MAIL FROM [javayou@gmail.com] mismatches client IP [218.19.72.248].
at javax.mail.Transport.send0(Transport.java:218)
at javax.mail.Transport.send(Transport.java:80)
at com.clickcom.mail.Mailer.sendMail(Mailer.java:87)
at com.clickcom.mail.Mailer.main(Mailer.java:53)
其他的服务基本正常,例如163.com。
这东西可用来发匿名邮件