i want to send email from java from an unsecured address
i use in smtp
I get a login error, and you could not access the address because it is not secure
I added permission to unsecure sites, but it didn't help
I can't add a security certificate on the computer,
because the code ends up being run from another server,
and not from my computer
I have to bypass the insecure site issue only through the code
my code is:
package test;
import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.net.ssl.SSLContext;
import org.ini4j.IniPreferences;
import java.io.FileReader;
import java.security.cert.X509Certificate;
import java.util.Properties;
import java.util.prefs.Preferences;
import com.sun.mail.util.*;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
public class TestMailAuth {
public static void main(String[] args) {
String emailtext = "blabla";
String mailserver= "142.90.x.xx";
String mailPort = "25";
String emailFrom = "xxxxxx.co.il";
String emailTo = "xxxxxx.co.il";
String pass="xxxxxxxxx"
Authenticator authenticator = new Authenticator()
{
//@Override
final String smtpAuthUserName =emailFrom;
final String smtpAuthPassword = pass;
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(smtpAuthUserName, smtpAuthPassword);
}
};
Properties properties = new Properties();
try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
properties.put("mail.smtp.host", mailserver);
properties.put("mail.smtp.port", mailPort);
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.smtp.ssl.trust", "*");
properties.put("mail.smtp.ssl.socketFactory", sf);
}
catch (Exception e) {
System.out.println(e);
}
properties.put("mail.debug", "true");
Session session = Session.getInstance( properties, authenticator );
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(emailFrom));
InternetAddress[] to = {new InternetAddress(emailTo)};
message.setRecipients(Message.RecipientType.TO, to);
message.setSubject("test_SUBJECT");
message.setText(emailtext);
Transport.send(message);
System.out.println("end. Success");
}
catch (MessagingException exception)
{
exception.printStackTrace();
}
}
}
my eror:
javax.mail.MessagingException: Can't send command to SMTP host;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1365)
at com.sun.mail.smtp.SMTPTransport.sendCommand(SMTPTransport.java:1353)
at com.sun.mail.smtp.SMTPTransport.ehlo(SMTPTransport.java:794)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:336)
at javax.mail.Service.connect(Service.java:258)
at javax.mail.Service.connect(Service.java:137)
at javax.mail.Service.connect(Service.java:86)
at javax.mail.Transport.send0(Transport.java:150)
at javax.mail.Transport.send(Transport.java:80)
at test.TestMailAuth.main(TestMailAuth.java:135)
I added debug and this is the result:
DEBUG: JavaMail version 1.3.3ea
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_331\lib\javamail.providers (The system cannot find the file specified)
DEBUG: URL jar:file:/C:/xxx/jars/pop3.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/xxx/jars/pop3.jar!/META-INF/javamail.providers
DEBUG: URL jar:file:/C:/xxx/jars/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: jar:file:/C:/xxx/jars/smtp.jar!/META-INF/javamail.providers
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Sun Microsystems, Inc], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Sun Microsy stems, Inc], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Sun Microsystems, Inc]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: URL jar:file:/C:/xxx/jars/smtp.jar!/META-INF/javamail.address.map
DEBUG: successfully loaded resource: jar:file:/C:/xxx/jars/smtp.jar!/META-INF/javamail.address.map
DEBUG: java.io.FileNotFoundException: C:\Program Files\Java\jre1.8.0_331\lib\javamail.address.map (The system cannot find the file specified)
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "xxx.xx.x.xx", port 25, isSSL false
220 Ex2016.xxxx.co.il Microsoft ESMTP MAIL Service ready at Sun, 11 Jun 2023 14:08:57 +0300
DEBUG SMTP: connected to host "xxx.xx.x.xx", port: 25
EHLO xxxx
250-Ex2016.xxx.co.il Hello
250-SIZE 104857600
250-PIPELINING
250-DSN
250-ENHANCEDSTATUSCODES
250-STARTTLS
250-X-ANONYMOUSTLS
250-AUTH NTLM
250-X-EXPS GSSAPI NTLM
250-8BITMIME
250-BINARYMIME
250-CHUNKING
250 XRDST
DEBUG SMTP: Found extension "SIZE", arg "104857600"
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "DSN", arg ""
DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "X-ANONYMOUSTLS", arg ""
DEBUG SMTP: Found extension "AUTH", arg "NTLM"
DEBUG SMTP: Found extension "X-EXPS", arg "GSSAPI NTLM"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "BINARYMIME", arg ""
DEBUG SMTP: Found extension "CHUNKING", arg ""
DEBUG SMTP: Found extension "XRDST", arg ""
STARTTLS
220 2.0.0 SMTP server ready
EHLO xxxxx
I searched a lot of guides, some said to try to change to:
properties.put("mail.smtp.starttls.enable", "false");
but i get eror :
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.57 SMTP; Client was not authenticated to send anonymous mail during MAIL FROM
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1333)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:906)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:535)
at javax.mail.Transport.send0(Transport.java:151)
at javax.mail.Transport.send(Transport.java:80)
at test.TestMailAuth.main(TestMailAuth.java:135)