如何从具有代理以访问互联网的VM的Java中发送邮件

时间:2018-10-10 11:56:27

标签: proxy smtp virtual-machine javamail starttls

我正在使用端口25和主机smtp.gmail.com。我已经将starttls属性设置为true。但我收到错误,无法与主机连接,端口smtp.gmail.com 25,超时-1

防火墙和网络中均允许使用端口。

根据https://javaee.github.io/javamail/FAQ#proxy邮件可以使用来自Javax版本1.6.0的HTTP代理发送,我使用的是1.6.2

到目前为止,我的代码是

    String to = "abc@corporate.com";
    String subject = "Test subject";
    String msg ="email text";
    final String from ="abc@corporate.com";
    final  String password ="password";
    Properties props = new Properties();
    props.put("mail.smtp.proxy.host", "xx.xxx.xx.xx");
    props.put("mail.smtp.proxy.port", "xxxx");
    props.setProperty("mail.host", "smtp.gmail.com");
    props.put("mail.smtp.auth", "true");  
    props.put("mail.smtp.port", "25");  
    props.put("mail.debug", "true");  
    props.put("mail.smtp.starttls.enable","true");
    Session session = Session.getDefaultInstance(props,  
    new javax.mail.Authenticator() {
       protected PasswordAuthentication getPasswordAuthentication() {  
       return new PasswordAuthentication(from,password);  
   }  
   });  
   Transport transport = session.getTransport();  
   InternetAddress addressFrom = new InternetAddress(from);  

   MimeMessage message = new MimeMessage(session);  
   message.setFrom(addressFrom);  
   message.setSubject(subject);  
   message.setContent(msg, "text/plain");  
   message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));  

   transport.connect();  
   Transport.send(message);  
   transport.close();
   }

0 个答案:

没有答案