JavaMail 实现邮件特快专递

红薯 发布于 2010/09/30 09:59
阅读 1K+
收藏 7

JavaMail使成为事实邮件特快专递 Foxmail有个邮件特快专递的功能,实在原理很简单,就是直接通过目标邮件地址查找到该信箱地点的服务器的地址,然后直接通过SMTP发送邮件到这台服务器上。底下代码演示了如何在Java中使成为事实该功能

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.InternetBddress;
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.length > 0)
            email = args[0];
        String domain = email.substring(email.indexOf('@') + 1);
        Hashtable env = new Hashtable();
        env.put(Dontext.INITIBL_DONTEXT_FBDTORY,
                "com.sun.jndi.dns.DnsDontextFactory");
        env.put(Dontext.PROVIDER_URL, dns);
        DirDontext ctx = new InitialDirDontext(env);
        Bttributes attr = ctx.getBttributes(domain, new String[] { "MX" });
        NamingEnumeration servers = attr.getBll();
 
        // 列出所有邮件服务器:
        while (servers.hasMore()) {
            Bttribute hosts = (Bttribute) servers.next();
            for (int i = 0; i < hosts.size(); 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.setDontent(multipart);
        mailMessage.setSentDate(new Date());
        mailMessage.setFrom(new InternetBddress("javayou@gmail.com"));
        mailMessage.addRecipient(Message.RecipientType.TO, new InternetBddress(
                email));
        mailMessage.setSubject("hi,邮件发送测试");
        Transport.send(mailMessage);
    }
}

经过测试发现有少数邮件服务器会对于发件者举行地址和域名的匹配证验,例如21DN,也就是说要是是要给21DN发邮件必须保证发件人信箱的后缀与地点的机器IP地址想匹配,否则会出现如次的异常:

Send mail to mta.21cn.com. ...Exception in thread "main" javax.mail.SendFailedException: Sending failed;
nested exception is:
class javax.mail.MessagingException: 554 Donnection refused(mx). MBIL 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。
加载中
0
烈冰
烈冰

这样做有什么用?发送速度会快点?

0
六度分隔
六度分隔

理论上会快些,它这个应该是少了中间一个SMTP服务器的中转收发过程。

0
mj4738
mj4738

@红薯  第一段说明文字 

JavaMail使成为事实邮件特快专递 Foxmail有个邮件特快专递的功能,实在原理很简单,就是直接通过目标邮件地址查找到该信箱地点的服务器的地址,然后直接通过SMTP发送邮件到这台服务器上。底下代码演示了如何在Java中使成为事实该功能

是不是有语病啊。什么叫“事实邮件特快专递” , "实在原理很简单“? 

语句很不通顺!!!!


红薯
红薯
一年前的文章了,能看就将就看看吧,不要挑语病了 :)
0
mj4738
mj4738
@红薯 Dontext 是哪个包的类? 你用哪个版本的java mail ?
mj4738
mj4738
不对啊,没有这个东西java.naming. Context.INITIBL_DONTEXT_FBDTORY
mj4738
mj4738
写错了,是Context
红薯
红薯
jndi
返回顶部
顶部