Ok 👍 mình hướng dẫn bạn cấu hình Postfix port 587 (submission) ĐÚNG CHUẨN, từng bước, áp dụng cho Postfix (Ubuntu) như server của bạn.

Mục tiêu sau khi xong:

  • Port 587
  • STARTTLS
  • SMTP AUTH
  • Dùng được với MailKit / .NET

🎯 MỤC TIÊU KỸ THUẬT

Postfix sẽ:

  • Lắng nghe 587
  • Bật STARTTLS
  • Cho phép AUTH LOGIN / PLAIN
  • Chỉ cho user đã auth gửi mail

1️⃣ KIỂM TRA POSTFIX ĐANG LẮNG NGHE PORT 587 CHƯA

sudo netstat -plnt | grep :587

Nếu KHÔNG thấy → cần cấu hình
Nếu nhưng chưa auth → vẫn phải chỉnh


2️⃣ SỬA FILE master.cf (QUAN TRỌNG NHẤT)

Mở file:

sudo nano /etc/postfix/master.cf

Tìm (hoặc thêm) TOÀN BỘ KHỐI DƯỚI ĐÂY
👉 NHỚ bỏ dấu # nếu đang bị comment

submission inet n       -       y       -       -       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_reject_unlisted_recipient=no
  -o smtpd_recipient_restrictions=permit_sasl_authenticated,reject
  -o milter_macro_daemon_name=ORIGINATING

📌 Ý nghĩa:

  • submission = port 587
  • tls_security_level=encrypt → BẮT BUỘC STARTTLS
  • sasl_auth_enable=yes → cho AUTH
  • permit_sasl_authenticated → chỉ user auth mới gửi

3️⃣ CẤU HÌNH SASL (AUTH) – BẮT BUỘC

Mở file:

sudo nano /etc/postfix/main.cf

Thêm hoặc đảm bảo có:

# TLS
smtpd_tls_cert_file=/etc/letsencrypt/live/mail.giamkichsan.com/fullchain.pem
smtpd_tls_key_file=/etc/letsencrypt/live/mail.giamkichsan.com/privkey.pem
smtpd_tls_security_level=may
smtpd_tls_loglevel=1

# SASL AUTH
smtpd_sasl_auth_enable = yes
smtpd_sasl_type = dovecot
smtpd_sasl_path = private/auth
smtpd_sasl_security_options = noanonymous
smtpd_sasl_local_domain = $myhostname
broken_sasl_auth_clients = yes

📌 Postfix KHÔNG tự auth → nó dùng Dovecot


4️⃣ CẤU HÌNH DOVECOT (NẾU CHƯA CÓ)

Kiểm tra dovecot:

systemctl status dovecot

Nếu chưa có:

sudo apt install dovecot-core dovecot-imapd -y

Sửa file:

sudo nano /etc/dovecot/conf.d/10-master.conf

Tìm block:

service auth {

Bên trong phải có:

service auth {
  unix_listener /var/spool/postfix/private/auth {
    mode = 0660
    user = postfix
    group = postfix
  }
}

📌 Cái này cho phép Postfix gọi Dovecot để AUTH


5️⃣ KHỞI ĐỘNG LẠI DỊCH VỤ

sudo systemctl restart dovecot
sudo systemctl restart postfix

6️⃣ TEST AUTH PORT 587 (CỰC KỲ QUAN TRỌNG)

openssl s_client -starttls smtp -connect mail.giamkichsan.com:587

Sau khi hiện:

220 mail.giamkichsan.com ESMTP Postfix

Gõ:

EHLO test

👉 BẠN PHẢI THẤY:

250-AUTH LOGIN PLAIN
250-STARTTLS

✅ Thấy AUTH → cấu hình THÀNH CÔNG
❌ Không thấy → Postfix chưa nhận SASL


7️⃣ CODE .NET CHUẨN SAU KHI CẤU HÌNH XONG

client.Connect(
    "mail.giamkichsan.com",
    587,
    SecureSocketOptions.StartTls
);

client.Authenticate(
    "vcduan@giamkichsan.com",
    "Soc@2902"
);

client.Send(message);

🧨 LỖI HAY GẶP & CÁCH SỬA NHANH

LỗiNguyên nhân
No AUTHDovecot chưa link Postfix
535 auth failedSai user / mailbox
TLS errorSai path cert
timeoutFirewall chặn 587

👉 Mở firewall:

sudo ufw allow 587/tcp

✅ TÓM TẮT CHỐT

  • ❌ Đừng cố port 465
  • 587 + STARTTLS là chuẩn
  • ✅ Postfix + Dovecot
  • ✅ MailKit chạy mượt

👉 Sau khi bạn cấu hình xong, copy output của lệnh này gửi mình:

openssl s_client -starttls smtp -connect mail.giamkichsan.com:587

Mình sẽ check và xác nhận OK 100% trước khi bạn chạy tool .NET 💯

🟢 CÁCH 2 (CHUẨN MAIL SERVER – KHUYÊN DÙNG)

👉 Dùng passwd-file cho mailbox (ĐÚNG CHUẨN ISP)

1️⃣ Tạo file password mailbox

mkdir -p /etc/dovecot/passwd
nano /etc/dovecot/passwd/users

Nội dung:

vcduan@giamkichsan.com:{PLAIN}Soc@2902

2️⃣ Sửa auth config

nano /etc/dovecot/conf.d/10-auth.conf

Đổi thành:

disable_plaintext_auth = no
auth_mechanisms = plain login

!include auth-passwdfile.conf.ext

3️⃣ Sửa passwd-file config

nano /etc/dovecot/conf.d/auth-passwdfile.conf.ext

Đảm bảo:

passdb {
  driver = passwd-file
  args = scheme=PLAIN /etc/dovecot/passwd/users
}

userdb {
  driver = static
  args = uid=vmail gid=vmail home=/var/mail/vhosts/%d/%n
}

Hãy bình luận đầu tiên

Để lại một phản hồi

Thư điện tử của bạn sẽ không được hiện thị công khai.


*