jjzjj

javax.mail.MessagingException : Could not connect to SMTP host?

coder 2024-03-27 原文

以下是我发送邮件的代码:

import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
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 void sendMail(String m_from,String m_to,String m_subject,String m_body){
      try {
            Session m_Session;
            Message m_simpleMessage;
            InternetAddress m_fromAddress;
            InternetAddress m_toAddress;
            Properties m_properties;

            m_properties     = new Properties();
            m_properties.put("mail.smtp.host", "usdc2spam2.slingmedia.com"); 
            m_properties.put("mail.smtp.socketFactory.port", "465");
            m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
            m_properties.put("mail.smtp.auth", "true");
            m_properties.put("mail.smtp.port", "9000");

            m_Session=Session.getDefaultInstance(m_properties,new Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("aaaaa","bbbbb@1"); // username and the password
                }
            });

            m_simpleMessage  =   new MimeMessage(m_Session);
            m_fromAddress    =   new InternetAddress(m_from);
            m_toAddress      =   new InternetAddress(m_to);

            m_simpleMessage.setFrom(m_fromAddress);
            m_simpleMessage.setRecipient(RecipientType.TO, m_toAddress);
            m_simpleMessage.setSubject(m_subject);

            m_simpleMessage.setContent(m_body, "text/html");

            //m_simpleMessage.setContent(m_body,"text/plain");

            Transport.send(m_simpleMessage);
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }
    }
    public static void main(String[] args) {
      SendMail send_mail    =   new SendMail();
      String empName = "xxxxx";
      String title ="<b>Hi !"+empName+"</b>";
      send_mail.sendMail("123erft@slingmedia.com", "abz@gmail.com", "Please apply for leave for the following dates", title+"<br>by<br><b>HR<b>");
    }
}

但是当我运行代码时,出现以下异常。

javax.mail.MessagingException: Could not connect to SMTP host: usdc2spam2.slingmedia.com, port: 9000;
  nested exception is:
    java.net.ConnectException: Connection refused: connect
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1934)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:638)
    at javax.mail.Service.connect(Service.java:317)
    at javax.mail.Service.connect(Service.java:176)
    at javax.mail.Service.connect(Service.java:125)
    at javax.mail.Transport.send0(Transport.java:194)
    at javax.mail.Transport.send(Transport.java:124)
    at samples.SendMail.sendMail(SendMail.java:46)
    at samples.SendMail.main(SendMail.java:55)
Caused by: java.net.ConnectException: Connection refused: connect
    at java.net.PlainSocketImpl.socketConnect(Native Method)
    at java.net.PlainSocketImpl.doConnect(Unknown Source)
    at java.net.PlainSocketImpl.connectToAddress(Unknown Source)
    at java.net.PlainSocketImpl.connect(Unknown Source)
    at java.net.SocksSocketImpl.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at java.net.Socket.connect(Unknown Source)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:288)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:231)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1900)

当我 ping 这个 usdc2spam2.slingmedia.com 时,它给我回复没有任何问题。我正在使用 windows 7

请帮我解决这个问题。

最佳答案

这是给我带来问题的这两行:

m_properties.put("mail.smtp.socketFactory.port", "465");
  m_properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");

并添加了这一行:

m_properties.put("mail.smtp.starttls.enable", "true");

删除并添加上面的代码行后,它工作正常。

关于javax.mail.MessagingException : Could not connect to SMTP host?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11029444/

