edit-icon download-icon

Send emails using SMTP

Last Updated: Sep 27, 2017

Enable SMTP email sending feature

  1. Log on to the DirectMail console.
  2. On the left-side navigation pane, click Sender Addresses.
  3. Find the sender address you want to enable SMTP sending feature for and click Set SMTP Password.
  4. Enter a password, and then click OK.

SMTP email send method

SMTP uses commands and responses to transfer messages between clients and servers. A client sends a command, and a server returns a response. The sender and the recipient interact in a question-and-answer session, which is controlled by the sender.

The latest SMTP RFC is RFC5321.

Two types of email servers use SMTP:

  • smtpd: It needs sender authorization.
  • mx: It receives emails from outside operator users and delivers them to local users.

Alibaba Cloud DirectMail server uses smtpd, therefore, user authorization is required (the user must be authorized for the sender address). You can authorize users (senders) and set a password for them in the DirectMail console. You can use the SMTP library to call the SMTP sending interface. For more information, see SMTP Reference.

The overall process for sending an email using SMTP is as follows:

  1. The client uses the telnet command to connect to the SMTP server to create a session.
  2. The client sends HELO or EHLO command.
  3. The client sends the AUTH authorization command to log on.
  4. The client sends MAIL command to specify a sender.
  5. The client sends RCPT command to specify a recipient.
  6. The client sends DATA command to enter mail body.
  7. The client sends . (dot) command to end the command DATA.
  8. The client sends QUIT command to end the session.

Example

The following telnet command session is provided to demonstrate an SMTP command session in order for you to familiarize yourself with how to send an email using SMTP.

Note: S stands for server, and C stands for client. Linux echo -n Content|base64 command can be used for base64 encoding.

  1. $telnet smtpdm.aliyun.com 25
  2. S:220 smtp.aliyun-inc.com MX AliMail Server(127.0.0.1)
  3. C:EHLO test.com
  4. S:250-smtp.aliyun-inc.com
  5. S:250-8BITMIME
  6. S:250-AUTH=PLAIN LOGIN XALIOAUTH
  7. S:250-AUTH PLAIN LOGIN XALIOAUTH
  8. S:250-PIPELINING
  9. S:250 DSN
  10. C:AUTH LOGIN
  11. S:334 dXNlcm5hbWU6
  12. C:dGVzdEB0ZXN0LmNvbQ== Note: base64 encoding of user test@test.com
  13. S:334 UGFzc3dvcmQ6
  14. C:dGVzdA== base64 encoding of password test
  15. S:235 Authentication successful
  16. C:MAIL FROM: <test@test.com> Note: Sender is enclosed in <> brackets
  17. S:250 Mail Ok
  18. C:RCPT TO: <test@test.com>
  19. S:250 Rcpt Ok
  20. C:DATA
  21. S:354 End data with <CR><LF>.<CR><LF>
  22. C:subject: test
  23. C:from: <test@test.com>
  24. C:to: <test@test.com>
  25. C:
  26. C:test
  27. C:.
  28. S:Data Ok: queued as freedom ###envid=148316944
  29. C:QUIT
  30. S:221 Bye

In the preceding example, the email body entered after the command DATA is a simple text string. For standard rich text emails, you must build emails in the MIME format. The emails must contain subjects and bodies to prevent the emails being tagged as spam.

For MIME protocol, see RFC 2045. You can use the library with multiple programming languages to encode MIME emails. Examples are provided on the Alibaba Cloud official website for different programming languages.

For more information on how to build MIME emails, see How can I send emails with attachments using SMTP?

Thank you! We've received your feedback.