利用Mail Exchanger (MX) Record原理 发送email

利用Mail Exchanger (MX) Record原理 发送email

        发送邮件,我们一般会利用java api 发送,但是,你发送出去的邮件状态你是得不到的,比如用户不存在等,至于为什么,请参考:http://www.oracle.com/technetwork/java/javamail/faq/index.html#badaddr,很多FAQ可以去看看。java api 发送邮件,首先我们必须配置已有邮件帐号服务器,由对方服务器代发,而邮件的发送要经过很多中奖节点送达到收件人服务器上。

     这篇博客介绍的是利用Mail Exchanger (MX) Record原理 发送email,克服发送邮件设置代理服务器及得到邮件发送的状态。

     先看如何操作:

1.查找收件人邮箱的服务器地址

 打开网页http://mxtoolbox.com/,比如foxmail.com邮箱后缀

 利用Mail Exchanger (MX) Record原理 发送email_第1张图片

   我们随便选一个邮件服务mx3.qq.com

2.java编程实现发送邮件:


package com.doctor.javamail.demo;

import java.io.UnsupportedEncodingException;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeUtility;

import com.sun.mail.smtp.SMTPTransport;

/**
 * @author sdcuike
 *
 *         Created on 2016年6月26日 下午6:46:12
 */
public class CodeForBlog {
    private static final String SMTP_PROTOCOL_PREFIX = "smtp://";

    private static final String mail_mime_charset = "mail.mime.charset";
    private static final String mail_smtp_connectiontimeout = "mail.smtp.connectiontimeout";
    private static final String mail_smtp_timeout = "mail.smtp.timeout";
    private static final String mail_smtp_host = "mail.smtp.host";
    private static final String mail_smtp_localhost = "mail.smtp.localhost";
    private static final String mail_smtp_debug = "mail.debug";

    public static void main(String[] args) throws MessagingException, UnsupportedEncodingException {
        Properties prop = System.getProperties();
        prop.setProperty(mail_mime_charset, "UTF-8");
        prop.setProperty(mail_smtp_timeout, "30000");
        prop.setProperty(mail_smtp_connectiontimeout, "30000");
        prop.setProperty(mail_smtp_localhost, "doctorwho.com");
        prop.setProperty(mail_smtp_host, "doctorwho.com");
        Session session = Session.getInstance(prop, null);

        MimeMessage mimeMessage = new MimeMessage(session);

        mimeMessage.setSubject("550 Mail content denied.");

        String nickName = "注意开会";
        InternetAddress sender = new InternetAddress(MimeUtility.encodeText(nickName) + " <[email protected]>");
        mimeMessage.setSender(sender);
        mimeMessage.setFrom(sender);

        // mimeMessage.setRecipient(RecipientType.CC, new InternetAddress("[email protected]"));
        mimeMessage.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
        mimeMessage.setReplyTo(new Address[] { sender });
        mimeMessage.setText("Mail content denied. ");

        SMTPTransport transport = (SMTPTransport) session.getTransport(new URLName(SMTP_PROTOCOL_PREFIX + "mx3.qq.com"));
        transport.connect();
        transport.sendMessage(mimeMessage, new Address[] { new InternetAddress("[email protected]") });
        String lastServerResponse = transport.getLastServerResponse();
        System.out.println(lastServerResponse);
    }

}

 在得到session的时候,我们用到了第一步查询的邮件服务地址。

而且,发送的状态信息会返回给你,比如用户在对方邮件服务器的状态:用户不存在、用户被锁,以及服务判断是不是垃圾邮件等。

3.What is a Mail Exchanger (MX) Record?

 参考http://blog.onlymyemail.com/what-is-a-mail-exchanger-mx-record/,其实就是DNS记录分类中特殊的一种邮件交互记录。具体详细的信息参考链接地址。


4.编程实现MX记录查询,获取邮件服务器

 待续。。。。。

你可能感兴趣的:(re,mail,mail,Exchanger,Exchanger,mx,mx,mx,Record发送email)