java - 连接被拒绝 : connect - Java mail API

标签 java email smtp jakarta-mail

我在使用 Java 邮件 API 从公司 Outlook 发送电子邮件时收到以下错误。

javax.mail.MessagingException: Could not connect to SMTP host:       smtp.mycompany.net.au, port: 25;
 nested exception is:
java.net.ConnectException: Connection refused: connect

我能够使用与我的计算机相同的端口远程登录服务器。问题的根本原因可能是什么?

用于发送电子邮件的代码是 -

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", mysmtpserver);
props.put("mail.smtp.port", myport);

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

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromEmailAddress));

message.setRecipients(Message.RecipientType.BCC, addressTo);
message.setSubject(emailSubject);

BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setHeader("Content-Type", "text/html;charset=UTF-8");
messageBodyPart.setHeader("Content-Transfer-Encoding", "quoted-printable"); 
messageBodyPart.setContent(emailBody, "text/html;charset=UTF-8");

Multipart multipart = new MimeMultipart();  
//part 1-add html part
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
String filename = reportFilePath.trim();
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));                    
messageBodyPart.setFileName(filename);                  
multipart.addBodyPart(messageBodyPart);

感谢解决该问题的任何帮助。

谢谢 利宾

最佳答案

java.net.ConnectException:连接被拒绝:由于以下原因引发连接错误,您必须检查:

  1. 您的主机名或端口
  2. 您的属性代码编写准确
  3. 确保防火墙没有阻止端口
  4. 如果您的服务器需要密码,请提供密码

感谢您提供代码。请应用以下代码。它非常容易理解并且运行良好。 100% 经过测试

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;   
import javax.mail.event.*;      
import java.net.*;
import javax.activation.*;

public class SendEmail extends ConfigProperties{

        public static void send(String from, String to, String subject, String content) throws AddressException, MessagingException{

        ErrorCheck ec   = new ErrorCheck();
        String error = "";

        try{
            error   = ec.error();

            String smtp = getPropVal("SMTP");
            String host = getPropVal("HOST");

            Properties props=new Properties();
                props.put(smtp,host);
            Session   session1  =  Session.getDefaultInstance(props,null);
            Message msg =new MimeMessage(session1);

            msg.setHeader("Content-Type", "text/plain; charset=UTF-8");
            msg.setHeader("Content-Transfer-Encoding", "quoted-printable");           

               msg.setFrom(new InternetAddress(from));
                   msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(to,false));
               msg.setSubject(subject);
               msg.setContent(content,"text/html; charset=UTF-8");      

                Transport.send(msg); 

            }catch(Exception e){
            ec.errorMsg(error+"SendEmail.send()", e);
            }
        }
}

关于java - 连接被拒绝 : connect - Java mail API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33796935/

相关文章:

php - 使用 PHPMailer 在邮件中添加附件失败

c# - 使用 C# 从 gmail 获取未读计数

c++ - SMTP编译错误

smtp - 需要在 IIS 上为 ASP.Net Core 身份服务器配置 SMTP 设置以进行电子邮件验证

java - 如何在数组:中查找包含相等总和的子集

java - 在 Java 中迭代 Map 时跳过索引

java - 如何访问 Apache Camel 2.14.1 中 Hashmap 中的属性?

email - 为什么此特定邮件会发送至 hotmail 用户的垃圾邮件?

java - 如何从 jsp 页面创建到 war 文件的链接

java - Log4j2 SMTP 到 Office365