2016-10-19 11 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を使用して認証を設定してください。

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); 
    } 

とプロパティ:

は、ここに私のSSL経由でのGoogleのSMTP接続のバージョンである

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また、 'Authentificator'なしで' Session'のデフォルトインスタンスを使用することに気付きました。あなたの質問をよりよく更新して、自分のサーバーを使用する –

関連する問題