Java SMTP发送邮件

package egtl.test;

import javax.naming.directory.Attributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.InitialDirContext;

import java.util.Properties;
import java.util.Date;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.Message;
import javax.mail.internet.MimeMessage;

public class SMTPSender {
    public static void main(String[] args) throws Exception {
        // 下面是邮件要群发给的多个收件人地址
        String[] to = {"Wei.Gan@163.com"};

        // 创建Session对象
        Properties props = new Properties();
        /*
         * mail.smtp.localhost属性用于设置SMTP协议的EHLO命令中的
         * 主机名,其他SMTP服务器可能需要使用这个主机名确定发件SMTP 服务器的身份,这个信息可以从JavaMail的javadocs文档中的
         * com.sun.mail.smtp包的帮助页面内查看到。
         */
        props.setProperty("mail.smtp.localhost", "10.81.192.21");
        Session session = Session.getInstance(props);
        session.setDebug(true);

        Message msg = createMessage(session);
        for (int i = 0; i < to.length; i++) {
            try {
                sendMessage(session, msg, to[i]);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    public static Message createMessage(Session session) throws Exception {

        String from = "Nan.Chen@163.com";// 发件人地址
        String subject = "SMTPSender demo";// 邮件主题
        String body = "This is a Lotus Notes Test...";// 邮件内容

        // 创建代表邮件的MimeMessage对象,不包含收件人地址
        MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress(from));
        msg.setSentDate(new Date());
        msg.setSubject(subject);
        msg.setText(body);
        return msg;
    }

    public static void sendMessage(Session session, Message msg, String to)
            throws Exception {
        // 设置邮件内容的收件人并生成邮件消息内容
        msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
        msg.saveChanges();

        // 连接收件人地址所在的SMTP服务器并发送邮件
        Transport transport = session.getTransport("smtp");
        String domain = to.substring(to.indexOf("@") + 1);
        String smtpServer = getSmtpServer(domain, null);
        transport.connect(smtpServer, null, null);
        transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO));
        transport.close();
    }

    public static String getSmtpServer(String domain, String dnsServer)
            throws Exception {
        DirContext ctx = new InitialDirContext();
        Attributes attrsMx = null;
        if (dnsServer != null) {
            attrsMx = ctx.getAttributes("dns:" + "//" + dnsServer + "/"
                    + domain, new String[] { "MX" });
        } else {
            attrsMx = ctx.getAttributes("dns:" + "/" + domain,
                    new String[] { "MX" });
        }

        String recordMx = (String) attrsMx.get("MX").get();
        String smtpServer = recordMx.substring(recordMx.indexOf(" ") + 1);
        return smtpServer;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Java实现SMTP邮件发送的示例代码: ```java import java.util.Properties; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.PasswordAuthentication; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class SendMail { public static void main(String[] args) { final String username = "your_email@example.com"; // 发件人邮箱账号 final String password = "your_email_password"; // 发件人邮箱密码 final String recipient = "recipient@example.com"; // 收件人邮箱账号 Properties props = new Properties(); props.put("mail.smtp.auth", "true"); props.put("mail.smtp.starttls.enable", "true"); props.put("mail.smtp.host", "smtp.example.com"); // 发件人邮箱SMTP服务器地址 props.put("mail.smtp.port", "587"); // 发件人邮箱SMTP服务器端口号 Session session = Session.getInstance(props, new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(username, password); } }); try { Message message = new MimeMessage(session); message.setFrom(new InternetAddress(username)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipient)); message.setSubject("Testing Subject"); message.setText("Dear Mail Crawler," + "\n\n No spam to my email, please!"); Transport.send(message); System.out.println("邮件发送成功!"); } catch (MessagingException e) { throw new RuntimeException(e); } } } ``` 请注意,您需要将示例代码中的发件人邮箱账号、密码、收件人邮箱账号、发件人邮箱SMTP服务器地址和端口号替换为您自己的信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值