Simple way to send string to a gmail?

is there a simple way to send a string to my gmail? ive read a few sources of people doing this but i still dont have a good understanding of how to do this in cpp to a service that has ssl/tls authentication. this is what i found from a old video of him using it with gmail

1
2
3
4
5
6
MailMessage^ mail = gcnew MailMessage(textBox_FROM->Text, textBox_TO->Text, textBox_SUBJECT->Text, textBox_BODY->Text);
		SmtpClient^ client = gcnew SmtpClient(textBox_SMTP->Text);
		client->Port = 587;
		client->Credentials = gcnew System::Net::NetworkCredential(textBox_USERNAME->Text, textBox_PASSWORD->Text);
		client->EnableSsl = true;
		client->Send(mail);
Last edited on
Your code looks fine and works perfectly on my machine.
Since many things can go wrong I would wrap the code in a try catch block.
do you think theres a setting thats not allowing it to go through? i get this error about 3 seconds after i click the button
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

and with the port 465 for ssl i get this error about 12 seconds after clicking the button
An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

Additional information: Failure sending mail.

i looked up a try/catch block and dont understand where to put certain parts of the code, i see
1
2
3
4
5
try {
   // protected code
}catch(...) {
   // code to handle any exception
}


would i put what i have above inside of try, and what would go inside the catch when the exception is thrown?

also i just changed it to a clr console application so i dont have to fill in the text boxes each time just to save a little bit of time.

1
2
3
4
5
6
MailMessage^ mail = gcnew MailMessage("*****@gmail.com", "*****@gmail.com", "mxs", "testmessage");
	SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
	client->Port = 465;
	client->Credentials = gcnew System::Net::NetworkCredential("*****@gmail.com", "*****");
	client->EnableSsl = true;
	client->Send(mail);
Last edited on
I works for me with port 587
Try this code.

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
#using <mscorlib.dll>

using namespace System;
using namespace System::Net::Mail;
using namespace System::Net;

int main()
{
  try
  {
    MailMessage^ mail = gcnew MailMessage("******@gmail.com", 
                                          "*******@gmail.com",
                                          "Test", "Hi ");

    SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
    client->Port = 587;
    client->Credentials = gcnew NetworkCredential("******",  "*******");
    client->EnableSsl = true;
    client->Send(mail);
    Console::WriteLine("Mail sent");
  }
  catch (Exception^ ex)
  {
    Console::WriteLine("\aERROR: " + ex->Message);
  }

    Console::ReadKey();
    return 0;
}


so i dont have to fill in the text boxes each time just to save a little bit of time.

Console apps are good for testing. However you can set the textboxes also in the Properties inspector.
true i forgot about that for the textboxes. heres the error it throws in my console window with port 587

ERROR: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

heres the error with port 465
ERROR: Failure sending mail.

ssl uses port 465 i think so im assuming thats why port 587 throws the not authenticated error
not authenticated error

Sounds more like invalid username or password. Try the userword without @gmail
haha i just tried it with my secondary email as the sender and got a Review blocked sign-in attempt email from google. i think theres a setting i need to disable or allow somewhere, im going to look for that but it was the username, i was using my actual username when it was everything before @gmail

edit: yep that worked, it was because of the username and having the access for less secure apps turned off. thank you very much sir that helped a ton. just need to figure out how come i cant send them from myself to myself but thats not a big deal. i think its because of 2 step verification
Last edited on
ok so everything is working as intended, however when i gave it to my friend to test it threw this error for him

https://gyazo.com/dc2bf2c288c740b023184041b4fbfa12

1
2
3
4
5
6
7
8
9
String^ regkey = gcnew String(readme.c_str()); // convert readme to system::string named regkey
		MailMessage^ mail = gcnew MailMessage("*****@gmail.com", "*****@gmail.com", "MXS from: " + username, "key: " + *****);

		SmtpClient^ client = gcnew SmtpClient("smtp.gmail.com");
		client->Port = 587;
		client->Credentials = gcnew System::Net::NetworkCredential("*****188", "*****");
		client->EnableSsl = true;
		client->Send(mail);
		Console::WriteLine("Mail sent");


edit: i set my network credential's to my actual email and that solved one issue, but now when he runs it it says failed to send mail
https://gyazo.com/fabd07794bdd5f9048b99441f9ff65c1
Last edited on
Topic archived. No new replies allowed.