?
Solved

SMTP

Posted on 2003-03-04
5
Medium Priority
?
667 Views
Last Modified: 2010-04-17
Does anyone know of a good "Coding with SMTP" tutorial on the net?

Regards

bukko
0
Comment
Question by:bukko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
5 Comments
 
LVL 6

Expert Comment

by:PlanetCpp
ID: 8066435
if you want to know how to send a simple email it's easy
i forgot how to send attachmets, you have to code the attachment in base-64 then theres more commands and things to specify. for simple email
HELO whoiam.net
....wait for response code 250
MAIL FROM:myname@mydomain.com
....wait for response code 250
RCPT TO:recipient@thierdomain.com
....wait for response code 250
DATA
..wait for 354 code, it will tell you to write data then end with a '.' on a single line
Date: 3 Mar 03 1:54:00
Subject: this is the subject

blah blah blah
blah blah
blah blah
.
..wait for 250
QUIT

that's all but make sure your name in mail from really belongs to that server, most servers do not allow redirection cause that allows fake email
youll get a code saying it wont allow it after rcpt to:
0
 
LVL 8

Author Comment

by:bukko
ID: 8067076

What I really want to do is retrieve mail from a POP3 server with code.
(I have a mad ambition to see if this can be done with intrinsic ASP code; most things can!)

Plus, how do I send the above; to a specific place? via an ARP type signal?

Thanks

bukko

0
 
LVL 1

Accepted Solution

by:
d_iego earned 400 total points
ID: 8067442
I think I have what you need in this site you'll find examples and code it's in c++ but if you want to do it with asp and know smtp with asp pop3 will be the same but with different protocol. the site is on:
I don't why the site is only in french it used to hava everything in english too. but I paste what I saved form the English old site:

POP (POP3) protocol : post office protocol  
POP protocol ( post office protocol ) (The RFC 1939, which replaced the RFC 1735, describes POP3 standart revision) is how the email clients get the emails on the web server. As well as the majority of the protocol, it is based on command line , that means that every operation are processed using a command text which is sent to the server. So it's easy to simulate a client using a telnet session to a server ( and using the right port ) so it's easy too to implement a task specialised client based on the mail.
The POP3 server listen to port 110. If you launch a telnet to a POP3 server port 110 the result is something like that :
   +OK POP3 pop3.serveur.com v4.38 server ready
The line starts with a state indicator +OK. Any answer starts with +OK or -ERR. Eaaaasyyyyy !!!

At the beginning of the session you are in login mode. Only the command USER, PASS et QUIT are accessible. So in order to login enter :


        > USER nicolas
        > PASS hello
Eaaasyyyyy (again) !!
Once log in, you can access to your mailbox, so you have to use the following commands : STAT This command send two lines : the first one gives the number of messages, the second line give the total size of the messages. In fact, this command is not so usefull !!
LIST LIST gives the mailbox content as described below :

LIST
+OK Mailbox contents follow
1 7774
2 513
3 10493
.
The last line is a single dot, which is a quite standart method for the mail protocol. Each line gives the number of the message and its size. Be aware that the number may be not sequentials !!! If a message is deleted, it just disappear.
 
RETR msg RETR gets one message. Use the number given by the LIST command. The result is the header of the message then the body of the message and a single dot. If the message contains a single dot it will be double. aller recevoir l'entete du message, puis le contenu et enfin une ligne avec simplement un point.  
DELE msg DELE delete a message. In fact the message is definitivly deleted only when the QUIT command is sent. So it is possible to use the RSET command in order to get back a message.
TOP msg n TOP send the header of a message plus the first lines of the body. This is an optionnal command which is generaly accessible.  
QUIT QUIT ends the session and delete definitivly all the DELE messages.  

 
This should be enough to do a lot of funny thing as remove all your e-mail by mistake on your server for example :-).
 
And the example is:

POP (POP3) protocol : POP3 Client  
Here is the source code of a really small POP3 client.

