根据邮箱DNS读取SMTP服务器地址,匿名发送邮件

本文详细介绍了如何从邮件地址中获取SMTP服务器IP,并通过Java实现邮件发送功能,包括设置邮件内容、发送者、接收者及邮件头信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

package com.tuqiang.parseprop;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import javax.mail.Address;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.Message.RecipientType;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

/**
 * 读取SMTP的IP地址
 * @author 王文莉
 */
public class ReadSmtpIp {

    
    public static void main(String[] args)
    {
        try {
            send("562341256@qq.com", "woerwoni@163.com", "收件人你好", "在做邮件发送测试......", "text/html;charset=UTF-8");
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    private static String getHost(String address)
    {
        return address.substring(address.indexOf("@") + 1);
    }
    

    public static void send(String to, String from, String subject, String content, String contentType) throws Exception {

        Properties props = new Properties();

        props.put("mail.smtp.localhost", getHost(from));

        String[] servers = getHostIp(getHost(to));

        for (String server : servers) {
            try {
                props.setProperty("mail.smtp.host", server);
                
                Session session = Session.getInstance(props, null);
                
                MimeMessage message = new MimeMessage(session);
                
                message.setContent(content, contentType);
                
                message.setSender(new InternetAddress(from));
                
                message.setFrom(new InternetAddress(from));
                
                message.setRecipient(RecipientType.TO, new InternetAddress(to));
                
                message.setSubject(subject);
                
                message.setHeader("Content-Type", contentType);
                
                message.setHeader("Content-Transfer-Encoding", "7bit");
                
                SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z");
                
                //SimpleDateFormat format = new SimpleDateFormat("YYYY-MM-DD HH:mm:ss");
                
                Date date = new Date();
                long time = date.getTime() - (long)24 * 60 * 60 *60;
                date.setTime(time);
                
                System.out.println( format.format(date));
                message.setHeader("Date", format.format(date));
                Transport.send(message, new Address[] { new InternetAddress(to) });
                System.out.println("break hello===============");
                break;
            } catch (RuntimeException e)
            {
                e.printStackTrace();
            }
        }
    }
    
    public static String[] getHostIp(String to)
    {
        try {
            Properties jndiEnvironmentProperties = new Properties();

            jndiEnvironmentProperties.put("java.naming.factory.initial", "com.sun.jndi.dns.DnsContextFactory");

            DirContext initialDirContext = new InitialDirContext(jndiEnvironmentProperties);

            Attributes attributes = initialDirContext.getAttributes(to, new String[] {"MX"});

            Attribute attribute = attributes.get("MX");
            
            String[] servers = new String[attribute.size()];

            for (int i = 0; i < attribute.size(); i++) {

                servers[i] = attribute.get(i).toString();
            }
            
            String[] host = new String[servers.length];
            int i = 0;
            for (String server : servers)
            {
                
                server = server.substring(server.indexOf(" ") + 1, servers[0].length() - 1);
                host[i] = server;
                i++;
            }
            return host;
        } catch (NamingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值