Thanks to visit codestin.com
Credit goes to www.twilio.com

Skip to contentSkip to navigationSkip to topbar
On this page

How to send an Email with SMTP


Learn how to send an email with SMTP. This tutorial uses Telnet so you can see how an SMTP server requests and responds.

In most other use cases, you would send email with the Twilio SendGrid console or the Twilio SendGrid API.


SMTP overview

Codestin Search App

The Simple Mail Transfer Protocol (SMTP) provides the language that transmits email from one server to another. Twilio SendGrid provides an SMTP service to deliver your email using its servers.

To set custom email handling instructions, use a JSON-encoded list called the X-SMTPAPI header in the Twilio SendGrid SMTP API. To modify your message in the ways you specify, Twilio SendGrid parses the X-SMTPAPI header.

To learn more about SMTP, see the SMTP Service Crash CourseCodestin Search App in the Twilio blog.


Prerequisites

Codestin Search App

Before you start this tutorial, complete the following prerequisites.

(information)

Placeholder variable notation

The following code samples wrap placeholder values in angle brackets (<YOUR_API_KEY>). Replace the brackets and the text they contain with an actual value.

For example, if your API key is SG.someactualkey, change echo -n '<YOUR_API_KEY>' | openssl base64 to echo -n 'SG.someactualkey' | openssl base64.

In some cases, a placeholder gets wrapped in two sets of angle brackets. In this case, Twilio SendGrid requires the outer set of brackets in the actual value and you shouldn't replace them.

For example, if the example displays From: "Example" <<[email protected]>>, change your value to From: "Example" <[email protected]>.

  1. Install OpenSSL.

  2. Install telnet. See the PuTTY for WindowsCodestin Search App, macOS Homebrew formulaCodestin Search App, or Linux instructionsCodestin Search App.

  3. Sign up for a SendGrid accountCodestin Search App

  4. Create and store a SendGrid API keyCodestin Search App with full access "Mail Send" permissions.

  5. Open your terminal, command prompt, or command line.

  6. Encode your API key using Base64, use the following command.

    echo -n '<YOUR_API_KEY>' | openssl base64

    Never convert your API key using an external webpage, always convert it in your terminal with OpenSSL.

  7. Save your encoded key for later.
    Check that the API key doesn't include any newline or whitespace characters by accident. This can happen when copying the encoded key from a shell that line wraps output. SMTP uses line breaks to end commands, so Linefeed characters prevent successful authentication.

  8. Verify your Sender Identity.


Send an SMTP email using Telnet

Codestin Search App

To see how the SMTP server requests and responds, use Telnet.

(error)

Danger

Telnet doesn't register backspaces. Type your commands correctly or copy and paste them from this tutorial.

  1. Type the following in the terminal and start a Telnet session:

    telnet smtp.sendgrid.net 587

    Use port 587. This should avoid rate limiting or connection blocking from ISPs and hosting providers.

  2. After connecting to Twilio SendGrid, log in to the mail service using the following SMTP command:

    AUTH LOGIN

    The mail server responds with 334 VXNlcm5hbWU6, a Base64-encoded request for your username.

  3. Type YXBpa2V5 and press Enter on your keyboard.
    Twilio SendGrid authenticates using an API key, so it expects apikey instead of your account username. When Base64-encoded, apikey becomes YXBpa2V5.

    The mail server responds with 334 UGFzc3dvcmQ6, a Base64-encoded request for your API Key as a password.

  4. Enter your Base64-converted API key in the next line as the password and press Enter.

    The mail server responds with 235 Authentication successful. This indicates an open connection to smtp.sendgrid.net on port 587 and that you have a valid API key.

  5. Add the email that you're sending from using the SMTP MAIL FROM command and press Enter.

    MAIL FROM: <SENDER_EMAIL>

    The mail server responds with 250 Sender address accepted.

  6. Type the SMTP RCPT TO command, then the recipient email address, and press Enter.

    RCPT TO: <RECIPIENT_ADDRESS>

    To add more recipient addresses, type RCPT TO <RECIPIENT_ADDRESS> and press Enter for each recipient.

    After each recipient, the mail server should respond with 250 Recipient address accepted.

  7. Type DATA and press Enter.

    The mail server responds with 354 Continue. Unlike the MAIL FROM and RCPT TO commands, which define the email envelope and route your message to the recipient, the DATA command modifies the content of your message.

  8. You can add a mail-to header to add the name and email address of the recipient to the email header and press Enter.
    Note: Wrap the name in quotation marks (") and the address in angle brackets (<,>).

    To: "<RecipientName>" <<RecipientEmailAddress>>
  9. Add a From header to add the name and email address of the sender to the email header and press Enter.

    (warning)

    SMTP requires a From header

    If you omit a From header, Twilio SendGrid blocks your email as it violates RFC 5322Codestin Search App.

    From: "<SenderName>" <<SenderEmail>>
  10. Add a Subject line and press Enter.

    Subject: <EMAIL_SUBJECT>
  11. Press Enter to start the body of your message.

  12. Type the body content then press Enter.

    "<MESSAGE>"

    For example:

    "This is a test for the SMTP relay."
  13. Type a period (.) as the signal to end the email body, then press Enter. This sends the email.

    The mail server returns 250 Ok: queued as <examplestring1234>. The mail server has put this email into the send queue. This queue clears quickly. The mail should deliver to the designated recipients in a short time.

  14. To exit the Telnet connection, type quit and press Enter.


Complete telnet SMTP example

Codestin Search App

The following example shows the all user inputs and SMTP server responses.

1
235 Authentication successful
2
3
250 Sender address accepted
5
250 Recipient address accepted
6
DATA
7
354 Continue
8
From: "Tira Misu" <[email protected]m>
9
To: "Person 1" <[email protected]m>
10
Subject: Test message subject
11
12
"This is the test message body."
13
.
14
250 Ok: queued as Yo60h6C5ScGPeP5fUWU3K
Do you want expert help to get your email program started on the right foot?

Save time and feel confident in your long-term success with Email Implementation. Our experts work as an extension of your team to ensure your email program is correctly set up and delivering value for your business.

IMPLEMENTATION SERVICESCodestin Search App

Additional resources

Codestin Search App