특별한 사유가 아니라면 보통 구글이나 네이버의 SMTP를 사용 하겠지만,
나의 경우 폐쇄망에서의 메일 알림 기능(JAVA)이 동작해야 했다.
따라서 간단하게 postfix & dovecot으로 SMTP, SMTPS 서버를 구축해본 경험을 포스팅 하려고 한다.
📌postfix 란?
이메일 송수신을 위해 사용되는 프로토콜
📌dovecot 란?
이메일을 저장하고 클라이언트에게 제공하는 프로토콜
📌SASL(Simple Authentication and Security Layer) 란?
이메일 서버와 클라이언트 간, 인증을 보안하기 위해 사용되는 프레임워크(신원확인)
📌SMTP(Simple Mail Transfer Protocol) 란?
이메일을 송신하기 위한 표준 프로토콜 - 주로 25번 port 사용
📌SMTPS(Simple Mail Transfer Protocol Secure) 란?
SMTP에서 ssl 또는 tls를 통한 암호화된 프로토콜 - 주로 465번 port 사용
폐쇄망이라는 가정이므로 rpm으로 설치함(일반적으로 yum -install... 로 설치해도 무방)
rpm -ivh libicu-60.3-2.el8_1.x86_64.rpm
rpm -ivh postfix-3.5.8-1.el8.x86_64.rpm
rpm -ivh clucene-core-2.3.3.4-31.20130812.e8e3d20git.el8.x86_64.rpm
rpm -ivh dovecot-2.3.16-2.el8.x86_64.rpm
rpm -ivh net-tools-2.0-0.52.20160912git.el8.x86_64.rpm
postfix 메일 시스템의 작동을 제어하는 설정(default는 설정 선택)
!! 리눅스 명령어 TIP :
1. /[찾을 이름] 엔터 후에 n키로 찾아가기
2. :set nu로 라인 수 확인
vi /etc/postfix/main.cf
myhostname = hostname(default)
mydomain = hostname(default)
myorigin = $myhostname(default)
inet_interfaces = all
mydestination = $myhostname, localhost.$mydomain, localhost(default)
mynetworks = 0.0.0.0/0
relay_domains = $mydestination(default)
postfix 설정 파일 수정(추가)
vi /etc/postfix/main.cf
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_auth_enable = yes
smtpd_sasl_security_options = noanonymous
smtpd_relay_restrictions = permit_mynetworks, permit_sasl_authenticated, reject_unauth_destination
postfix 설정 파일 수정(주석 제거)
vi /etc/postfix/master.cf
smtp inet n - n - - smtpd
submission inet n - n - - smtpd
-o syslog_name=postfix/submission
-o smtpd_tls_security_level=encrypt
-o smtpd_sasl_auth_enable=yes
-o smtpd_tls_auth_only=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
smtps inet n - n - - smtpd
-o syslog_name=postfix/smtps
-o smtpd_tls_wrappermode=yes
-o smtpd_sasl_auth_enable=yes
-o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
-o milter_macro_daemon_name=ORIGINATING
dovecot 설정
vi /etc/dovecot/dovecot.conf
protocols = imap pop3 lmtp
listen = *, ::
SMTP 인증 메커니즘에 대한 정보 등록
vi /etc/dovecot/conf.d/10-master.conf
unix_listener /var/spool/postfix/private/auth {
mode = 0666
user = postfix
group = postfix
}
일반과 로그인을 통한 인증 메커니즘 사용
vi /etc/dovecot/conf.d/10-auth.conf
disable_plaintext_auth=yes
auth_mechanisms = plain login
재시작 및 활성화
systemctl restart postfix
systemctl enable postfix
systemctl restart dovecot
systemctl enable dovecot
방화벽 내리기 또는 해당 포트 허용(둘 중 선택)
service firewalld stop
or
firewall-cmd --permanent --add-service=smtp
firewall-cmd --permanent --add-service=imaps
firewall-cmd --permanent --add-service=pop3s
firewall-cmd --permanent --add-port=465/tcp
firewall-cmd --permanent --add-port=587/tcp
firewall-cmd --permanent --add-port=110/tcp
firewall-cmd --permanent --add-port=143/tcp
사용자 생성 및 비밀번호 등록
useradd test
passwd test
public static void mailServerSend() {
String user = "test"; // 서버에서 만든 사용자
String password = "1234";// 위 사용자의 비밀번호
Properties prop = new Properties();
prop.put("mail.smtp.host", "xxx.xxx.xxx.xxx");// 구축한 서버 IP
prop.put("mail.smtp.port", 465);// 기본 25번, ssl 465번
prop.put("mail.smtp.auth", "true");
prop.put("mail.smtp.ssl.enable", "true");// 기본일 경우 false, ssl일 경우 true
prop.put("mail.smtp.ssl.trust", "xxx.xxx.xxx.xxx");// 서버 IP
Session session = Session.getDefaultInstance(prop, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
try {
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));// 포매팅 설정을 해주었기 때문에 따로 @hostname을 붙일 필요 없음
message.addRecipient(Message.RecipientType.TO, new InternetAddress("사용자계정@hostname));// 수신자 이메일
message.setSubject("mail send test");// 이메일 제목
message.setText("this is test mail");// 이메일 내용
Transport.send(message);
System.out.println("message sent successfully...");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
MailTest.mailServerSend();
}
outlook으로 송신한 이메일 확인(수신자 이메일 ex) 사용자계정@outlook.kr)
로그 확인(vi /var/log/maillog)
또는
실시간 확인(tail -f /var/log/maillog)
수신 메일 확인(수신자 이메일 ex) 사용자계정@hostname)
vi /var/mail/[사용자 계정]
myhostname = hostname(default) 은 사내 host명을 사용하신 것인가요? 어떤 값을 넣었는지 더 디테일하게 알 수 있나요?