简体   繁体   中英

error while sending email in Java using Apache Commons email libs

I was trying to use Apache Commons library to send email. But it gives me an error saying

"the type of setAuthenticator(Authenticator) is erroneous" in email.setAuthenticator function.

Here is my code. I am not sure why I get this error.

public static void sendSimpleMail() throws Exception {
    Email email = new SimpleEmail();
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator("me@gmail.com","my gmail password"));
    email.setDebug(false);
    email.setHostName("smtp.gmail.com");
    email.setFrom("me@gmail.com");
    email.setSubject("Hi");
    email.setMsg("This is a test mail ... :-)");
    email.addTo("you@gmail.com");
    email.setTLS(true);
    email.send();
    System.out.println("Mail sent!");
}

您可以尝试对身份验证数据使用其他方法来直接接收用户名和密码,然后将通过commons-email在内部处理Authenticator:

email.setAuthentication("me@gmail.com","my gmail password")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM