Unable to send an email using SMTP (Getting javax.mail.MessagingException: Could not convert socket to TLS;)

前端 未结 11 1863
北恋
北恋 2020-12-10 12:04

I have written the following code for sending email using javamail API through SMTP as TLS as SSL is not supported but I ended up with the following exception. Please see my

相关标签:
11条回答
  • 2020-12-10 12:27

    The stack trace reveals that the actual cause of the problem is this:

    java.security.InvalidAlgorithmParameterException: Prime size must be multiple of 64, and can only range from 512 to 1024 (inclusive)
    

    You are running into a limitation of older versions of Java that did not support DH primes longer than 1024 bits, which your SMTP server was probably requiring. Here are the relevant bug entries:

    • http://bugs.java.com/bugdatabase/view_bug.do?bug_id=6521495
    • http://bugs.java.com/bugdatabase/view_bug.do?bug_id=7044060

    This restriction/limitation was removed in Java 8 (see the release notes).

    Note that as has been pointed out already, your "fix" of disabling STARTTLS is not a real fix: It means your password will be sent as plain text, plus this will only work for SMTP servers that allow unencrypted traffic on port 587.

    0 讨论(0)
  • 2020-12-10 12:28
    session.getProperties().put("mail.smtp.starttls.enable","true");
    props.put("mail.smtp.ssl.trust", "smtp.office365.com(site where your account is)");
    props.put("mail.smtp.starttls.enable", true);
    

    These codes should be able to start a ttls communication and get the mail service running.

    In addition to this the antivirus creates a firewall that stops the handshake from happening.

    0 讨论(0)
  • 2020-12-10 12:31

    I resolved this issue by just commenting the below property

    props.put("mail.smtp.starttls.enable", "true"); 
    

    and the code got executed with no errors or warning or simply delete this line from the above source code. It is working like a charm till date.

    0 讨论(0)
  • 2020-12-10 12:33

    I resolved this issue by disabling my virus guard.

    0 讨论(0)
  • 2020-12-10 12:34

    I had a same issue with smtp.gmail.com and fixed with the following steps

    1. changed my code as per berhauz comments
    2. changed in Gmail settings from this link: https://www.google.com/settings/security/lesssecureapps
    0 讨论(0)
  • 2020-12-10 12:38

    Make sure your antivirus software is not blocking the application. In my case Avast was blocking me from sending e-mails in a Java SE application.

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