Spring Boot 2.0本地发送邮件失败求助(Mac/Linux环境)
Let me walk you through why you're hitting that connection refused error and how to get your notification system up and running properly using your Mac/Linux's built-in sendmail.
Why the Connection Refused Error Happens
Your current configuration tells Spring Boot to connect to localhost:25 for SMTP, but here's the catch: the default sendmail installation on Mac/Linux doesn't run as an SMTP server listening on port 25. Sendmail is a Mail Transfer Agent (MTA) that works as a command-line tool or local delivery agent, not a network-facing SMTP service. So when JavaMailSender tries to connect to localhost:25, there's nothing listening there—hence the connection refusal.
Solution 1: Directly Use Sendmail Command (Recommended)
Instead of trying to connect to an SMTP port, configure Spring Boot to call the sendmail executable directly. This leverages your existing sendmail setup without needing to run an SMTP server.
Update Your application.properties
Replace your current config with this:
spring.mail.host=localhost spring.mail.port=25 spring.mail.protocol=smtp # Critical: Point JavaMail to your sendmail executable spring.mail.properties.mail.smtp.sendmail=/usr/sbin/sendmail spring.mail.properties.mail.smtp.sendmail.args=-i spring.mail.properties.mail.smtp.auth=false spring.mail.properties.mail.smtp.starttls.enable=false spring.mail.properties.mail.smtp.connectiontimeout=5000 spring.mail.properties.mail.smtp.timeout=3000 spring.mail.properties.mail.smtp.writetimeout=5000
The mail.smtp.sendmail and sendmail.args=-i settings tell JavaMail to bypass SMTP connection and use the sendmail command directly.
Example Email Service Code
Here's a simple service to send to multiple Gmail addresses:
@Service public class NotificationService { private final JavaMailSender mailSender; @Autowired public NotificationService(JavaMailSender mailSender) { this.mailSender = mailSender; } public void sendBulkNotification(List<String> recipientEmails, String subject, String messageBody) throws MessagingException { MimeMessage mimeMessage = mailSender.createMimeMessage(); MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true); helper.setFrom("your-notifier@example.com"); // Use a valid-looking sender address to avoid Gmail spam filters helper.setTo(recipientEmails.toArray(new String[0])); helper.setSubject(subject); helper.setText(messageBody, false); // Set to true if you need HTML content mailSender.send(mimeMessage); } }
Solution 2: Run a Local SMTP Server (If You Prefer SMTP Connection)
If you really want to use the SMTP approach, you'll need to start an SMTP server on your localhost port 25. Here's how to do it for Mac and Linux:
For Linux (Ubuntu/Debian)
- Install Postfix (a lightweight MTA):
sudo apt-get update && sudo apt-get install postfix - During setup, choose "Local only" or "Internet Site" (depending on your needs) and follow the prompts.
- Start and enable the service:
sudo systemctl start postfix sudo systemctl enable postfix
For Mac
- Enable the built-in Postfix service:
sudo postfix start - To make it start automatically on boot:
sudo launchctl load -w /System/Library/LaunchDaemons/com.apple.postfix.master.plist
Once the SMTP server is running, your original application.properties should work as expected.
Quick Test to Validate Sendmail
Before firing up Spring Boot, test if sendmail can send emails directly from the command line:
echo "Subject: Test Notification\n\nThis is a test from sendmail on your local machine" | /usr/sbin/sendmail -i your-gmail-address@gmail.com
If this arrives in your Gmail (check spam folder if needed), your Spring Boot setup will work too.
Pro Tip for Gmail Delivery
Gmail is strict about spam filters, so:
- Use a sender address with a valid domain (even a fake one like
notifier@yourdomain.comhelps) - Add the sender address to your Gmail's "Trusted senders" list to avoid it being marked as spam
内容的提问来源于stack exchange,提问作者gangkui2008

