Send E-Mail in C++ - c++

Im trying to send an email in C++. I found the class CSmtp which looks like a fine one.So I tried out the example project but it gives the ErrorCode :
Error: Undefined error id.
Now I tried to find out where the problem is because the errorcode is too general.I Debuged the project find this Errorcode
ErrorCode WSA_SELECT (109) ECSmtp::CSmtpError
I googled after it but found no answer.Someone can help ?
Link for class : CSmtp with SSL/TLS
Code:
#include "CSmtp.h"
#include <iostream>
int main()
{
bool bError = false;
try
{
CSmtp mail;
#define test_gmail_tls
#if defined(test_gmail_tls)
mail.SetSMTPServer("smtp.gmail.com",587);
mail.SetSecurityType(USE_TLS);
#elif defined(test_gmail_ssl)
mail.SetSMTPServer("smtp.gmail.com",465);
mail.SetSecurityType(USE_SSL);
#elif defined(test_hotmail_TLS)
mail.SetSMTPServer("smtp.live.com",25);
mail.SetSecurityType(USE_TLS);
#elif defined(test_aol_tls)
mail.SetSMTPServer("smtp.aol.com",587);
mail.SetSecurityType(USE_TLS);
#elif defined(test_yahoo_ssl)
mail.SetSMTPServer("plus.smtp.mail.yahoo.com",465);
mail.SetSecurityType(USE_SSL);
#endif
mail.SetLogin("* My Email Adress *");
mail.SetPassword("*Password*");
mail.SetSenderName("User");
mail.SetSenderMail("* My Email Adress *");
mail.SetReplyTo("* Email Adress of my friend *");
mail.SetSubject("The message");
mail.SetXPriority(XPRIORITY_NORMAL);
mail.SetXMailer("The Bat! (v3.02) Professional");
mail.AddMsgLine("Hello,");
mail.AddMsgLine("");
mail.AddMsgLine("...");
mail.AddMsgLine("How are you today?");
mail.AddMsgLine("");
mail.AddMsgLine("Regards");
mail.ModMsgLine(5,"regards");
mail.DelMsgLine(2);
mail.AddMsgLine("User");
//mail.AddAttachment("../test1.jpg");
//mail.AddAttachment("c:\\test2.exe");
//mail.AddAttachment("c:\\test3.txt");
mail.Send();
}
catch(ECSmtp e)
{
std::cout << "Error: " << e.GetErrorText().c_str() << ".\n";
bError = true;
}
if(!bError)
std::cout << "Mail was send successfully.\n";
return 0;
}

I would suggest you to go with powershell script for sending email and then call that script from your c++ program. Use smtp.gmail.com as server & 465 port. :)
This can help..
https://github.com/udit043/Send-email-using-powershell

Standard C++11 does not have such facilities (but your particular OS might provide additional SMTP client libraries). Maybe you want a library like vmime or a framework like POCO
Notice that the very ability of email is operating system specific.
Also, most SMTP servers are configured to reject open mail relaying (otherwise, they would be used by spam bots, and emails going thru them would be rejected since blacklisted).
You could for example configure some SMTP service on your local machine or server, and programmatically send email thru localhost (then the SMTP server would spool and relay the email, etc.) - or to some specific and well-defined "smarthost" if your system and network has one; you probably don't want to wire-in some SMTP relay host names in your program, and you do want the email to be spooled (not lost!) if the recipient SMTP server is down or unreachable; in other words, you need some SMTP server.
You certainly don't want to code your SMTP server from scratch!
Hence, your program should only send SMTP to some trusted host, which is correctly configured to avoid spamming (then you might need to set some From or Reply-To SMTP header to something appropriate for your SMTP host). Be sure that major Internet companies like Google, Yahoo, AOL etc... are configuring carefully their SMTP service to disallow open relay spamming. Often, they accept SMTP relay connections only from their business clients (and they would cut a client doing automated spamming). So you should send SMTP to your ISP's SMTP server (or to some local or near SMTP server).
Notice that spamming is usually forbidden by law or by your ISP terms of service.
You could also run some command (e.g. using popen) to send mail ...

