javax.mail.AuthenticationFailedException: failed to connect, no password specified?

前端 未结 14 1954
广开言路
广开言路 2020-12-17 10:46

This program attempts to send e-mail but throws a run time exception:

javax.mail.AuthenticationFailedException: failed to connect, no password specified?
         


        
14条回答
  •  北海茫月
    2020-12-17 11:14

    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    import com.opensymphony.xwork2.ActionSupport;
    import com.opensymphony.xwork2.ModelDriven;
    
    @SuppressWarnings("serial")
    public class RegisterAction {
    
    
        public String execute() {
    
    
             RegisterAction mailBean = new RegisterAction();
    
               String subject="Your username & password ";
    
               String message="Hi," + username;
              message+="\n \n Your username is " + email;
              message+="\n \n Your password is " + password;
              message+="\n \n Please login to the web site with your username and password.";
              message+="\n \n Thanks";
              message+="\n \n \n Regards";
    
               //Getting  FROM_MAIL
    
               String[] recipients = new String[1];
                recipients[0] = new String();
                recipients[0] = customer.getEmail();
    
               try{
              mailBean.sendMail(recipients,subject,message);
    
              return "success";
              }catch(Exception e){
               System.out.println("Error in sending mail:"+e);
              }
    
            return "failure";
        }
    
        public void sendMail( String recipients[ ], String subject, String message)
                 throws MessagingException
                  {
                    boolean debug = false;
    
                     //Set the host smtp address
    
                     Properties props = new Properties();
                     props.put("mail.smtp.host", "smtp.gmail.com");
                     props.put("mail.smtp.starttls.enable", true);
                     props.put("mail.smtp.auth", true);
    
                    // create some properties and get the default Session
    
                    Session session = Session.getDefaultInstance(props, new Authenticator() {
    
                        protected PasswordAuthentication getPasswordAuthentication() {
                            return new PasswordAuthentication(
                                    "username@gmail.com", "5373273437543");// Specify the Username and the PassWord
                        }
    
                    });
                    session.setDebug(debug);
    
    
                    // create a message
                    Message msg = new MimeMessage(session);
    
    
                    InternetAddress[] addressTo = new InternetAddress[recipients.length];
                    for (int i = 0; i < recipients.length; i++)
                    {
                      addressTo[i] = new InternetAddress(recipients[i]);
                    }
    
                    msg.setRecipients(Message.RecipientType.TO, addressTo);
    
                    // Optional : You can also set your custom headers  in the Email if you Want
                    //msg.addHeader("MyHeaderName", "myHeaderValue");
    
                    // Setting the Subject and Content Type
                    msg.setSubject(subject);
                    msg.setContent(message, "text/plain");
    
                    //send message
                    Transport.send(msg);
    
                    System.out.println("Message Sent Successfully");
                  }
    
    }
    

提交回复
热议问题