SMTP CS176A Ramya Raghavendra

advertisement
SMTP
CS176A
Ramya Raghavendra
ramya@cs.ucsb.edu
SMTP
• What is SMTP?
– Simple Mail Transfer Protocol
– SMTP server – every email client interfaces to send
email
– List of SMTP servers in transit - Check the headers
on your email!
• Why talk directly to SMTP server?
– Automation of emails in applications
• Without dependency on another email client
– Real time notification of status
• Wrong ID, passwords etc
Example
• Consider Windows
alternative like MAPI
• More complex
• More overhead
Multipart Messages
• But can we send multipart messages with
attachments?
– YES
– SMTP is only a delivery mechanism, nothing to do
with contents
– RFCs on how to create multipart messages with
attachments
• Your assignment
– Simple text
SMTP commands
HELO hostname
Initiate SMTP conversation
MAIL From: <source>
Start. Sender’s email address
RCPT To: <dest>
Destination. Can be repeated.
SIZE=<# of bytes>
Size of text. Good practice.
DATA
Data will follow. Terminate with “.”
QUIT
Terminates the connection.
VRFY username
Verify email. Can be disabled for security.
Subject:
Cc:
Reply-To:
Sent as a part of DATA stream.
For testing purposes
• Part 1 :
– Please note: NO TURNIN
• Part 2:
– To : command line argument
– Subject: cs176aHW3ProgPart2
– Content: Name and Perm no.
Email Sender: Logic
/* open the network connection */
sfd = smtpConnect(smtp_server,smtp_port);
if (sfd == INVALID_SOCKET) {
rc=(-1);
goto cleanup;
}
// Send HELO
smtpHelo(sfd,helo_domain);
//Send “From” address
smtpMailFrom(sfd,from);
//Send “To” address
smtpRcptTo(sfd);
Email Sender: Logic (Cont)
//Send Data
smtpData(sfd);
//Send End-of-mail “.”
smtpEom(sfd);
//Send QUIT
smtpQuit(sfd);
Connection to SMTP Server
/* connect to SMTP server and returns the socket fd */
static SOCKET smtpConnect (char *smtp_server, int port) {
SOCKET
sfd;
sfd=clientSocket(smtp_server,port);
if (sfd == INVALID_SOCKET) {
errorMsg("Could not connect to SMTP server \"%s\" at port %d\n",
smtp_server,port);
return (INVALID_SOCKET);
}
/* save it. we'll need it to clean up */
smtp_socket=sfd;
return (sfd);
}
One example
/* SMTP: HELO */
static int smtpHelo(int sfd,char *helo_domain)
{
memset(buf,0,sizeof(buf));
(void) snprintf(buf,sizeof(buf)-1,"HELO %s\r\n",helo_domain);
//Socket write function
sockPuts(sfd,buf);
return (smtpResponse(sfd));
}
Download