--------------------------------------------------------------------------------
In order to test, do not forget to modify 'SERVEUR POP3', USERNAME et PASSWORD.
--------------------------------------------------------------------------------

#include <arpa/telnet.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <netdb.h>
#include <netinet/in.h>
#include <sys/socket.h>

#define PACKET_SIZE  1024

#define DEBUG 1

void appli ( void ) ;
int writen(int fd, char *ptr, int n);
int readn(int fd, char *ptr, int n);

char *proxy_name = "Le SERVEUR POP3";
unsigned short proxy_port=110;

char *user= "USERNAME";
char *pass= "PASSWORD";

int to_server_socket = -1;


int main (int argc, char *argv[])
{
struct sockaddr_in serverSockAddr;    /* addresse de la socket */
struct hostent *serverHostEnt;        /* description du host serveur */
unsigned long hostAddr;                       /* addr du serveur */
long status;
char buffer[PACKET_SIZE+1];
int optval = 1;

  bzero(&serverSockAddr,sizeof(serverSockAddr));/*init a zero serverSockAddr*/
  hostAddr = inet_addr(proxy_name); /* converti l'adress ip 9.100.1.1
                                       en entier long */

  if ( (long)hostAddr != (long)-1)                  
          bcopy(&hostAddr,&serverSockAddr.sin_addr,sizeof(hostAddr));
  else                           /* si on a donne un nom  */
  {
     serverHostEnt = gethostbyname(proxy_name);
     if (serverHostEnt == NULL)
     {
             printf("ca chie gethost\n");
             exit(0);
     }
     bcopy(serverHostEnt->h_addr,&serverSockAddr.sin_addr,
           serverHostEnt->h_length);
  }

  serverSockAddr.sin_port = htons(proxy_port); /* host to network port  */
  serverSockAddr.sin_family = AF_INET;  /* AF_*** : INET=internet */
  /* creation de la socket */

  if ( (to_server_socket = socket(AF_INET,SOCK_STREAM,0)) < 0)
  {
          printf("ca chie creation socket client\n");
          exit(0);
  }
/*
  if (setsockopt(to_server_socket,SOL_SOCKET,SO_KEEPALIVE,
      (char *)&optval,sizeof(int))                           <0)
  {
          printf("ca chie setsockopt\n");
          exit(0);
  }
*/  
  /* requete de connexion */
  if(connect(to_server_socket,(struct sockaddr *)&serverSockAddr,
             sizeof(serverSockAddr))<0)
  {
          printf("ca chie demande de connection\n");
          exit(0);
  }
  appli();

  /* fermeture de la connection */
  shutdown(to_server_socket,2);
  close(to_server_socket);
  return 0;
}


int writen(int fd, char *ptr, int n)
{
char *p;
int nl, nw;
        nl = n;
        while ( nl > 0 ) {
                nw = write(fd, ptr, nl);
                if ( nw <= 0 )
                        return nw;     /*error*/
                nl -= nw;
                ptr += nw;
        }
        return (n-nl);
}

void writehex(char *buf) {
int n = strlen(buf);

  do {
    printf("0x%X ", buf[n]);
  } while (n--);
}

int readn(int fd, char *ptr, int n){
int nl, nr, retry=0;

        nl = n;
        bzero(ptr,n);
        while ( nl > 0 ) {
                nr = read(fd,ptr,nl);
                if (nr < 0 ) {
                    if (errno != EAGAIN) { printf("EAGAIN\n"); exit(1); }
                    retry++;
                    usleep(100);
                    if ( retry >= 5 ) return nr;     /*error*/
                } else {
                    if ( nr == 0 )
                           break;
                }
                nl -= nr;
                ptr += nr;
                if ( *(ptr-2) == '\r' && *(ptr-1) == '\n' )
                  break;
        }
        *ptr = 0x00;
        return (n-nl);
}


