Run Local SMTP Server on your development environment.
Your code needs to run the test cases with send email from SMTP server but you have no SMTP Mail Server on your local and you get the error below:
> :bootRun
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused (Connection refused). Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: localhost, 25; timeout -1;
nested exception is:
java.net.ConnectException: Connection refused (Connection refused)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:448)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:356)
Option 1: Via Docker
- Install Docker If you not yet.
- Register a Docker ID
https://hub.docker.com/ if you don't have.
- Login Docker Hub.
- Skip this step if Docker already installed on your Mac/PC and you already logged in Docker Hub.
Pull and run djfarrelly/maildev Image from Docker Hub.
- Search for "djfarrelly/maildev" in Docker Hub and Checkout the document for more details.
- Run the command below to starting a Local SMTP Mail Server Instance as a Docker container
named "MailDev" and
mapping port 25(SMTP) and 80(Web) from the container to port 25 and 1080 on your Docker host (your PC/Mac). This will take you a few minutes to download the image and Starting the container.
docker run -d -it --name MailDev -p 1080:80 -p 25:25 djfarrelly/maildev
- Now re-run your test cases again:
2019-11-23 23:26:43.499 DEBUG 7573 --- [sbrgprjs-task-1] me.koacervate.aop.logging.LoggingAspect : Enter: me.koacervate.service.MailService.sendCreationEmail() with argument[s] = [User{login='koacervate', firstName='Khue', lastName='Duke', email='koacervate@googlemail.com', imageUrl='null', activated='true', langKey='en', activationKey='null'}]
2019-11-23 23:26:43.500 DEBUG 7573 --- [sbrgprjs-task-1] me.koacervate.service.MailService : Sending creation email to 'koacervate@googlemail.com'
2019-11-23 23:26:43.526 DEBUG 7573 --- [sbrgprjs-task-1] me.koacervate.service.MailService : Send email[multipart 'false' and html 'true'] to 'koacervate@googlemail.com' with subject 'jsbrgprjs account activation' and content=<!DOCTYPE html>
<html lang="en">
<head>
<title>jsbrgprjs account activation</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="shortcut icon" href="http://127.0.0.1:8080/favicon.ico" />
</head>
<body>
<p>Dear koacervate</p>
<p>Your jsbrgprjs account has been created, please click on the URL below to access it:</p>
<p>
<a href="http://127.0.0.1:8080/account/reset/finish?key=RCTVtLkilrCdawcCDYKf">http://127.0.0.1:8080/account/reset/finish?key=RCTVtLkilrCdawcCDYKf</a>
</p>
<p>
<span>Regards,</span>
<br/>
<em>jsbrgprjs Team.</em>
</p>
</body>
</html>
2019-11-23 23:26:44.073 DEBUG 7573 --- [sbrgprjs-task-1] me.koacervate.service.MailService : Sent email to User 'koacervate@googlemail.com'
2019-11-23 23:26:44.073 DEBUG 7573 --- [sbrgprjs-task-1] me.koacervate.aop.logging.LoggingAspect : Exit: me.koacervate.service.MailService.sendCreationEmail() with result = null
- You can see the sent email in Web UI
http://localhost:1080/#/email/JevGMzCd
Option 2: Via install maildev node package as below:
$ npm install -g maildev
$ maildev
with options as below:
Usage
maildev [options]
-h, --help output usage information
-V, --version output the version number
-s, --smtp <port> SMTP port to catch emails [1025]
-w, --web <port> Port to run the Web GUI [1080]
--ip <ip address> IP Address to bind SMTP service to
--outgoing-host <host> SMTP host for outgoing emails
--outgoing-port <port> SMTP port for outgoing emails
--outgoing-user <user> SMTP user for outgoing emails
--outgoing-pass <password> SMTP password for outgoing emails
--outgoing-secure Use SMTP SSL for outgoing emails
--auto-relay [email] Use auto-relay mode. Optional relay email address
--auto-relay-rules <file> Filter rules for auto relay mode
--incoming-user <user> SMTP user for incoming emails
--incoming-pass <pass> SMTP password for incoming emails
--web-ip <ip address> IP Address to bind HTTP service to, defaults to --ip
--web-user <user> HTTP user for GUI
--web-pass <password> HTTP password for GUI
--base-pathname <path> base path for URLs
--disable-web Disable the use of the web interface. Useful for unit testing
--hide-extensions <extensions> Comma separated list of SMTP extensions to NOT advertise
(STARTTLS, SMTPUTF8, PIPELINING, 8BITMIME)
-o, --open Open the Web GUI after startup
-v, --verbose
--silent
Please refer to documents at
https://hub.docker.com/r/djfarrelly/maildev/ for more details.
You can register a free account for testing and choose to integrate via API or SMTP then follow the instructions in the website you will have all necessary information to send emails with your code.
mail:
host: smtp.sendgrid.net
port: 465
username: apikey
password: SG.t2L6qpv3TjmvgcasoksVbg.DRsZrTwq3LmApU1uaQ6mjv-koafl0NaSzogCW6X9***
properties:
mail:
smtp:
auth: true
starttls:
enable: true
References:
- https://docs.docker.com
-
https://hub.docker.com/r/djfarrelly/maildev/
-
https://github.com/maildev/maildev