Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Hands-On Network Programming with C

You're reading from  Hands-On Network Programming with C

Product type Book
Published in May 2019
Publisher Packt
ISBN-13 9781789349863
Pages 478 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Lewis Van Winkle Lewis Van Winkle
Profile icon Lewis Van Winkle

Table of Contents (26) Chapters

Title Page
Dedication
About Packt
Contributors
Preface
1. Introducing Networks and Protocols 2. Getting to Grips with Socket APIs 3. An In-Depth Overview of TCP Connections 4. Establishing UDP Connections 5. Hostname Resolution and DNS 6. Building a Simple Web Client 7. Building a Simple Web Server 8. Making Your Program Send Email 9. Loading Secure Web Pages with HTTPS and OpenSSL 10. Implementing a Secure Web Server 11. Establishing SSH Connections with libssh 12. Network Monitoring and Security 13. Socket Programming Tips and Pitfalls 14. Web Programming for the Internet of Things 1. Answers to Questions 2. Setting Up Your C Compiler on Windows 3. Setting Up Your C Compiler on Linux 4. Setting Up Your C Compiler on macOS 5. Example Programs 6. Other Book You May Enjoy Index

Chapter 8. Making Your Program Send Email

In this chapter, we will consider the protocol responsible for delivering email on the internet. This protocol is called the Simple Mail Transfer Protocol (SMTP).

Following an exposition of the inner workings of email transfer, we will build a simple SMTP client capable of sending short emails.

The following topics are covered in this chapter:

  • How SMTP servers work
  • Determining which mail server is responsible for a given domain
  • Using SMTP
  • Email encoding
  • Spam-blocking and email-sending pitfalls
  • SPF, DKIM, and DMARC

Technical requirements


The example programs from this chapter can be compiled with any modern C compiler. We recommend MinGW on Windows and GCC on Linux and macOS. See Appendix B, Setting Up Your C Compiler on Windows, Appendix C, Setting Up Your C Compiler on Linux, and Appendix D, Setting Up Your C Compiler on macOS, for compiler setup.

The code for this book can be found at https://github.com/codeplea/Hands-On-Network-Programming-with-C.

From the command line, you can download the code for this chapter with the following command:

git clone https://github.com/codeplea/Hands-On-Network-Programming-with-C
cd Hands-On-Network-Programming-with-C/chap08

Each example program in this chapter runs on Windows, Linux, and macOS. When compiling on Windows, each example program requires linking with the Winsock library. This can be accomplished by passing the -lws2_32 option to gcc.

We provide the exact commands needed to compile each example as they are introduced.

All of the example programs in this chapter...

Email servers


SMTP is the protocol responsible for delivering emails between servers. It is a text-based protocol operating on TCP port 25.

Not all emails need to be delivered between systems. For example, imagine you have a Gmail account. If you compose and send an email to your friend who also has a Gmail account, then SMTP is not necessarily used. In this case, Gmail only needs to copy your email into their inbox (or do equivalent database updates).

On the other hand, consider a case where you send an email to your friend's Yahoo! account. If the email is sent from your Gmail account, then it's clear that the Gmail and Yahoo! servers must communicate. In that case, your email is transmitted from the Gmail server to the Yahoo! server using SMTP.

This connection is illustrated in the following diagram:

Retrieving your email from your mail service provider is a different issue than delivering email between service providers. Webmail is very popular now for sending and receiving mail from your...

SMTP dialog


SMTP is a text-based TCP protocol that works on port 25. SMTP works in a lock-step, one-at-a-time dialog, with the client sending commands and the server sending responses for each command.

In a typical session, the dialog goes as follows:

  1. The client first establishes a connection to the SMTP server.
  2. The server initiates with a greeting. This greeting indicates that the server is ready to receive commands.
  3. The client then issues its own greeting.
  4. The server responds.
  5. The client sends a command indicating who the mail is from.
  6. The server responds to indicate that the sender is accepted.
  7. The client issues another command, which specifies the mail's recipient.
  8. The server responds indicating the recipient is accepted.
  9. The client then issues a DATA command.
  10. The server responds asking the client to proceed.
  11. The client transfers the email.

