使用 javax 邮件和 apache commons 获取此协议错误

问题描述 投票:0回答:1

我的项目中有这个 java 类用于发送电子邮件,但无论我尝试做什么,它都会因某些协议错误而失败。

请记住,我只需要一个简单的电子邮件发件人,它用于学校项目。

这是我正在使用的课程:


import org.apache.commons.mail.*;


public class javaMail
{
    protected static String emailEmpresarial = "[email protected]";
    protected static String senha = "password";
    
    public static void sendMail(String emailCliente)
    {


        SimpleEmail email = new SimpleEmail(); 
        email.setHostName("smtp-mail.outlook.com");
        email.setSmtpPort(587);
        email.setAuthenticator(new DefaultAuthenticator(emailEmpresarial, senha));
        email.setStartTLSRequired(true);
        
        try 
        {
            
            email.setFrom(emailEmpresarial);
            email.setSubject("Test");
            email.setMsg("Success!");
            email.addTo(emailCliente);
            email.send();
            System.out.println("Enviado com sucesso!");
            
        } catch(Exception e)
        {
            e.printStackTrace();
        }
    }
}```

But this is what i'm getting:

> org.apache.commons.mail.EmailException: Sending the email to the following server failed : smtp-mail.outlook.com:587
>   at org.apache.commons.mail.Email.sendMimeMessage(Email.java:1469)
>   at org.apache.commons.mail.Email.send(Email.java:1496)
>   at internetConnectionAPIs.javaMail.sendMail(javaMail.java:36)
>   at App.main(App.java:12)
> Caused by: javax.mail.MessagingException: Could not convert socket to TLS;
> nested exception is:
>   javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)
>   at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1918)
>   at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:652)
> Caused by: javax.net.ssl.SSLHandshakeException: No appropriate protocol (protocol is disabled or cipher suites are inappropriate)


I tried to change to gmail(but it seems that it no longer works), try to change port, tried to change server, but it simply won't work, any ideas why? I'm using eclipse in focal fossa linux, and my project have javax mail, apache commons email and activation jar. 

I even tried to ChatGPT it out of this, but to no avail. I tried to check for firewall blocks but there was no problem, i was using setSSLonConnect but it was forcing the port to be 465 even tho i've set it to 587, but after removing it, same error. Gmail is not being allowed to ''lesser security apps'' now, and i can only resort to other services, if you have any idea how to help, i'll be more than glad. 

Also i'm very newbie to java and to this kind of error treating, i tried this following a youtube tutorial which was really plain and simple, and weirdly didn't need activation jar library, but my project needed it. I checked internet connection but it was working just fine. Do i need to connect eclipse to the internet or something like that? 

All i'm trying to do is just send a plain email, just that, i just need to implement an email sender for a school project, what do this no appropriate protocol means? 

Well, i don't have anything to add to it anymore, stackoverflow is still asking me to add more details, this is what i have and i simply can't work it out. 

PS: My java version is 11, could it be the issue? 
Also, please don't mind the email and password hardcoded to the program, it was meant to be just for testing. 
java eclipse apache email
1个回答
0
投票

我觉得可能是STARTTLS的问题。启动 java 时尝试此选项:-Dmail.smtp.starttls.enable=true。见http://www.oracle.com/technetwork/java/javamail/faq/index.html#hotmail

© www.soinside.com 2019 - 2024. All rights reserved.