int test_OK(char *buf, int do_exit) {
 char *ptr, tmp[PACKET_SIZE+1];

    bzero(tmp, PACKET_SIZE+1);
    if ((ptr=strstr(buf, "+OK")) == NULL) {
      if (DEBUG) printf("%s", buf);
        if ( strstr(buf, "-ERR") ) {
          printf("ERROR: -->%s<--\n", buf);
          exit(1);
        }
      if (do_exit ) exit(1);
      else return 1;
    } else {
      if (DEBUG) printf("-->%s<--\n",ptr);
      while(*ptr != '\n') ptr++ ;
      bcopy(ptr+1,tmp,strlen(ptr));
      bzero(buf,PACKET_SIZE+1);
      bcopy(tmp, buf, strlen(tmp));
    }
    return 0;
}

void appli ( void )
{
unsigned char bufw[PACKET_SIZE+1];
unsigned char bufr[PACKET_SIZE+1];
unsigned char list[PACKET_SIZE+1];
int n, msg, index, retry = 4;

  bzero(bufw, PACKET_SIZE+1);
  bzero(bufr, PACKET_SIZE+1);

  do {
    n=readn(to_server_socket,bufr,PACKET_SIZE);
    retry--;
  } while(test_OK(bufr,retry==0));

  sprintf(bufw,"USER %s\r\n",user);
  writen(to_server_socket,bufw,strlen(bufw));
  n=readn(to_server_socket,bufr,PACKET_SIZE);
  if (DEBUG) printf("USER: ");
  test_OK(bufr,1);
 
  sprintf(bufw,"PASS %s\r\n",pass);
  writen(to_server_socket,bufw,strlen(bufw));
  n=readn(to_server_socket,bufr,PACKET_SIZE);
  if (DEBUG) printf("PASS: ");
  test_OK(bufr,1);
 
  sprintf(bufw,"LIST\r\n");
  writen(to_server_socket,bufw,strlen(bufw));
  n=readn(to_server_socket,bufr,PACKET_SIZE);
  if (DEBUG) printf("LIST: ");
  test_OK(bufr,1);

  if (strlen(bufr) == 0 ) n=readn(to_server_socket,list,PACKET_SIZE);
  else bcopy(bufr, list, strlen(bufr) + 1) ;
  if (DEBUG) printf(list);

  index = 0;
  while ( list[index] != '.' ) {
 
    sscanf(&list[index], "%d", &msg);
    while(list[index++] != '\n');

    sprintf(bufw,"RETR %d\r\n",msg);
    if (DEBUG) printf("^[[34m %s^[[30m",bufw);

    writen(to_server_socket,bufw,strlen(bufw));
    n=readn(to_server_socket,bufr,PACKET_SIZE);
    test_OK(bufr,1);
    if (!DEBUG) printf("%s",bufr);
   
    n=strlen(bufr);
    do {
      if (strlen(bufr) == 0 ) n=readn(to_server_socket,bufr, PACKET_SIZE);
      if (!DEBUG) printf("%s",bufr);
      if ( ! strncmp("\r\n.\r\n",&bufr[n-5],5) ) break;
      bzero(bufr, PACKET_SIZE+1);
    } while ( 1 );

  }
}
 
I hope that helps
Diego Mendieta
0
 
LVL 8

Author Comment

by:bukko
ID: 8068464

That's great info so far.
Do you know of a way of invoking Telnet from ASP so I can contact the server?
Is there a function within CDONTS or similar which will allow me to do this?

Thanks

bukko
0
 
LVL 8

Author Comment

by:bukko
ID: 8068530

Actually, I have decided to accept your comment as an answer because you've given me what I really need to know.
I've been able to manually contact my mailserver and 'retrieve' data, so that's got that part sussed.
I'll ask the Telnet question in the ASP section and throw loads of points at it - maybe some bright spark will be able to help!

Thanks to all for your help,

bukko
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Introduction to Processes
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question