有关javax.mail.MessagingException : Could not connect to SMTP host?的更多相关文章

  1. ruby - 使用ruby mail gem获取电子邮件正文而不获取附件 - 2

    我正在尝试使用IMAP和ruby​​mailgem获取gmail的正文。当我按照anotherstackoverflowanswer.中的描述获取RFC822字段时,它工作得很好.Fiedl很好地描述了这种方法answer类似的问题。这种方法很棒,只是它需要获取RFC822,而RFC822也会获取所有附件。是否有任何其他领域或其他方法我可以采取不获取附件但仍然使用ruby​​mailgem来获得解码良好的正文? 最佳答案 您必须解析并理解返回的BODYSTRUCTURE响应的实际结构,请参阅RFC3501,p.56.还要记住应用相关

  2. ruby - 在使用 ruby​​ 中的 "mail"gem 阅读电子邮件时需要帮助 - 2

    我正在使用Watir进行自动化,它会创建一封我需要检查的电子邮件。有人指出电子邮件gem是执行此操作的最简单方法。我添加了以下代码,并且能够从我的收件箱中收到第一封电子邮件。require'mail'require'openssl'Mail.defaultsdoretriever_method:pop3,:address=>"email.someemail.com",:port=>995,:user_name=>'domain/username',:password=>'pwd',:enable_ssl=>trueendputsMail.first我是这个论坛的新手,有以下问题:如何获

  3. ruby "Mail" gem 文档 - 2

    我找不到Mailgem的完整文档https://github.com/mikel/mail对于文档,所有链接都指向github主页面,但自述文件远未完成。有没有其他方法可以在源代码中搜索所有内容? 最佳答案 根据以上评论中的@d11wtq,您可以在ruby-doc.org上找到rdoc文档和rubydoc.info 关于ruby"Mail"gem文档,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/q

  4. ruby - 在 Ruby Net::IMAP 中选择邮箱 "sent mail"或 "all mail" - 2

    我试图在Ruby中使用Net::IMAP来搜索我发送的所有邮件,但我无法选择收件箱以外的任何内容。imap.select('INBOX')工作正常,但是imap.select('Mail/sent-mail')如Net::IMAP文档所示,显示“未知邮箱”。顺便说一句,这是与gmail一起使用的。我还尝试将“in”、“anywhere”添加到我的imap.search()中,但没有解析。当前代码:imap.select('INBOX')now=Time.now.localtime-1209600#twoweekssince=now.day.to_s()+"-"+Date::MONTHN

  5. javax 脚本如何从 Java 调用 JavaScript 中的函数 - 2

    我正在尝试通过Java调用JavaScript中的函数。这在直接将脚本作为字符串读取时效果很好,但我使用的是CompiledScripts。当我使用编译脚本执行此操作时,如果我还添加绑定(bind),它会提示找不到方法。没有绑定(bind)它可以工作,但当然函数失败,因为它需要绑定(bind)。有什么想法吗?CompiledScriptscript=...getscript....Bindingsbindings=script.getEngine().createBindings();LoggerscriptLogger=LogManager.getLogger("TEST_SCRIP

  6. javascript - Rhino load() 函数在 javax.script 提供的 JavaScript 中可用吗? - 2

    一些为Rhino的shell开发的JavaScript文件使用load()来加载额外的JavaScript文件。我正在尝试使用javax.script从这些RhinoJavaScript文件之一中嵌入功能。不幸的是,javax.script的JavaScript没有实现load()函数。尝试对包含load()的脚本执行eval()时,会发生以下错误:com.sun.script.javascript.RhinoScriptEngine:-1:in`eval':javax.script.ScriptException:sun.org.mozilla.javascript.internal

  7. go-mail 嵌入图像不适用于 exe - 2

    我正在使用go-mail发送邮件。我在邮件中嵌入图像经过m.Embed("common/static/img/logo.png")并在HTML中使用它运行main.go时运行正常。但是,当我要投影并执行main.exe时,出现错误“系统找不到指定的路径。” 最佳答案 这里可能有多个问题。一个是您使用的文件路径具有特定于平台的路径分隔符。Windows使用“\”而不是“/”。要编写与平台无关的路径,请使用https://godoc.org/path/filepath#Joinfilepath.Join("common","static

  8. email - 如何为电子邮件设置 "MAIL FROM" header ? - 2

    在用于设置“退回域”的SparkPost(电子邮件发送提供商)文档中说specifiedinthe[...]mailfromheaderintheSMTPpayloadhttps://www.sparkpost.com/docs/tech-resources/custom-bounce-domain/但是当我设置“MAILFROM”header时,我从他们的服务器收到回复5505.6.0Invalidheaderfound(seeRFC2822section3.6)我正在使用插件gomail"gopkg.in/gomail.v2"设置“MAILFROM”header的实际含义是什么?如

  9. google-app-engine - 继续 Google App Engine 返回错误 : API error 1 (mail: INTERNAL_ERROR): Internal error - 2

    我有点迷路了。通过GAE发送电子邮件似乎不起作用。抛出的错误是:无法发送电子邮件:API错误1(邮件:INTERNAL_ERROR):内部错误我已经尝试了几个不同的发件人地址,但似乎都始终如一地有效。有时它有效有时它不。在本地,一切似乎都正常(当然没有邮件发送,但日志显示发送假设的电子邮件)。代码:func(coinflip*Coinflip)mailParticipants(contextappengine.Context,key*datastore.Key){participants,_,_:=coinflip.fetchParticipants(context)fori:=ran

  10. xml - 使用 JAXB 时出现 java.net.MalformedURLException : no protocol with javax. xml.stream.XMLStreamException - 2

    我正在使用JAXB解码XML文档。在解析XML时,它会抛出一个用XMLStreamException包装的MalformedURLException。我的理解是在创建XMLStreamReader对象本身时它抛出了异常。有什么建议吗?我使用的代码片段:XMLInputFactoryxif=XMLInputFactory.newFactory();XMLResolverresolver=newXMLResolver();//tocapturesystemID,baseURIetc.xif.setXMLResolver(resolver);//ThrowsMalformedURLExcep

随机推荐