The protocol is very simple. In the following example SMTP session, mail.example.net is the client, and the server is mail.example.com (C and S indicate whether...

The format of an email


If we make an analogy to physical mail, the SMTP commands MAIL FROM and RCPT TO address the envelope. Those commands give the SMTP server information on how the mail is to be delivered. In this analogy, the DATA command would be the letter inside the envelope. As it's common to address a physical letter inside an envelope, it's also common to repeat the delivery information in the email, even though it was already sent to the SMTP server through the MAIL and RCPT commands.

A simple email may look like the following:

From: Alice Doe <alice@example.net>
To: Bob Doe <bob@example.com>
Subject: Re: The Cake
Date: Fri, 03 May 2019 02:31:20 +0000

Hi Bob,

Do NOT forget to bring the cake!

Best,
Alice

The entire email is transmitted to an SMTP server following the DATA command. A single period on an otherwise blank line is transmitted to indicate the end of the email. If the email contains any line beginning with a period, the SMTP client should replace it with two...

A simple SMTP client program


With a basic understanding of both SMTP and the email format, we are ready to program a simple email client. Our client takes as inputs: the destination email server, the recipient's address, the sender's address, the email subject line, and the email body text.

Our program begins by including the necessary headers with the following statements:

/*smtp_send.c*/

#include "chap08.h"
#include <ctype.h>
#include <stdarg.h>

We also define the following two constants to make buffer allocation and checking easier:

/*smtp_send.c continued*/

#define MAXINPUT 512
#define MAXRESPONSE 1024

Our program needs to prompt the user for input several times. This is required to get the email server's hostname, the recipient's address, and so on. C provides the gets() function for this purpose but gets() is deprecated in the latest C standard. Therefore, we implement our own function.

The following function, get_input(), prompts for user input:

/*smtp_send.c continued*/

void...

Enhanced emails


The emails we've been looking at so far have been only simple text. Modern email usage often demands fancier formatted emails.

We can control the content type of an email using the Content-Type header. This is very similar to the content type header used by HTTP, which we covered in Chapter 7, Building a Simple Web Server.

If the content type header is missing, a content type of text/plain is assumed by default. Therefore, the Content-Type header in the following email is redundant:

From: Alice Doe <alice@example.net>
To: Bob Doe <bob@example.com>
Subject: Re: The Cake
Date: Fri, 03 May 2019 02:31:20 +0000
Content-Type: text/plain

Hi Bob,

Do NOT forget to bring the cake!

Best,
Alice

If you want formatting support in your email, which is common today, you should use a text/html content type. In the following email, HTML is used to add emphasis:

From: Alice Doe <alice@example.net>
To: Bob Doe <bob@example.com>
Subject: Re: The Cake
Date: Fri, 03 May 2019...

Spam-blocking pitfalls


It can be much harder to send emails today than it was in the past. Spam has become a major problem, and every provider is taking actions to curb it. Unfortunately, many of these actions can also make it much more difficult to send legitimate emails.

Many residential ISPs don't allow outgoing connections on port 25. If your residential provider blocks port 25, then you won't be able to establish an SMTP connection. In this case, you may consider renting a virtual private server to run this chapter's code.

Even if your ISP does allow outgoing connections on port 25, many SMTP servers won't accept mail from a residential IP address. Of the servers that do, many will send those emails straight into a spam folder.

For example, if you attempt to deliver an email to Gmail, you may get a response similar to the following:

550-5.7.1 [192.0.2.67] The IP you're using to send mail is not authorized
550-5.7.1 to send email directly to our servers. Please use the SMTP
550-5.7.1 relay...

Summary


In this chapter, we looked at how email is delivered over the internet. SMTP, the protocol responsible for email delivery, was studied in some depth. We then constructed a simple program to send short emails using SMTP.

We looked at the email format too. We saw how MIME could be used to send multipart emails with file attachments.

We also saw how sending emails over the modern internet is full of pitfalls. Many of these stems from attempts to block spam. Techniques used by providers, such as blocking residential IP addresses, SPF, DKIM, DMARC, and IP address reputation monitoring, may make it difficult for our simple program to deliver email reliably.

In the next chapter, Chapter 9Loading Secure Web Pages with HTTPS and OpenSSL, we look at secure web connections using HTTPS.

Questions


Try these questions to test your knowledge from this chapter:

  1. What port does SMTP operate on?
  2. How do you determine which SMTP server receives mail for a given domain?
  3. How do you determine which SMTP server sends mail for a given provider?
  4. Why won't an SMTP server relay mail without authentication?
  5. How are binary files sent as email attachments when SMTP is a text-based protocol?

The answers can be found in Appendix A, Answers to Questions.

Further reading


For more information about SMTP and email formats, please refer to the following links:

lock icon The rest of the chapter is locked
You have been reading a chapter from
Hands-On Network Programming with C
Published in: May 2019 Publisher: Packt ISBN-13: 9781789349863
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}