首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >连接拒绝:连接- Java邮件API

连接拒绝:连接- Java邮件API
EN

Stack Overflow用户
提问于 2015-11-19 06:42:42
回答 1查看 6.2K关注 0票数 1

在使用java邮件API从公司outlook发送电子邮件时,我收到了以下错误。

代码语言:javascript
复制
javax.mail.MessagingException: Could not connect to SMTP host:       smtp.mycompany.net.au, port: 25;
 nested exception is:
java.net.ConnectException: Connection refused: connect

我能够用相同的端口告诉服务器,我的machine.What可能是问题的根本原因?

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

代码语言:javascript
复制
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);

感谢在解决这一问题上的任何帮助。

谢谢利宾

EN

回答 1

Stack Overflow用户

发布于 2015-11-19 10:04:12

java.net.ConnectException:连接被拒绝:由于必须检查的下列原因,连接错误抛出:

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

谢谢你提供你的代码。请应用以下代码。这很容易理解,也很好用。100%测试

代码语言:javascript
复制
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);
            }
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33796935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档