java - 通过公司域发送 JavaMail 时遇到问题

标签 java dns smtp jakarta-mail

我正在使用 JavaMail 来自动化工作流程。我想使用 JavaMail 发送电子邮件,两封电子邮件均以 @business.com 结尾。这是一个片段:

String host = "smtp.fakename.com";

    Properties props = new Properties();

    props.put("mail.debug", "true");
    props.put("mail.smtp.host", host);

    try {
        Session session = Session.getDefaultInstance(props, null);

        Message msg = new MimeMessage(session);

        msg.setFrom(new InternetAddress("fakename@business.com"));
        msg.addRecipient(Message.RecipientType.TO, new InternetAddress("anotherfakename@business.com", "Name"));
        msg.setSubject("Test E-Mail through Java");

        Transport.send(msg);
        System.out.println("Sent message successfully...");
    }

我很困惑为什么它不起作用。我认为我不需要进行任何类型的身份验证,因为电子邮件是通过同一域发送的,但我也尝试过使用身份验证。我尝试过通过端口 25、110、143、465 和 587。我能够使用相同的代码并添加密码身份验证来通过 gmail 发送电子邮件。我看到的最常见错误是“无法连接到 SMTP 主机:...”。任何见解将不胜感激。

最佳答案

String host = "smtp.xyz.com";
final String username = "xyz@business.com";
final String password = "your password";

Properties props = new Properties();

props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
// go to account setting -> smtp setting and get server name and port
props.put("mail.smtp.host", "your server name");
props.put("mail.smtp.port", "your server port");
props.put("mail.debug", "true");

try {
    Session session = Session.getInstance(prop,
            new javax.mail.Authenticator() {
                protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
                    return new javax.mail.PasswordAuthentication(username, password);
                }
            });

    Message msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress("xyz@business.com"));

    msg.setRecipients(Message.RecipientType.TO,
            InternetAddress.parse("abc@business.com"));
    msg.setSubject("Test E-Mail through Java");

    Transport.send(msg,username,password);
    System.out.println("Sent message successfully...");
}catch(Exception e){

}

你可以试试这个可能会有帮助!!!

关于java - 通过公司域发送 JavaMail 时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24810655/

相关文章:

java - Spring 4 - HTTP 状态 400,所需参数不存在

java - Maven 拒绝从远程存储库下载 aar 打包的依赖项

java - 如何确保使用java.net传递的文件与传输前一样

grails - 我们可以为 grails 中的域类属性设置多个别名吗

ssl - Ejabberd 配置问题

php - 如何在 PHP 中启用用户自定义域

php - 使用 php 脚本 smtp.gmail 发送电子邮件时出错 Pear Centos

c# - 通过smtp服务器异步发送电子邮件-多线程发送电子邮件

java - 将 Action 发送给正在监听它的按钮

php - WP Mail SMTP 版本 1.4.2 停止发送电子邮件