2017-11-24 13 views
0

「com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1クライアントが認証されていません」というエラーが表示されます。私のコードで何が間違っているのか教えてください。com.sun.mail.smtp.SMTPSendFailedException:530 5.7.1クライアントが認証されていない

Properties mailprops = new Properties(); 
mailprops.setProperty("mail.transport.protocol", "smtp"); 
mailprops.setProperty("mail.smtp.host", "MyHost"); 
mailprops.setProperty("mail.smtp.user", "UserName"); 
mailprops.setProperty("mail.smtp.password", "Password"); 

Session mailSession = Session.getDefaultInstance(mailprops, null); 
MimeMessage message = new MimeMessage(mailSession); 
message.setSubject(mySubject); 
message.addRecipient(To); 
message.addFrom(from address); 

try{ 
Transport.send(message); 
}catch (SendFailedException sfe) { 
}catch (MessagingException mex) { 
} 
+0

「クライアントが認証されていません」とは、ユーザー名やパスワードが正しくないことを意味します。彼らが正しいことを確認しましたか? Thunderbirdなどのメールクライアントを使ってメールを送信できますか?メールを送信するために正しいポートに接続していますか? –

+0

私の信任状は実際には適切です。はい、実際に他のクライアントを使用してメールを送信できます。 –

答えて

0

ようなものを試してみてください:

 Transport trans = sess.getTransport("smtp"); 
     try { 
      String host = sess.getProperty("mail.smtp.host"); 
      String port = sess.getProperty("mail.smtp.port"); 
      String user = sess.getProperty("mail.smtp.user"); 
      String password = sess.getProperty("mail.smtp.password"); 
      int prt = port == null ? -1 : Integer.parseInt(port); 
      trans.connect(host, prt, user, password); 
      trans.sendMessage(message, 
        message.getRecipients(Message.RecipientType.TO)); 
     } finally { 
      trans.close(); 
     } 

UPDATE:

というか:

int prt = port == null ? 25 : Integer.parseInt(port); 
1

が がsessionオブジェクト

を作成する Authenticatorオブジェクトを提供し、アプローチの下に試してみてくださいを
public class MailWithPasswordAuthentication { 
public static void main(String[] args) throws MessagingException { 
new MailWithPasswordAuthentication().run(); 
} 
private void run() throws MessagingException { 
Message message = new MimeMessage(getSession()); 
message.addRecipient(RecipientType.TO, new InternetAddress("[email protected]")); 
message.addFrom(new InternetAddress[] { new InternetAddress("[email protected]") }); 
message.setSubject("the subject"); 
message.setContent("the body", "text/plain"); 
Transport.send(message); 
} 
private Session getSession() { 
Authenticator authenticator = new Authenticator(); 
Properties properties = new Properties(); 
properties.setProperty("mail.smtp.submitter", authenticator.getPasswordAuthentication().getUserName()); 
properties.setProperty("mail.smtp.auth", "true"); 
properties.setProperty("mail.smtp.host", "mail.example.com"); 
properties.setProperty("mail.smtp.port", "25"); 
return Session.getInstance(properties, authenticator); 
} 
private class Authenticator extends javax.mail.Authenticator { 
private PasswordAuthentication authentication; 
public Authenticator() { 
String username = "auth-user"; 
String password = "auth-password"; 
authentication = new PasswordAuthentication(username, password); 
} 
protected PasswordAuthentication getPasswordAuthentication() { 
return authentication; 
} 
} 
} 
0
  1. 認証を有効にします。
  2. STARTTLSを有効にします。
  3. Session.getInstance引数で渡すAuthenticatorオブジェクトを作成します。

サンプルコード:

props.put( "mail.smtp.auth"、 "真"); props.put( "mail.smtp.starttls.enable"、 "true");

 Authenticator auth = new Authenticator() { 
      //override the getPasswordAuthentication method 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(fromEmail, password); 
      } 
     }; 
     Session session = Session.getInstance(props, auth); 
0

あなたはdon't need an Authenticatorと答えています。 mail.smtp.passwordプロパティはありません。 Transport.send method that takes a username and passwordに電話してください。

+0

ええ、私のSession.getDefaultInstance(mailprops、null)は、私が渡しているプロパティを設定していません。常にデフォルト値を使用しています。どうして? –

+0

[よくある間違い](https://javaee.github.io/javamail/FAQ#commonmistakes)です。 –

+0

私は間違っていますか? –

関連する問題