Related

I'm getting bounce when I send an email to a specific address

I'm getting bounce when I send an email to a specific address using SES, from gmail the mail is delivered correctly
For Transient -> General AWS says The recipient's email provider sent a general bounce message. You might be able to send a message to the same recipient in the future if the issue that caused the message to bounce is resolved.
How can I fix the issue if I do not know the problem?
"eventType":"Bounce",
"bounce":{
"bounceType":"Transient",
"bounceSubType":"General",
"bouncedRecipients":[
{
"emailAddress":"{some_email}",
"action":"failed",
"status":"5.7.8",
"diagnosticCode":"smtp; 535 5.7.8 Error: blocked by Block Address check from 54.240.8.90"
}
],
"timestamp":"2019-07-03T19:48:56.445Z",
"feedbackId":"0100016bb962013a-6cd68815-3c51-4216-9946-50f01b923057-000000",
"reportingMTA":"dsn; a8-90.smtp-out.amazonses.com"
}
Not much you can do, seems like the recipient side is checking IP reputation and found that SES IP (sending IP) 54.240.8.90 is in the blacklist, it also sent you a bounce back with custom message "smtp; 535 5.7.8 Error: blocked by Block Address check from 54.240.8.90".
Seems like they're using SORBS SPAM .
https://mxtoolbox.com/SuperTool.aspx?action=blacklist%3a54.240.8.90&run=toolpage
http://www.sorbs.net/cgi-bin/db
Couple of things you can try:
Remove the IP from SORBS by yourself (it may get added again)
Contact AWS to contact them to remove it from Blacklist.
Try dedicated IP pool.

Clarification needed about a SSL client using Boost asio

I have a C++ application that uses Boost_asio to do TCP/IP connection who read a .php document in a Web server that in turn uses the php script to update certain statistics.
The whole thing work as planned, but recently the server changed to use SSL (Google mandatory) and naturally, the previous connection stopped to work.
After a lot of theoretical reading about SSL, I'm still in the dark about certain practical details.
Using the example in the Boost asio documentation and a file “cacert.pem”, downloaded form somewhere following indications in this site, I'm able to run correctly the example using:
<host> = “www.google.com” and <port> = “https”.
Using the example “as is”, the Google server response is:
Verifying /OU=GlobalSign Root CA - R2/O=GlobalSign/CN=GlobalSign
Verifying /C=US/O=Google Trust Services/CN=Google Internet Authority G3
Verifying /C=US/ST=California/L=Mountain View/O=Google LLC/CN=www.google.com
Enter message: Reply:
But when using
<host> = “www.zator.com” and <port> = “https”
my domain, hosted in 1&1 IONOS, the reply is:
Handshake failed: tlsv1 alert internal error
At this point I have several questions:
What in the hell mean the sentence: ctx.load_verify_file("cacert.pem"); ?
The content of that file, can be the culprit of the fail when connecting with my domain?
Is that sentence essential to complete the connection?
In the case of google server (absence of error), is it supposed that after the sentence io_context.run(); the connection has been correctly established?
Assuming I make public the client's member socket_ (private in the example), can I continue with some as (I can't test that in google :-)
std::string request("GET /mystatistics.php HTTP/1.1\r\n\r\n");
boost::asio::write(c.socket_, boost::asio::buffer(request));
boost::system::error_code ec;
std::string response;
do { // read response
char buf[1024];
size_t bytes_transferred = c.socket_.read_some(boost::asio::buffer(buf), ec);
if (!ec) response.append(buf, buf + bytes_transferred);
} while (!ec);
std::cout << "Response received: '" << response << "'\n";
Thanks in advance.
I've found some useful information here. A good, albeit partial info, and a good start point to further search

Is it possible to connect to the Google IOTCore MQTT Bridge via Javascript?

