2013-10-08 2 views
3

Я внедрил класс электронной почты и извлек все атрибуты из файла свойств. Вот мой код:как обращаться с прокси при работе с почтовым сервером?

static { 
     // Load the properties file 
     try { 
      properties = new Properties(); 
      InputStream inputStream = Email.class.getClassLoader() 
        .getResourceAsStream("/mail.properties"); 
      properties.load(inputStream);`enter code here` 
     } catch (Exception e) { 
      logger.error(e.getMessage()); 
     } 
    } 

    /** 
    * 
    * @param to 
    *   : mail Sent to 
    * @param from 
    *   : mail sent from 
    * @param subject 
    *   : mail's subject 
    * @param body 
    *   : mail's body 
    * @throws Exception 
    */ 
    public static void sendTextMail(String to, String from, String subject, 
      String body) throws Exception { 

     if (properties.isEmpty()) { 
      throw new Exception("Cannot send mail. Host data not available."); 
     } 

     // Authenticate the session with username and password 
     Session session = Session.getInstance(properties, new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication((String) properties 
         .get("mail.login.username"), (String) properties 
         .get("mail.login.password")); 
      } 
     }); 

     // Create to and from addresses 
     InternetAddress fromAddress = new InternetAddress(from); 
     InternetAddress toAddress = new InternetAddress(to); 

     // Create the message instance 
     // and add the sender, recipient, subject and body. 
     Message msg = new MimeMessage(session); 
     msg.setFrom(fromAddress); 
     msg.setSubject(subject); 
     msg.setRecipient(RecipientType.TO, toAddress); 
     msg.setContent(body, "text/plain"); 

     // Finally send the email 
     Transport.send(msg); 

    } 

Когда я попытался отправить почту я получил эту ошибку: 16: 48: 23882 ОШИБКА AuthorController: 1199 - не удалось подключиться к SMTP хост: smtp.gmail.com, порт: 25 Прокси блокирует мой сервер. Как преодолеть эту проблему

+1

Попробуйте использовать TLS на порту 465. Порт 25 часто блокируется из-за спамеров. – artbristol

+1

Вы уверены, что это прокси, а не просто простой брандмауэр? –

ответ

0

Большинство интернет-провайдеров блокируют исходящие соединения на порту 25. Однако вы обычно можете использовать порт 587 для неинкретированной SMTP-связи.

Чтобы изменить порт, используемый вы можете настроить свойства экземпляра вы настроите в статическом блоке:

... 
properties.load(inputStream); 
properties.put("mail.smtp.port", "587"); 
... 

После того, как вы подтвердите, что работы вы можете поставить установку в вашем /mail.properties файле:

mail.smtp.port:587 
Смежные вопросы