2016-10-19 2 views
0

следующий код для отправки электронной почты через наш собственный сервер дает AuthenticationFailedExceptionAuthenticationFailedException: не удалось подключиться ошибка при отправке электронной почты

String to = "[email protected]"; 
String from = "[email protected]"; 
String host = "Mail.abc.co.in"; 
String message= null; 
Properties props = System.getProperties(); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", host); 
props.put("mail.smtp.port", "587"); 
Session session = Session.getDefaultInstance(props); 
InternetAddress fromAddress = null; 
InternetAddress toAddress = null; 
try { 
    Message simpleMessage = new MimeMessage(session); 
    fromAddress = new InternetAddress(from); 
    toAddress = new InternetAddress(to); 
    simpleMessage.setFrom(fromAddress); 
    simpleMessage.setRecipient(RecipientType.TO, toAddress); 
    simpleMessage.setSubject("-------------"); 
    simpleMessage.setContent(message, "text/html"); 
    Transport trans = session.getTransport("smtp"); 
    trans.connect("Mail.abc.co.in", 587, "[email protected]", "password"); 
    trans.sendMessage(simpleMessage, simpleMessage.getAllRecipients());      
} catch (MessagingException e) { 
    e.printStackTrace(); 
} 

помощь в Выяснить проблемы Спасибо advanve

ответ

0

Что SMTP сервер вы используете ?

TSL/SSL требуется на некоторых серверах (например, в Google smtp) Попробуйте настроить авторизацию через ssl.

Вот мой вариант подключения Google SMTP через SSL:

private void configSession() throws IOException { 

    // Configuring smtp 
    Properties properties = new Properties(); 
    properties.put("mail.smtp.host", smtpHost); 
    properties.put("mail.smtp.socketFactory.port", smtpSocketFactoryPort); 
    properties.put("mail.smtp.socketFactory.class", smtpSocketFactoryClass); 
    properties.put("mail.smtp.auth", smtpAuth); 
    properties.put("mail.smtp.port", smtpPort); 

    session = Session.getInstance(properties, new MailAuthenticator(username, password)); 
} 

private class MailAuthenticator extends Authenticator { 
    private String username; 
    private String password; 

    MailAuthenticator(String username, String password) { 
     this.username = username; 
     this.password = password; 
    } 

    @Override 
    protected PasswordAuthentication getPasswordAuthentication() { 
     return new PasswordAuthentication(username, password); 
    } 
} 

public void send(){ 
    try { 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(username)); 
     message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email)); 
     message.setSubject(messageTitle); 
     message.setText("Hello"); 
     Transport.send(message); 
    } catch (Exception e) { 
     logger.error("Error sending email:", e); 
    } 

И свойства:

mail.smtp.host=smtp.gmail.com 
mail.smtp.socketFactory.port=465 
mail.smtp.socketFactory.class=javax.net.ssl.SSLSocketFactory 
mail.smtp.auth=true 
mail.smtp.port=465 
+0

Но у меня есть свой собственный сервер рассылки и я хочу сделать почту, используя только этот сервер. – Betty

+0

@Betty Также я заметил, что вы используете экземпляр по умолчанию 'Session' без' Authentificator'. Лучше обновите свой вопрос и укажите, что вы используете свой собственный сервер. –

Смежные вопросы