I am trying to set up an emailprogram, the first code that I try to compile is this where I set an emailTo and emailFrom in the arguments of MailMessage.

When I compile this code, I have an errormessage that says.
'System::Net::Mail::MailMessage' : class does not have a copy-constructor

What does this meen, am I doing this wrong ?

using namespace System::Collections::Generic;
using namespace System::Web;
using namespace System::Net::Mail;
using namespace System::Threading;

String^ EmailRecipient = "testTo@hotmail.com";
String^ EmailFrom= "testFrom@hotmail.com";

MailMessage mm = gcnew MailMessage(EmailRecipient, EmailFrom);

Recommended Answers

All 11 Replies

I might ask the question different as I now understand that it is different to send mail througth C++ and ASP.net.
I about know to to this in ASP.net but not C++.

What I want to accomplish is to send:
Subject: "Hello"
Message: "This is the mailmessage"

from email: testfrom@hotmail.com
to this email: testto@hotmail.com

I think I ask the question that simple for a start.
I have looked at examples on google but there is only very complex ones...

I'm pretty sure you can use .NET inside of C++, but I'm willing to bet it's going to be an uglier mess than simply using sockets and sending the e-mail yourself....but that in and of itself is nasty, because you have to deal with low-level sockets (be wise, use SDL_Net and save the sanity). Worse yet, when dealing with an e-mail host like hotmail.com or yahoo.com, you are going to have to be faced with negotiating with MX garbage (ie: domains of that size, have many e-mail servers... and one main server that tells you which sub-server to use).

Thank you Comatose,

I am prepared to learn all that ugly mess because I really have the ambition to do this in C++. = )
I have done a program in ASP.net on a webpage with a lot of code in C# for ASP.net wich works fine, but the syntax and classes are not really the same in ordinary C++.
However what parameters I have that I dont know how to set up in C++ code is:

EmailTo: To@gmail.com
EmailFrom: From@gmail.com

Subject: "Hello"
BodyMessage: "This message will be sent !"
SmtpClient: "smtp.gmail.com"
Port: 587

Any beginning of code to have a path to start out with would be very appreciated.

Try this..

...
try
{
    SmtpClient client("?Host?");
    client.Port::set(587);
    MailMessage ^msg=gcnew MailMessage();
    msg->To->Add("?dest?");
    //...
    msg->From::set(gcnew MailAddress("?Sender?"));
    client.Send(msg);
    delete msg;
}
catch(System::Net::Mail::SmtpException ^e)
{
   //...Error handle
}
...

Thank you Cikara21 this really got me further very fast as I know a bit how to do it from ASP.net.
I have tried to develop you code a bit where I beleive I need to put UserName and Password for the email that is sending from and also a subject and a body like this.

However if I compile the code, there will be errormessages saying:
'System::Net::NetworkCredential' : class does not have a copy-constructor
'System::Net::Mail:: SmtpClient::Credentials::set' : cannot convert parameter 1 from 'System::Net::NetworkCredential' to 'System::Net::ICredentialsByHost ^'

In ASP.net it works but perheps C++ does this in a little different way when "logging in" to the EmailAccount that will send the email ?

MailMessage^ mm = gcnew MailMessage(From@gmail.com, To@gmail.com);

mm->Subject = "Hello !";
mm->Body = "This message will be sent !";
			 
SmtpClient client(smtp.gmail.com);
client.Port::set(587);

		
try
{

client.EnableSsl = true; //Depending on server SSL Settings
System::Net::NetworkCredential NetworkCred = gcnew System::Net::NetworkCredential(); //My UserName and password;

    NetworkCred.UserName = "From@gmail.com";
    NetworkCred.Password = "This is the password";
    client.UseDefaultCredentials = true;
    client.Credentials = NetworkCred;
	 client.Send(mm);
	 delete mm;
}
	catch(System::Net::Mail::SmtpException^ e)
	{
		//...Error handle
	}

I think you need to get one thing straight when using C++/CLI. When you use gcnew, you're allocating managed memory to a .NET reference. When you use new, you're allocating unmanaged memory to a C++ pointer. I added the pointer part because the syntax is equivalent (pointers use * and new, .NET references use ^ and gcnew):

// Unmanaged
T *p = new T();

// Managed
T ^p = gcnew T();

You seem to habitually omit the ^ operator when allocating managed memory.

Thanks Narue, I did get the idéa of how that is built up then, so I did a change for that also and it works.

I do have 2 questions. I have set up the code so the code loops through 2 emails wich works fine. I have put some code outside the loop to make it goes faster.

1. What I wonder is if there is anything else I can do to make the loop to send these 2 emails faster or is this the fastest possible way to set up this ?

2. Then I wonder for this catch().
What happens in the catch(), I know that it is an exception, does it meen that the email wasn´t sent succesfully ? Is this when the catch is called and important to know, if there will be any errorbox(MessageBox) showing if this happens or if it only happens internally in the program where I can save this to a logfile or myself show a MessageBox ?

SmtpClient client(smtp.gmail.com);
client.Port::set(587);
MailMessage^ mm; //Declare

client.EnableSsl = true; //Depending on server SSL Settings
System::Net::NetworkCredential^ NetworkCred = gcnew System::Net::NetworkCredential(); //My UserName and password;
NetworkCred->UserName = "From@gmail.com";
NetworkCred->Password = "Password";
client.UseDefaultCredentials = true;
client.Credentials = NetworkCred;


	 List<String^>^ EmailList = gcnew List<String^>;
	 EmailList->Add("To1@hotmail.com");
	 EmailList->Add("To2@hotmail.com");



	 //Send 2 Emails;
	 for( int i = 0; i < 2; i++)
	 {
						
	 mm = gcnew MailMessage(EmailFrom->Trim(), EmailList[i]->Trim());
	 mm->Subject = "Hello";
	 mm->Body = "Message";

		
		try
		{
			 client.Send(mm);
			 delete mm;
		}
		catch(System::Net::Mail::SmtpException^ e)
		{
			//...Error handle
		}
	}

I have used the code above to send 10 emails to myself in that loop.

As I notice it takes about 1 second per email to be delievered.

Why does it take so long time ?
For example why will not all 10 emails be sent in only 1 second ?
What is the bottleneck here ?

I wonder what could be possible to do, to increase this speed ?

I'd wager the code is blocking... waiting for the network. Maybe I'm wrong, but usually in a program like that (that sends a bunch of data over a network) the bottleneck and wait time is the network.

For example why will not all 10 emails be sent in only 1 second ?

Why would you want to do that anyway? Smells like an email-bomb to me, and that would be against the rules.... :icon_wink:

In a way yes, honestly but it is for about 3000 customers for my company where I will give them advertisment and as the customers increases there would be good to be able to send the email quickly.

As I understand this depends of the smpt.gmail.com server but I have heard and red that this could be sent through my own computers instead of that server.

In other word use my computer as a smtp server ?
Should that meen that I could add or change some code in my emailcode now or how would that work.

Thank you...

Why would you want to do that anyway? Smells like an email-bomb to me, and that would be against the rules.... :icon_wink:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.