Java传输邮件发件人错误

时间:2017-06-28 11:32:15

标签: java mailer transport

我需要在我的JAVA项目上保留一个对象后发送电子邮件,我正在使用此代码

   public static void sendMail(String protocol, String host, String port, final String userName, final String password, 
           String subject, byte[] content, String para, String cc, String co) {
       Properties props = getServerProperties(protocol, host, port);

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

       try {
             final Message message = new MimeMessage(session);
             message.setFrom(new InternetAddress(userName));
             message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(para));
             message.setRecipients(Message.RecipientType.CC, InternetAddress.parse(cc));
             message.setRecipients(Message.RecipientType.BCC, InternetAddress.parse(co));
             message.setSubject(subject);
             message.setText(content.toString());
             Transport.send(message);
       } catch (MessagingException e) {
             throw new RuntimeException(e);
       }
    }

   private static Properties getServerProperties(String protocol, String host, String port) {
      Properties properties = new Properties();
      properties.put(String.format("mail.%s.host", protocol), host);
      properties.put(String.format("mail.%s.port", protocol), port);
      properties.setProperty(String.format("mail.%s.socketFactory.class", protocol), "javax.net.ssl.SSLSocketFactory");
      properties.setProperty(String.format("mail.%s.socketFactory.fallback", protocol), "false");
      properties.setProperty(String.format("mail.%s.socketFactory.port", protocol), String.valueOf(port));
      return properties;
   }

但是当我拨打 Transport.send(消息); 时会返回错误消息

无法进行API调用mail.Send在既不是原始请求线程也不是ThreadManager创建的线程的线程中

我在邮件上做错了什么?

0 个答案:

没有答案