Send Mail Using Smtp in C++ on Linux

Send Mail using SMTP in C++ on Linux

You might be interested in libcURL.

It's a great multi-platform C library which supports a lot of different protocols, including SMTP.

The official web page contains samples and tips to get you started.

Here is one that might help you.

Bindings exists for C++ (but I never used them) if you don't want to use the C interface.

sending an email from a C/C++ program in linux

You could invoke your local MTA directly using popen() and feed it RFC822-compliant text.

#include <stdio.h>
#include <string.h>
#include <errno.h>
int sendmail(const char *to, const char *from, const char *subject, const char *message)
{
int retval = -1;
FILE *mailpipe = popen("/usr/lib/sendmail -t", "w");
if (mailpipe != NULL) {
fprintf(mailpipe, "To: %s\n", to);
fprintf(mailpipe, "From: %s\n", from);
fprintf(mailpipe, "Subject: %s\n\n", subject);
fwrite(message, 1, strlen(message), mailpipe);
fwrite(".\n", 1, 2, mailpipe);
pclose(mailpipe);
retval = 0;
}
else {
perror("Failed to invoke sendmail");
}
return retval;
}

main(int argc, char** argv)
{
if (argc == 5) {
sendmail(argv[1], argv[2], argv[3], argv[4]);
}
}

Send emails from linux appication in c++

You could use something like the QT framework on linux to help you.

But you probably just need this: Send Mail using SMTP in C++ on Linux

Hope that helps,

~ Dan

A simple way to send mails from a C application on Unix

Here's a nice SMTP library, libESMTP

Sending emails using C

You should take a look at some examples on smtp via telnet :)

Basically you need to input in plaintext something like this:

HELO local.domain.name 
MAIL FROM: mail@domain.ext
RCPT TO: mail@otherdomain.ext
DATA
...

EDIT according to this example, your code should be:

// Not sure about this one, maybe just "\n"
#define SEPARATOR "\n\r"

int sendData( Socket *socket, const char *data) {
int iResult;
iResult = send(socket, data, (int) strlen(data), 0);
if(iResult == SOCKET_ERROR){
// Do error handling as you like
}
return iResult;
}

sendData( socket, "HELO local.doman.name" SEPARATOR);
sendData( socket, "MAIL FROM: mail@domain.ext" SEPARATOR);
sendData( socket, "RCPT TO: mail@otherdomain.ext" SEPARATOR);
sendData( socket, "DATA" SEPARATOR);
sendData( socket, "This is subject of my mail" SEPARATOR SEPARATOR);
sendData( socket, "And this is text" SEPARATOR);
sendData( socket, "." SEPARATOR); // Send mail

How to use SMTP Auth in C?

First of all, I hope you intend to improve your client so that it actually reacts to what the server sends. For example, it looks like in your _launch_ssl function you do not actually check the server's reply to EHLO to make sure STARTTLS is offered as an option before you attempt to use it. The pseudocode of that function should be:

  1. send EHLO (preferably not with something meaningless like [127.0.0.1]!)
  2. check the server's reply
  3. if STARTTLS is not among the options offered, bail
  4. if STARTTLS is among the options offered, send STARTTLS and continue.

Similarily, in main(), you need to check after EHLO that the server has actually responded 250, and check after AUTH that the server has actually responded 334.

Otherwise, the problem seems to be that you don't send \r\n after your responses. In other words:

ssl_write_socket(c, "Y2hlcmNoZXN0b3lhQGhvdG1haWwuY29tXHJcbg0K");

should be:

ssl_write_socket(c, "Y2hlcmNoZXN0b3lhQGhvdG1haWwuY29tXHJcbg0K\r\n");

Furthermore, your base64 string contains an \\r\\n\r\n (backslash + 'r' + backslash + 'n' + CR + LF) embedded inside the string as though the \\r\\n\r\n were part of the username. Assuming that's wrong, then, in fact, your code should probably actually read:

ssl_write_socket(c, "Y2hlcmNoZXN0b3lhQGhvdG1haWwuY29t\r\n");


Related Topics



Leave a reply



Submit