I've been trying to use the javacscript version of the Eclipse Paho MQTT client to access the Google IOTCore MQTT Bridge, as suggested here:
https://cloud.google.com/iot/docs/how-tos/mqtt-bridge
However, whatever I do, any attempt to connect with known good credentials (working with other clients) results in this connection error:
errorCode: 7, errorMessage: "AMQJS0007E Socket error:undefined."
Not much to go on there, so I'm wondering if anyone has ever been successful connecting to the MQTT Bridge via Javascript with Eclipse Paho, the client implementation suggested by Google in their documentation.
I've gone through their troubleshooting steps, and things seem to be on the up and up, so no help there either.
https://cloud.google.com/iot/docs/troubleshooting
I have noticed that in their docs they have sample code for Java/Python, etc, but not Javascript, so I'm wondering if it's simply not supported and their documentation just fails to mention as such.
I've simplified my code to just use the 'Hello World' example in the Paho documentation, and as far as I can tell I've done things correctly (including using my device path as the ClientID, the JWT token as the password, specifying an 'unused' userName field and explicitly requiring MQTT v3.1.1).
In the meantime I'm falling back to polling via their HTTP bridge, but that has obvious latency and network traffic shortcomings.
// Create a client instance
client = new Paho.MQTT.Client("mqtt.googleapis.com", Number(8883), "projects/[my-project-id]/locations/us-central1/registries/[my registry name]/devices/[my device id]");
// set callback handlers
client.onConnectionLost = onConnectionLost;
client.onMessageArrived = onMessageArrived;
// connect the client
client.connect({
mqttVersion: 4, // maps to MQTT V3.1.1, required by IOTCore
onSuccess:onConnect,
onFailure: onFailure,
userName: 'unused', // suggested by Google for this field
password: '[My Confirmed Working JWT Token]' // working JWT token
function onFailure(resp) {
console.log(resp);
}
// called when the client connects
function onConnect() {
// Once a connection has been made, make a subscription and send a message.
console.log("onConnect");
client.subscribe("World");
message = new Paho.MQTT.Message("Hello");
message.destinationName = "World";
client.send(message);
}
// called when the client loses its connection
function onConnectionLost(responseObject) {
if (responseObject.errorCode !== 0) {
console.log("onConnectionLost:"+responseObject.errorMessage);
}
}
// called when a message arrives
function onMessageArrived(message) {
console.log("onMessageArrived:"+message.payloadString);
}
I'm a Googler (but I don't work in Cloud IoT).
Your code looks good to me and it should work. I will try it for myself this evening or tomorrow and report back to you.
I've spent the past day working on a Golang version of the samples published on Google's documentation. Like you, I was disappointed to not see all Google's regular languages covered by samples.
Are you running the code from a browser or is it running on Node.JS?
Do you have a package.json (if Node) that you would share too please?
Update
Here's a Node.JS (JavaScript but non-browser) that connects to Cloud IoT, subscribes to /devices/${DEVICE}/config and publishes to /devices/${DEVICE}/events.
https://gist.github.com/DazWilkin/65ad8890d5f58eae9612632d594af2de
Place all the files in the same directory
Replace values in index.js of the location of Google's CA and your key
Replaces [[YOUR-X]] values in config.json
Use "npm install" to pull the packages
Use node index.js
You should be able to pull messages from the Pub/Sub subscription and you should be able to send config messages to the device.
Short answer is no. Google Cloud IoT Core doesn't support WebSockets.
All the JavaScript MQTT libraries use WebSocket because JavaScript is restricted to perform HTTP requests and WebSocket connections only.

Setting password in IRC BOT written in C++

I'm trying to make an IRC Bot that will log in to an existing account (on QuakeNet), so the Q bot can grant him the operator rank. I can successfully log in to server with a proper nick, but I don't know how to make my bot actually log in to an account. Here is the code I use:
send(cSock, "PASS SuperPasswordOfAnAdmin\r\n", strlen("PASS SuperPasswordOfAnAdmin\r\n"), NULL);
send(cSock, "USER custom 0 0 SuperUsernameOfAnAdmin\r\n", strlen("USER custom 0 0 SuperUsernameOfAnAdmin\r\n"), NULL);
send(cSock, "NICK SuperNickOfAnAdmin\r\n", strlen("NICK SuperNickOfAnAdmin\r\n"), NULL);
And it doesn't seems to work properly. Does anybody know what should I do?
Thanks in advance for any replies.
I would suggest using a client like XChat and manually performing the steps that you're trying to have the bot automate and watching the raw log window. This will show you the commands that are being executed by your client, and anything the server is sending that you'll need to wait on or respond to.
Note that when you're looking at the commands in the QuakeNet documentation, these are client commands, not the actual IRC commands that are sent to the server. For instance, /msg user message here is actually sent over the wire as PRIVMSG user :message here.
I suspect you will have to do somewhat more than what your initial code suggests in order to properly satisfy the IRC server, like handling PING/PONG and waiting for the 001 numeric. In pseudocode:
// connect
conn := Connect("tcp", "your.irc.server:6667")
// login
Fprintf(conn, "PASS %s\r\n", server_password)
Fprintf(conn, "USER %s . . :%s\r\n", username, realname)
Fprintf(conn, "NICK %s\r\n", nick)
forever {
line := ReadLine(conn)
command, args := ParseIRCLine(line)
// welcome message, we're in!
if command == "001" {
break
}
// PING, send PONG
if command == "PING" {
Fprintf(conn, "PONG :%s\r\n", Join(args, " "))
}
}
Fprintf("PRIVMSG Q#CServe.quakenet.org :AUTH %s %s\r\n", username, password)
// wait for response from Q
// join channels, etc
// handle more pings, channel messages, etc
Usually IRC servers have a service called NICKSERV which allows you to identify your account:
You specify the following command:
/nickserv identify [nickname] password
So in your C++ program you would have to send:
send(cSock, "NICKSERV IDENTIFY SuperNickofAnAdmin SuperPasswordOfAnAdmin");
For QBOT you can do:
/msg Q#CServe.quakenet.org AUTH username password
So in your C++ program you send:
send(cSock, "MSG Q#CServe.quakenet.org AUTH SuperNickofAnAdmin SuperPasswordOfAnAdmin");
To grant the user operator status on IRC you will have to send the command:
MODE #Channel +o username
+o is the operator status and will display the '#' before the user's alias.
You can use a telnet client to speak directly with the server. This will require you to use all of the behind-the-scenes commands, which may help familiarize you with the required I/O needed for a running bot.
Source is the top rated answer on this question: How do i program a simple IRC bot in python?

How to detect when a mail is sent in vmime

I send mail from vmime using the following code:
vmime::string urlString;
urlString="smtp://outgoing.verizon.net";
vmime::utility::url url(urlString);
vmime::ref <vmime::net::transport> tr =
g_session->getTransport(url,vmime::create <interactiveAuthenticator>());
// You can also set some properties (see example7 to know the properties
// available for each service). For example, for SMTP:
tr->setProperty("options.need-authentication", true);
tr->setProperty("auth.username", userName);
tr->setProperty("auth.password", password);
fromString=userName+"#verizon.net";
vmime::mailbox from(fromString);
vmime::mailboxList to;
toString = toUserName+"#verizon.net";
to.appendMailbox(vmime::create <vmime::mailbox>(toString));
std::ostringstream data;
data<<subjectId;
// Connect to server
tr->connect();
// Send the message
vmime::string msgData = data.str();
vmime::utility::inputStreamStringAdapter vis(msgData);
tr->send(from, to, vis, msgData.length());
logMsg(DEBUG,2,"Thread Id: %ld,Sent the data in the transaction",pthread_self());
I see that the sent data is succesful from the log.
But when i connect to the box[to which the mail was sent] and check the inbox, i see 0 mails to that inbox.
There is no excepton or error from vmime.
when i connect to the web version of the mail box.Iam unable to see any transactions is sent box, even for the succesful mails.Can anyone help how i can see the mails sent in the sent box?
Thanks in advance.
use any sniffer to catch SMTP traffic from your host. if everything looks good (i.e. SMTP session correct), then it's not a problem of your host, but remote MTA (make sure your email wasn't detected as SPAM & etc)