Java Mail API: Difference between revisions

From Chorke Wiki
Jump to navigation Jump to search
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
==Mail Config==
==Mail Config==
<source lang="java" highlight="27-29">
<syntaxhighlight lang="java" highlight="27-29">
import java.util.Properties;
import java.util.Properties;


Line 38: Line 38:
     }
     }
}
}
</source>
</syntaxhighlight>
 
==Properties==
<syntaxhighlight lang="properties">
chorke.notify.email.smtp.host: smtp.gmail.com
chorke.notify.email.smtp.port: 587
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: false
</syntaxhighlight>
 
<syntaxhighlight lang="properties">
chorke.notify.email.smtp.host: mail.chorke.org
chorke.notify.email.smtp.port: 587
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: false
</syntaxhighlight>
 
<syntaxhighlight lang="properties">
chorke.notify.email.smtp.host: mail.chorke.org
chorke.notify.email.smtp.port: 465
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: true
</syntaxhighlight>


==Knowledge==
==Knowledge==
<source lang="bash">
<syntaxhighlight lang="bash">
# debugging certificate handshacking
# debugging certificate handshacking
service='api.chorke.org:5443/soap/services';\
service='api.chorke.org:5443/soap/services';\
echo -e "GET / HTTP/1.0\r\n" | openssl s_client \
echo -e "GET / HTTP/1.0\r\n" | openssl s_client \
-connect $service -CAfile chorke_client.pem
-connect $service -CAfile chorke_client.pem
</source>
</syntaxhighlight>


  openssl help
  openssl help
Line 64: Line 101:
{|
{|
|valign="top"|
|valign="top"|
* [https://stackoverflow.com/questions/60654561/ Java Mail cannot connect to SMTP using TLS/SSL]
* [https://letsencrypt.org/certificates/ Let’s Encrypt Chain of Trust]
* [https://letsencrypt.org/certificates/ Let’s Encrypt Chain of Trust]
* [[Java Key Store|Security » Java » Key Store]]
* [[Security/Certificate|Security » Certificate]]
* [[Security/Password|Security » Password]]
* [https://eclipse-ee4j.github.io/mail/ Jakarta Mail API]
* [https://eclipse-ee4j.github.io/mail/ Jakarta Mail API]
* [[Java/Security|Security » Java]]
* [[Java Key Store]]
* [[Java Key Store]]
* [[Java/Security]]
* [https://javaee.github.io/javamail/ Java Mail API]
* [https://javaee.github.io/javamail/ Java Mail API]
* [[Java]]
* [[Java]]

Latest revision as of 02:21, 27 May 2025

Mail Config

import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;

@Configuration
public class MailConfig {

    @Bean
    public JavaMailSender getMailSender() {
        JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
        mailSender.setHost("mail.chorke.org");
        mailSender.setPort(465);

        if(!"noreply@chorke.org".isEmpty()) {
            mailSender.setUsername("noreply@chorke.org");
            mailSender.setPassword("p@$$w0rd");
        }

        Properties javaMailProperties = new Properties();
        javaMailProperties.put("mail.smtp.auth", "noreply@chorke.org".isEmpty() ?  "false" : "true");
        javaMailProperties.put("mail.smtp.ehlo", "true");

        javaMailProperties.put("mail.smtp.ssl.trust", "mail.chorke.org");
        javaMailProperties.put("mail.smtp.starttls.enable", "true");
        javaMailProperties.put("mail.smtp.ssl.enable", "true");

        javaMailProperties.put("mail.transport.protocol", "smtp");
        javaMailProperties.put("mail.debug", "true");

        mailSender.setJavaMailProperties(javaMailProperties);
        return mailSender;
    }
}

Properties

chorke.notify.email.smtp.host: smtp.gmail.com
chorke.notify.email.smtp.port: 587
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: false
chorke.notify.email.smtp.host: mail.chorke.org
chorke.notify.email.smtp.port: 587
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: false
chorke.notify.email.smtp.host: mail.chorke.org
chorke.notify.email.smtp.port: 465
chorke.notify.email.smtp.username: no-reply@chorke.org
chorke.notify.email.smtp.password: p@$$w0rd
chorke.notify.email.smtp.alias: no-reply@chorke.org
chorke.notify.email.smtp.starttls: true
chorke.notify.email.smtp.debug: false
chorke.notify.email.smtp.ehlo: true
chorke.notify.email.smtp.ssl: true

Knowledge

# debugging certificate handshacking
service='api.chorke.org:5443/soap/services';\
echo -e "GET / HTTP/1.0\r\n" | openssl s_client \
-connect $service -CAfile chorke_client.pem
openssl help
openssl help pkcs12
keytool --help -importkeystore
openssl s_client -connect mail.chorke.org:465 -state
sudo keytool -import -trustcacerts -alias letsencrypt_r3 -file lets-encrypt-r3.der -keystore\
 /usr/lib/jvm/java-8-openjdk-amd64/jre/lib/security/cacerts

sudo keytool -import -trustcacerts -alias letsencrypt_r3 -file lets-encrypt-r3.der -keystore\
 /etc/ssl/certs/java/cacerts

Enter keystore password: changeit

References