Exception to fix javax.mail.AuthenticationFailedException exception

谁都会走 提交于 2019-12-02 13:33:55

问题


I am learning how to send an email with javamail API, i have created the necessary properties and instructions to send a simple email using SMTP server, and i am using this code :

     Properties props=new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");

    Session session= Session.getDefaultInstance(props, new javax.mail.Authenticator() {

    protected PasswordAuthentication getpPasswordAuthentication(){
    return new  PasswordAuthentication("myemailadresse@gmail.com", "password");
    }


    });
    try{
        Message message=new MimeMessage(session);
        message.setFrom(new InternetAddress("myemail"));    
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recepientemailadresse"));
        message.setSubject("the java mail test");
        message.setText("Guess what brother the java mail is working correctly");
        Transport.send(message);
         JOptionPane.showMessageDialog(rootPane, "message sent");
    }
    catch(Exception e){
        JOptionPane.showMessageDialog(rootPane, e);
         e.printStackTrace();
    }

and i the run time an exception occurred mentioning that :

    javax.mail.AuthenticationFailedException: failed to connect, no password specified?
at javax.mail.Service.connect(Service.java:329)
at javax.mail.Service.connect(Service.java:176)
at javax.mail.Service.connect(Service.java:125)
at javax.mail.Transport.send0(Transport.java:194)
at javax.mail.Transport.send(Transport.java:124)
at transfer.Maitest.jButton1ActionPerformed(Maitest.java:96)
at transfer.Maitest.access$000(Maitest.java:20)
at transfer.Maitest$1.actionPerformed(Maitest.java:45)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6505)

would you tell me what did i miss please ??


回答1:


First, read this JavaMail FAQ entry about common mistakes. After correcting them, read this JavaMail FAQ entry that tells you how to connect to Gmail. If it still doesn't work, this JavaMail FAQ entry about debugging will help.




回答2:


If your email server does not require authentication and you don't want to supply a password (for example, test environment), try this:

props.put("mail.smtp.auth", "false");


来源:https://stackoverflow.com/questions/12839132/exception-to-fix-javax-mail-authenticationfailedexception-exception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!