2017-07-25 30 views
0

を使用してemailを送信しようとしています。接続に失敗し、パスワードは、私が正しい認証用の電子メールのIDとパスワードを渡したときjava/servletでメールを送信する際にエラーが発生しました。

は、なぜ私はこのエラーを取得しています指定されていません。しかし、私は

javax.mail.AuthenticationFailedExceptionの下のようなエラーが直面しています?

これはJavaMailのAuthenticatorのがのjavax.mailパッケージで見つかった、同じ名前のjava.netクラスから異なっている

import java.io.IOException; 
import java.net.PasswordAuthentication; 
import java.util.Properties; 

import javax.mail.Authenticator; 
import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 
import javax.servlet.ServletException; 
import javax.servlet.annotation.WebServlet; 
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse; 

/** 
* Servlet implementation class TestMail 
*/ 
@WebServlet("/TestMail") 
public class TestMail extends HttpServlet { 
    private static final long serialVersionUID = 1L; 


    public TestMail() { 
     super(); 

    } 

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     todo(request,response); 

    } 


    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     todo(request,response); 

    } 

    private void todo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { 
     // TODO Auto-generated method stub 
     request.setCharacterEncoding("utf8"); 
     response.setCharacterEncoding("utf8"); 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable","true"); 
     props.put("mail.smtp.host","smtp.gmail.com"); 
     props.put("mail.smtp.post","587"); 



     Session session = Session.getDefaultInstance(props, 
       new Authenticator() { 
        protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(
           "[email protected]", "testing123"); 
          } 
       }); 

     Message message=new MimeMessage(session); 
     try { 
      message.setFrom(new InternetAddress("[email protected]","hello")); 

     message.setRecipients(Message.RecipientType.TO,InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Email"); 
     message.setText("hello this is testing mail \n \n Congrets"); 
     Transport.send(message); 
     System.out.println("Mail Sent Successfully"); 
     } catch (MessagingException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

    } 

} 

my error image

+0

[JavaMail FAQ](https://javaee.github.io/javamail/FAQ#commonmistakes)で説明されているように、よくある間違いです。 –

+0

okkそれは今、あなたに感謝@ビルシャノン –

答えて

0

私のコードです。これらの2つは、JavaMail APIがJava 1.1で動作するのと同じオーセンティケータを共有しません。最後に、

Properties props = new Properties(); 

    props.put("mail.smtp.host",     <mail_server>); 
    props.put("mail.smtp.port",     <port>); 
    props.put("mail.smtp.user",     <user>); 
    props.put("mail.smtp.password",    <pw>); 
    props.put("mail.smtp.auth",     <logon>); 
    props.put("mail.from",      <from>); 

    Session session = Session.getInstance(props, null); 

    try { 
     MimeMessage msg = new MimeMessage(session); 
     msg.setFrom(); 
     msg.setRecipients(Message.RecipientType.TO, <email_to>); 
     msg.setRecipients(Message.RecipientType.CC, <email_cc>); 
     msg.setRecipients(Message.RecipientType.BCC, <email_bcc>); 
     msg.setSubject(x_subject, Globals.q_UNICODE_MAIL); 
     msg.setSentDate(new Date()); 
     msg.setHeader("Disposition-Notification-To", x_from); 
    // 
    //<build your Multipart message> 
    // 

3): http://www.rgagnon.com/javadetails/java-0538.html

0

1)POP3電子メールのための適切なjarファイルを使用してください::

import java.util.Properties; 
    import javax.activation.*; 
    import javax.mail.*; 

2)そして、あなたのメッセージを構築する

はこれを参照してください。接続して送信:

 Transport transport = session.getTransport("smtp"); 
     if (<logon_required>) { 
      transport.connect(<mail_server>, <user>, <pw>); 
     } 
     transport.sendMessage(msg, msg.getAllRecipients()); 
関連する問題