Send emails using SMTP

更新时间:
复制 MD 格式

Direct Mail supports SMTP, letting you send emails programmatically using any language's SMTP library or standard command-line tools.

Prerequisites

Before sending emails over SMTP, set an SMTP password for your sender address.

  1. Log in to the Direct Mail console.

  2. In the left navigation pane, click Sender Addresses.

  3. Find the sender address you want to use, then click Set SMTP Password in the Actions column.

  4. Enter an SMTP password and click OK.

How SMTP sending works

Simple Mail Transfer Protocol (SMTP) transfers messages through a command-and-response exchange between a client and a server. The client sends a command; the server returns an acknowledgement. The sender controls this process.

For the full protocol specification, see RFC5321.

SMTP servers fall into two categories:

  • smtpd (mail sending server) — requires sender authentication before accepting outbound messages.

  • mx (mail receiving server) — accepts inbound messages delivered from external domains to local users.

The Direct Mail server is an smtpd server. The authentication username must exactly match the sender address, and the password is the one you set in the console.

To send emails from application code, use an SMTP library for your language instead of raw SMTP commands. See SMTP call examples.

Connection details

Parameter

Value

Host

smtpdm.aliyun.com

Port (unencrypted)

80

Port (TLS/SSL)

465

Authentication

AUTH LOGIN with Base64-encoded username and password

Username

Your sender address (e.g., sender@example.com)

Password

The SMTP password set in the Direct Mail console

SMTP session flow

Each SMTP session follows this sequence:

  1. Connect to the SMTP server using telnet (unencrypted) or openssl s_client (TLS).

  2. Send EHLO to identify the client and negotiate capabilities.

  3. Send AUTH LOGIN and supply Base64-encoded credentials.

  4. Send MAIL FROM to specify the sender address.

  5. Send RCPT TO to specify the recipient address.

  6. Send DATA followed by the message headers and body.

  7. Send . (a lone period) to signal the end of the message body.

  8. Send QUIT to close the session.

Examples

The examples below show complete SMTP sessions using command-line tools. S: is the server response; C: is the client command. Use echo -n Content|base64 on Linux to Base64-encode values.

Unencrypted (port 80)

$telnet smtpdm.aliyun.com 80
S:220 smtp.aliyun-inc.com MX AliMail Server(127.0.0.1)
C:EHLO test.com
S:250-smtp.aliyun-inc.com
S:250-8BITMIME
S:250-AUTH=PLAIN LOGIN XALIOAUTH
S:250-AUTH PLAIN LOGIN XALIOAUTH
S:250-PIPELINING
S:250 DSN
C:AUTH LOGIN
S:334 dXNlcm5hbWU6
C:YSoqKkBleGFtcGxlLm5ldA==         Note: The Base64 encoding of the username a***@example.net.
S:334 UGFzc3dvcmQ6
C:dGVzdA==                     Note: The Base64 encoding of the user password test.
S:235 Authentication successful
C:MAIL FROM: <a***@example.net>   Note: Enclose the sender address in angle brackets (<>).
S:250 Mail Ok
C:RCPT TO: <a***@example.net>
S:250 Rcpt Ok
C:DATA
S:354 End data with <CR><LF>.<CR><LF>
C:subject: test
C:from: <a***@example.net>
C:to: <a***@example.net>
C:
C:test
C:.
S:Data Ok: queued as freedom ###envid=148316944
C:QUIT
S:221 Bye

Encrypted (port 465, TLS)

First, connect with OpenSSL. Choose one of the following commands based on the TLS version you want to use:

openssl s_client -connect smtpdm.aliyun.com:465 -crlf
or
openssl s_client -connect smtpdm.aliyun.com:465 -tls1 -cipher AES128-SHA -crlf
openssl s_client -connect smtpdm.aliyun.com:465 -tls1_1 -cipher AES128-SHA -crlf
openssl s_client -connect smtpdm.aliyun.com:465 -tls1_2 -cipher AES128-GCM-SHA256 -crlf
openssl s_client -connect smtpdm.aliyun.com:465 -tls1_3 -cipher AES128-GCM-SHA256 -crlf

After OpenSSL connects, enter the following commands in the same window, one line at a time:

$openssl s_client -connect smtpdm.aliyun.com:465 -crlf
***
***
***
S:220 DirectMail Smtpd Server(127.0.0.1)
C:EHLO test.com
S:250-smtp.aliyun-inc.com
S:250-8BITMIME
S:250-AUTH=PLAIN LOGIN XALIOAUTH
S:250-AUTH PLAIN LOGIN XALIOAUTH
S:250-PIPELINING
S:250 DSN
C:AUTH LOGIN
S:334 dXNlcm5hbWU6
C:YSoqKkBleGFtcGxlLm5ldA==          Note: The Base64 encoding of the username a***@example.net.
S:334 UGFzc3dvcmQ6
C:dGVzdA==                        Note: The Base64 encoding of the user password test.
S:235 Authentication successful
C:MAIL FROM: <a***@example.net>  Note: Enclose the sender address in angle brackets (<>).
S:250 Mail Ok
C:rcpt to: <a***1@example.net>  Note: Use lowercase for rcpt to. Uppercase may not be recognized.
S:250 Rcpt Ok
C:DATA
S:354 End data with <CR><LF>.<CR><LF>
C:subject: test
C:from: <a***@example.net>
C:to: <a***1@example.net>
C:
C:test
C:.
S:250 Data Ok: queued as freedom ###envid=600000105713504612
C:QUIT
S:DONE

Message body and MIME

The examples above use a plain text body. For rich text emails (HTML, attachments, encoded subjects), format the message according to the Multipurpose Internet Mail Extensions (MIME) specification. Correct MIME formatting reduces the chance of the recipient's mail server flagging the message as spam.

For the MIME specification, see RFC2045. You can also use the language-specific libraries in the Direct Mail SMTP examples to build MIME-encoded messages.

For a step-by-step example, see How do I send an email with an attachment using SMTP?