Sendmail is an easy-to-use mail service software (Mail Transfer Agent, MTA). Many Linux distributions come with Sendmail pre-installed, allowing you to configure your server for sending and receiving emails with minimal setup. The following example uses CentOS 6.
1. Domain Name Configuration
First, you need to set up the MX record and SPF record for your domain’s DNS. A typical domain configuration looks like this:
Record Type Hostname Target
------------------------------------
A @ 123.123.123.123 (Set this to your server's IP)
CNAME www your-domain.com
CNAME SMTP your-domain.com
MX @ smtp.your-domain.com
TXT @ v=spf1 a mx ~all
The MX record is essential for mail servers, while the TXT (SPF) record helps prevent your sent emails from being flagged as spam. If you do not add an SPF record, recipient mail servers may discard your emails or move them to the spam folder. Therefore, this record is indispensable today. For more information on SPF settings, click here.
2. Configuring Sendmail
The Sendmail version used below is 8.14. The configuration process is straightforward. First, ensure that sendmail and its related services are installed on your system:
# yum install sendmail
# yum install sendmail-cf
# yum install sendmail-devel
# yum install sendmail-milter
Then, switch to the /etc/mail directory.
a. Edit the local-host-names file and add your domain name (e.g., yourdomain.com), one per line.
b. Edit sendmail.mc and remove the comments (i.e., “dnl #”) from the following two lines:
TRUST_AUTH_MECH(`EXTERNAL DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
define(`confAUTH_MECHANISMS’, `EXTERNAL GSSAPI DIGEST-MD5 CRAM-MD5 LOGIN PLAIN’)dnl
c. Edit sendmail.mc and replace “127.0.0.1” in the following line with “0.0.0.0” to allow external access to your server. If you have multiple IP addresses and want to use only one for mail services, specify that specific IP address instead.
DAEMON_OPTIONS(`Port=smtp,Addr=127.0.0.1, Name=MTA’)dnl
d. Create a local user account, such as “webmaster”, and set a password.
e. Edit the virtusertable file and add the following line:
webmaster@yourdomain.com webmaster
f. Recompile the Sendmail configuration:
# m4 sendmail.mc > sendmail.cf
# make
g. Restart the Sendmail service. Your configuration is now complete, and you can start sending emails using the mail command.
3. Configuring IMAP or POP3 Services
Although the above configuration allows you to send emails externally (via command line or programming) and receive replies, providing IMAP or POP3 services makes it easier for various email clients to send and receive mail. Dovecot is a popular service for this purpose.
a. Install the Dovecot service first:
# yum install dovecot
b. Edit Dovecot’s configuration file, typically located at /etc/dovecot.conf (some Linux distributions may place it in /etc/dovecot/conf.d/). Add the following line under the #protocols section:
protocols imap, pop3
If you do not need POP3, simply remove it.
c. Continue editing the Dovecot configuration file. Under the #mail_location section, add:
mail_location = mbox:~/mail:INBOX=/var/mail/%u
e. Since simple IMAP or POP3 configurations transmit data without encryption, some Linux distributions prohibit plaintext password logins (though web applications running on the server sending emails are not affected by this restriction). Therefore, add the following line under #disable_plaintext_auth:
disable_plaintext_auth = no
f. Restart the Dovecot service. You can now test sending and receiving emails using an email client.
4. Sending Emails from Web Applications
Here is an example of sending and receiving emails using Java. In Java, JavaMail is commonly used for this purpose, but Apache Commons Email offers a simpler alternative:
Email email = new SimpleEmail();
email.setHostName("smtp.yourdomain.com");
email.setAuthenticator(new DefaultAuthenticator("webmaster", "password"));
email.setTLS(false);
email.setFrom("webmaster@yourdomain.com");
email.setSubject("Hello");
email.setMsg("This is a test mail");
email.addTo("foo@bar.com");
email.send();
References:
1. CentOS Sendmail Roundcube Configuration Tutorial
2. Apache Commons Email