Send an email in c++

Hello...Im wrinting a program and I need to send a simple email within intranet. I have no idea how to do. Could somebody help me please??
Thanks
You have to make your own SMTP Client.

http://www.codeproject.com/Articles/28806/SMTP-Client
Ill try the source in this article...Thank you
If your on Linux just use the command line utility mail

Im on windows XP
A help please...I found the smtp backage but i dont know how to install it :(
Hi

If you can't install a mail server (such Sendmail or Postfix or whatever ) on Windows, than try to create a VM Linux on your Windows Host OS using any virtualization software, and than use the mail server being installed on the VM to send your email( a Linux, it might be Ubuntu, Centos or whatever you feel comfortable with). By default each Linux OS has a email server delivered with it, so you may not need to install one.

By the way you can send email only in your intranet, you cant send email www, you can do it only if you have a registered mail server.



Last edited on
Actuallly what I want is to send email in my intranet. I think it is more complicated for me to work with Linux :(
Hi

Linux is not that complicated man, try Ubuntu if the other distributions are complicated for you, or
get the Source code of an opensource mail server and try to build it on your Windows OS.
the last option is to install Microsoft Exchange Server on your Windows maschine, but it is not free, it costs some thing :-(


Last edited on
Ill try it...Thanks anyway ^^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
#define _WIN32_WINNT 0x0501
#include <iostream>
#include <string>
#include <ws2tcpip.h>
#include <windns.h>

bool serveur_ok(char *msg)
{
  bool retour = ((msg[0] == '2' && msg[1] == '5' && msg[2] == '0') || (msg[0] == '2' && msg[1] == '2' && msg[2] == '0') || (msg[0] == '3' && msg[1] == '5' && msg[2] == '4'));
  if(!retour)
    std::cout << "Échec: réponse du serveur : " << msg << std::endl;
  return retour;
}

bool verifier_rec(int val)
{
  if(val == -1)
  {
    std::cout << "Erreur: Réception a échoué" << std::endl;
    return false;
  }
  return true;
}

bool verifier_env(int val)
{
  if(val == -1)
  {
    std::cout << "Erreur: Envoi a échoué" << std::endl;
    return false;
  }
  return true;
}

std::string obtenir_nom_serveur(std::string email)
{
  std::string srv = "";
  if(email.find("@") == std::string::npos)
  {
    std::cout << "Addresse e-mail mal formé" << std::endl;
  } else {
    std::string domain = email.substr(email.find("@")+1,email.size());
    if(!domain.empty())
    {
      PDNS_RECORD pdr = 0;
      DNS_STATUS rslt;
      rslt = DnsQuery(domain.c_str(),DNS_TYPE_MX,DNS_QUERY_STANDARD,0,&pdr,0);
      if(rslt)
      {
        std::cout << "Requête DNS a échoué" << std::endl;
      } else {
        srv = pdr->Data.MX.pNameExchange;
        DnsRecordListFree(pdr,DnsFreeRecordList);
      }
    } else {
      std::cout << "Addresse e-mail incorrect" << std::endl;
    }
  }
  return srv;
}

bool envoyer_smtp(const char *de,const char *a,const char *serveur,const char *port,const char *sujet,const char *msg)
{
  WSAData wsaData;
  SOCKET sock;
  addrinfo allusions;
  addrinfo *infos;

  if(WSAStartup(MAKEWORD (1, 1),&wsaData) != 0)
  {
    std::cout << "Erreur: Impossible d'initialiser le réseau" << std::endl;
    return false;
  }
  memset(&allusions,0,sizeof(addrinfo));
  allusions.ai_family = AF_UNSPEC;
  allusions.ai_socktype = SOCK_STREAM;
  if(getaddrinfo(serveur,port,&allusions,&infos) != 0)
  {
    std::cout << "Erreur:. Impossible de résoudre le address de serveur" << std::endl;
    return false;
  }
  sock = socket(infos->ai_family,infos->ai_socktype,infos->ai_protocol);
  if (sock == -1)
  {
    std::cout << "Erreur:. Impossible d'initialiser socket" << std::endl;
    return false;
  }
  int result = 0;
  result = connect(sock,infos->ai_addr,infos->ai_addrlen);
  if (result == -1)
  {
    std::cout << "Erreur: Connexion refusée ou le port est fermé" << std::endl;
    return false;
  }
  char tampon[1024] = {0};
  int recoctets = 0, envoctets = 0;
  recoctets = recv(sock,tampon,1024,0);
  if(!verifier_rec(recoctets))
    return false;
  if(serveur_ok(tampon))
  {
    strcpy(tampon,"HELO ");
    strcat(tampon,de);
    strcat(tampon,"\r\n");
    envoctets = send(sock,tampon,strlen(tampon),0);
    if(!verifier_env(envoctets))
      return false;
    memset(tampon,0,1024);
    recoctets = recv(sock,tampon,1024,0);
    if(!verifier_rec(recoctets))
      return false;
    if(serveur_ok(tampon))
    {
      strcpy(tampon,"MAIL FROM: <");
      strcat(tampon,de);
      strcat(tampon,">");
      strcat(tampon,"\r\n");
      envoctets = send(sock,tampon,strlen(tampon),0);
      if(!verifier_env(envoctets))
        return false;
      memset(tampon,0,1024);
      recoctets = recv(sock,tampon,1024,0);
      if(!verifier_rec(recoctets))
        return false;
      if(serveur_ok(tampon))
      {
        strcpy(tampon,"RCPT TO: <");
        strcat(tampon,a);
        strcat(tampon,">");
        strcat (tampon,"\r\n");
        envoctets = send(sock,tampon,strlen(tampon),0);
        if(!verifier_env(envoctets))
          return false;
        memset(tampon,0,1024);
        recoctets = recv(sock,tampon,1024,0);
        if(!verifier_rec(recoctets))
          return false;
        if(serveur_ok(tampon))
        {
          envoctets = send(sock,"DATA\r\n",6,0);
          if(!verifier_env(envoctets))
            return false;
          memset(tampon,0,1024);
          recoctets = recv(sock,tampon,1024,0);
          if(!verifier_rec(recoctets))
            return false;
          if(serveur_ok(tampon))
          {
            strcpy(tampon,"From: <");
            strcat(tampon,de);
            strcat(tampon,">");
            strcat (tampon,"\r\n");
            envoctets = send(sock,tampon,strlen(tampon),0);
            if(!verifier_env(envoctets))
              return false;
            strcpy(tampon, "To: <");
            strcat(tampon,a);
            strcat(tampon,">");
            strcat(tampon,"\r\n");
            envoctets = send(sock,tampon,strlen(tampon),0);
            if(!verifier_env(envoctets))
              return false;
            strcpy(tampon,"Subject:");
            strcat(tampon,sujet);
            strcat(tampon,"\r\n");
            envoctets = send(sock,tampon,strlen(tampon),0);
            if(!verifier_env(envoctets))
              return false;
            if(strlen(msg)>1024)
            {
              for(int L = strlen(msg);L;)
              {
                memcpy(tampon,msg,1024);
                envoctets = send(sock,tampon,strlen(tampon),0);
                if(!verifier_env(envoctets))
                  return false;
                L = L - envoctets;
              }
            } else {
              envoctets = send(sock,msg,strlen(tampon),0);
              if(!verifier_env(envoctets))
                return false;
            }
            envoctets = send(sock,"\r\n.\r\n",5,0);
            if(!verifier_env(envoctets))
              return false;
            memset(tampon,0,1024);
            recoctets = recv(sock,tampon,1024,0);
            if(!verifier_rec(recoctets))
              return false;
            if(serveur_ok(tampon))
            {
              send(sock,"QUIT\r\n",6,0);
              return true;
            }
          }
        }
      }
    }
  }
  return false;
}

int main()
{
  std::string srv, leml, reml, suj, msg;
  std::cout << std::endl << "Entrez votre adresse e-mail (en la forme \"noms@serveur\"): ";
  std::getline(std::cin,leml);
  std::cout << std::endl << "Entrez l'adresse cible de e-mail (en la forme \"cible@serveur\"): ";
  std::getline(std::cin,reml);
  srv = obtenir_nom_serveur(reml);
  if(srv.empty())
  {
    std::cout << "Impossible de localiser le serveur pour l'adresse cible de e-mail" << std::endl;
    return 0;
  }
  std::cout << std::endl << "Entrez le sujet de e-mail: ";
  std::getline(std::cin,suj);
  std::cout << std::endl << "Entrez votre message (Entrez une ligne avec seulement '.' a la fin)" << std::endl;
  std::string tmp;
  while(std::getline(std::cin,tmp) && tmp != ".")
  {
    msg += tmp;
  }
  if(envoyer_smtp(leml.c_str(),reml.c_str(),srv.c_str(),"25",suj.c_str(),msg.c_str()))
  {
    std::cout << "* Message envoyé *" << std::endl;
  } else {
    std::cout << "* Message a échoué *" << std::endl;
  }
  return 0;
}

Topic archived. No new replies allowed.