JavaMail - Sender Address rejected: Access Denied

前端 未结 1 382
你的背包
你的背包 2021-02-04 21:37

Ok, I don\'t know what else to do. This code worked perfectly fine a week ago when I wrote and tested it. Then I embedded it into my program and realised I kept getting exceptio

相关标签:
1条回答
  • 2021-02-04 21:59

    Finally figured out a work-around (though I still can't understand why there's a problem in the first place, seeing as the codes used to work. Anyways...)

    private String outgoingMailServer = "smtp.mail.yahoo.com";    
    boolean debug = false;
    
    //set the host outgoing mail smtp server.
    Properties properties = new Properties();
    properties.put("mail.smtp.host", outgoingMailServer);
    properties.put("mail.smtps.auth", "true");
    
    Authenticator authenticator = new SMTPAuthentication();
    Session session = Session.getDefaultInstance(properties, authenticator);
    session.setDebug(debug);
    
    //create a message session
    Message msg = new MimeMessage(session);
    
    //set the addresses, to and from
    InternetAddress fromAddress;
    fromAddress = new InternetAddress(emailFromAddress);
    msg.setFrom(fromAddress);
    
    //since mail can be sent to more than one recipient, create loop
    //to add all addresses into InternetAddress, addressTo.
    //InternetAddress[] toAddress = new InternetAddress[recipients.length];
    InternetAddress[] toAddress = new InternetAddress[recipients.size()];
    for (int i = 0; i < recipients.size(); i++) {
        toAddress[i] = new InternetAddress(recipients.get(i));
    }
    msg.setRecipients(Message.RecipientType.TO, toAddress);
    
    //set the subject and content type
    msg.setSubject(emailSubject);
    msg.setContent(actualMessage, "text/html; charset=utf-8");
    
    //send the email
    Transport transport = session.getTransport("smtps");
    transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    
    //email sent
    //note, this does not necessarily mean the email was delivered. The
    //sysetm has no control over that
    emailSent = true;
    

    You'll find that the main difference between the codes in the question and these ones are:

    Transport.send(msg);
    

    and

    Transport transport = session.getTransport("smtps");
    transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    

    Turns out, a Transport object had to be created and connected using the proper credentials (port number, username, password, and mail server).

    Also, I did a process of elimination and found out that as long as you have this:

    Transport transport = session.getTransport("smtps");
    transport.connect(outgoingMailServer, 465, emailUserName, emailPassword);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();
    

    you don't need this:

    Authenticator authenticator = new SMTPAuthentication();
    Session session = Session.getDefaultInstance(properties, authenticator);
    

    The above could as well be:

    Session session = Session.getDefaultInstance(properties, null);
    

    Anyway, that's the answer. You can also alter this answer for gmail. Just be sure to change the outgoing mail server to gmail's, as well as the from email address, username and password, and you'll be just fine :)

    0 讨论(0)
提交回复
热议问题