<small id='jSjOB'></small><noframes id='jSjOB'>

    <bdo id='jSjOB'></bdo><ul id='jSjOB'></ul>

        <tfoot id='jSjOB'></tfoot>
      1. <legend id='jSjOB'><style id='jSjOB'><dir id='jSjOB'><q id='jSjOB'></q></dir></style></legend>
      2. <i id='jSjOB'><tr id='jSjOB'><dt id='jSjOB'><q id='jSjOB'><span id='jSjOB'><b id='jSjOB'><form id='jSjOB'><ins id='jSjOB'></ins><ul id='jSjOB'></ul><sub id='jSjOB'></sub></form><legend id='jSjOB'></legend><bdo id='jSjOB'><pre id='jSjOB'><center id='jSjOB'></center></pre></bdo></b><th id='jSjOB'></th></span></q></dt></tr></i><div id='jSjOB'><tfoot id='jSjOB'></tfoot><dl id='jSjOB'><fieldset id='jSjOB'></fieldset></dl></div>

        如何更改 JavaMail 端口

        How to change JavaMail port(如何更改 JavaMail 端口)
        • <i id='JEKfY'><tr id='JEKfY'><dt id='JEKfY'><q id='JEKfY'><span id='JEKfY'><b id='JEKfY'><form id='JEKfY'><ins id='JEKfY'></ins><ul id='JEKfY'></ul><sub id='JEKfY'></sub></form><legend id='JEKfY'></legend><bdo id='JEKfY'><pre id='JEKfY'><center id='JEKfY'></center></pre></bdo></b><th id='JEKfY'></th></span></q></dt></tr></i><div id='JEKfY'><tfoot id='JEKfY'></tfoot><dl id='JEKfY'><fieldset id='JEKfY'></fieldset></dl></div>

          <legend id='JEKfY'><style id='JEKfY'><dir id='JEKfY'><q id='JEKfY'></q></dir></style></legend>
            <tbody id='JEKfY'></tbody>

          <small id='JEKfY'></small><noframes id='JEKfY'>

            • <tfoot id='JEKfY'></tfoot>

                <bdo id='JEKfY'></bdo><ul id='JEKfY'></ul>

                  本文介绍了如何更改 JavaMail 端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在使用 JavaMail 编写一个小型 Java 应用程序,该应用程序向用户发送一封自动电子邮件.他们可以(目前)在两个端口之间进行选择:25 和 587.可以通过 GUI 上的单选按钮选择端口.

                  I'm writing a small Java app using JavaMail that sends the user an automated email. They can choose between (for now) two ports: 25 and 587. The port can be selected via a radio button on the GUI.

                  我添加了一个测试按钮,允许用户测试电子邮件设置(包括端口).但是,由于某种原因,一旦用户尝试发送测试电子邮件,就无法更改端口.Javamail 将始终使用原始测试电子邮件的端口.

                  I added a test button to allow the user to test the email settings (including port). However, for some reason, once the user tries to send a test email, the port can't be changed. Javamail will always use the port of the original test email.

                  示例:用户尝试在端口 25 上发送电子邮件,而 JavaMail 说它无法在端口 25 上连接(例如,SMTP 主机使用另一个端口).用户单击端口 587,并尝试发送新电子邮件.JavaMail 再次抛出一个错误,说它无法连接到端口 25.

                  Example: User tries to send an email on port 25 and JavaMail says it can not connect on port 25 (for example, the SMTP host uses another port). User clicks port 587, and tries to send a new email. JavaMail throws an error saying it can not connect on port 25, again.

                  我有点不知道为什么.每次发送新的测试电子邮件时,都会创建一个全新的 SendMailUsingAuthentication 对象.在该类中,属性始终重置为正确的端口.每当我调试时,据我所知,所有变量都是正确的并且与正确的端口相关联.Transport 内部有什么我想念的东西吗?

                  I'm kind of stumped as to why. Every time a new test email is sent an entirely new SendMailUsingAuthentication object is created. Within that class the properties are always reset to the proper port. Whenever I debug, as far as I can see, all variables are correct and associated with the correct port. Is there something going on inside of Transport that I'm missing?

                  在前端 GUI 中:

                  private void testButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
                  
                      int port = port25RadioButton.isSelected() ? PORT_25 : PORT_587;
                      notifier = new SendMailUsingAuthentication(hostNameTextField.getText(),
                              userTextField.getText(), getPassword(), emailTextField.getText().split(","),port);
                  
                  
                      Thread wait = new Thread(new Runnable() {
                  
                          public void run() {
                              try {
                                  changeStatusText("Sending test email...");
                                  notifier.postTestMail();
                                  changeStatusText("Test email sent.");
                              } catch (AddressException ex) {
                                  changeStatusText("Error.  Invalid email address name.");
                              } catch (MessagingException ex) {
                                  changeStatusText("SMTP host connection refused.");
                                  System.err.println(ex.getMessage());
                              } catch (Exception ex) {
                                  System.err.println(ex);
                              }
                          }
                      });
                  
                      wait.start();
                  }
                  

                  在电子邮件发件人类中:

                  In the email sender class:

                  public void postTestMail() throws MessagingException, AddressException{
                      String[] testReciever = new String[1];
                      testReciever[0] = emailList[0];
                      postMail(testReciever, "Test email.", "Your email settings are successfully set up.", emailFromAddress);
                  }
                  
                  private void postMail(String recipients[], String subject,
                          String message, String from) throws MessagingException, AddressException {
                  
                      //Set the host smtp address
                      Properties props = new Properties();
                      props.put("mail.smtp.port", smtpPort);
                      props.put("mail.smtp.host", smtpHostName);
                      props.put("mail.smtp.auth", "true");
                      props.put("mail.smtp.starttls.enable", true);
                      Authenticator auth = new SMTPAuthenticator();
                      Session session = Session.getDefaultInstance(props, auth);
                      session.setDebug(false);
                  
                      // create a message
                      Message msg = new MimeMessage(session);
                  
                      // set the from and to address
                      InternetAddress addressFrom = new InternetAddress(from);
                      msg.setFrom(addressFrom);
                  
                      InternetAddress[] addressTo = new InternetAddress[recipients.length];
                      for (int i = 0; i < recipients.length; i++) {
                          addressTo[i] = new InternetAddress(recipients[i]);
                      }
                      msg.setRecipients(Message.RecipientType.TO, addressTo);
                  
                      // Setting the Subject and Content Type
                      msg.setSubject(subject);
                      msg.setContent(message, "text/plain");
                      Transport.send(msg);
                  }
                  

                  推荐答案

                  发生这种情况是因为您使用的是 getDefaultInstance() 其中 说:

                  This happens because you're using getDefaultInstance() which says:

                  获取默认的 Session 对象.如果尚未设置默认值,则会创建一个新的 Session 对象并将其安装为默认值.

                  Get the default Session object. If a default has not yet been setup, a new Session object is created and installed as the default.

                  并且 Properties 参数仅在创建新的 Session 对象时使用."

                  And that the Properties argument is "used only if a new Session object is created."

                  所以当您第一次调用 getDefaultInstance 时,它会使用您指定的端口.之后,Session 已经创建,随后对 getDefaultInstance 的调用将返回相同的会话,并忽略更改的属性.

                  So the first time you invoke getDefaultInstance it uses your specified port. After that, the Session has already been created, and subsequent calls to getDefaultInstance will return that same session, and ignore the changed properties.

                  尝试使用 Session.getInstance() 而不是 getDefaultInstance(),每次都会使用提供的属性创建一个新的 Session.

                  Try using Session.getInstance() instead of getDefaultInstance(), which creates a new Session each time, using the supplied properties.

                  仔细阅读 javadocs 是值得的.

                  It pays to read the javadocs very carefully.

                  这篇关于如何更改 JavaMail 端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Compiling C++ for the JVM(为 JVM 编译 C++)
                  Compile to java bytecode (without using Java)(编译成java字节码(不使用Java))
                  How to drive C#, C++ or Java compiler to compute 1+2+3+...+1000 at compile time?(如何在编译时驱动 C#、C++ 或 Java 编译器计算 1+2+3+...+1000?)
                  Java ClassLoader: load same class twice(Java ClassLoader:两次加载相同的类)
                  How to debug .class files in ECLIPSE?(如何在 ECLIPSE 中调试 .class 文件?)
                  Java quot;The blank final field may not have been initializedquot; Anonymous Interface vs Lambda Expression(Java“可能尚未初始化空白的最终字段匿名接口与 Lambda 表达式)
                  <i id='WCzPT'><tr id='WCzPT'><dt id='WCzPT'><q id='WCzPT'><span id='WCzPT'><b id='WCzPT'><form id='WCzPT'><ins id='WCzPT'></ins><ul id='WCzPT'></ul><sub id='WCzPT'></sub></form><legend id='WCzPT'></legend><bdo id='WCzPT'><pre id='WCzPT'><center id='WCzPT'></center></pre></bdo></b><th id='WCzPT'></th></span></q></dt></tr></i><div id='WCzPT'><tfoot id='WCzPT'></tfoot><dl id='WCzPT'><fieldset id='WCzPT'></fieldset></dl></div>
                  <legend id='WCzPT'><style id='WCzPT'><dir id='WCzPT'><q id='WCzPT'></q></dir></style></legend>
                    <tbody id='WCzPT'></tbody>

                    <tfoot id='WCzPT'></tfoot>
                  • <small id='WCzPT'></small><noframes id='WCzPT'>

                          • <bdo id='WCzPT'></bdo><ul id='WCzPT'></ul>