954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to send and Email?

I'm working in this project that has a feedback form and in this feedback form there is only body, so it doesn't ask for subject and who it's from. And all the messages send from will go to a custom mail account, for example [email]Example@custom.com[/email] How can I do this in the simplest way, without need of outlook and I'm using net framework 4 windows form applications.

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Look for details on the System::Net::Mail::SmtpClient class. It's dead easy to send email with .NET.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I done a search on google for so long but every one does it on win32 and the ones does in on .net have the subject and to and ask you to enter your details which is not what i want. Any codes you could help me with? or tutorials or websites has what im looking for

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

To see why I don't believe that sorry excuse, click here . That page took me all of 5 seconds to find, and it has a full working program in C++/CLI.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I'm getting hell of a lot errors!

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Any other help?

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

This might help.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;

static bool mailSent;

static void SendCompletedCallback(Object^ sender, AsyncCompletedEventArgs^ e)
{
    // Get the unique identifier for this asynchronous 
    // operation.
    String^ token = (String^) e->UserState;

    if (e->Cancelled)
    {
        Console::WriteLine("[{0}] Send canceled.", token);
    }
    if (e->Error != nullptr)
    {
        Console::WriteLine("[{0}] {1}", token, 
            e->Error->ToString());
    } else
    {
        Console::WriteLine("Message sent.");
    }
    mailSent = true;
}

int main(array<String^>^ args)
{
    if (args->Length > 1)
    {
        // Command line argument must the the SMTP host.
        SmtpClient^ client = gcnew SmtpClient(args[1]);
        // Specify the e-mail sender.
        // Create a mailing address that includes a UTF8 
        // character in the display name.
        MailAddress^ from = gcnew MailAddress("jane@contoso.com",
            "Jane " + (wchar_t)0xD8 + " Clayton",
            System::Text::Encoding::UTF8);
        // Set destinations for the e-mail message.
        MailAddress^ to = gcnew MailAddress("ben@contoso.com");
        // Specify the message content.
        MailMessage^ message = gcnew MailMessage(from, to);
        message->Body = "This is a test e-mail message sent" +
            " by an application. ";
        // Include some non-ASCII characters in body and 
        // subject.
        String^ someArrows = gcnew String(gcnew array<wchar_t>{L'\u2190', 
            L'\u2191', L'\u2192', L'\u2193'});
        message->Body += Environment::NewLine + someArrows;
        message->BodyEncoding = System::Text::Encoding::UTF8;
        message->Subject = "test message 1" + someArrows;
        message->SubjectEncoding = System::Text::Encoding::UTF8;
        // Set the method that is called back when the send
        // operation ends.
        client->SendCompleted += gcnew
            SendCompletedEventHandler(SendCompletedCallback);
        // The userState can be any object that allows your 
        // callback method to identify this send operation.
        // For this example, the userToken is a string constant.
        String^ userState = "test message1";
        client->SendAsync(message, userState);
        Console::WriteLine("Sending message... press c to" +
            " cancel mail. Press any other key to exit.");
        String^ answer = Console::ReadLine();
        // If the user canceled the send, and mail hasn't been 
        // sent yet,then cancel the pending operation.
        if (answer->ToLower()->StartsWith("c") && mailSent == false)
        {
            client->SendAsyncCancel();
        }
        // Clean up.
        delete message;
        client = nullptr;
        Console::WriteLine("Goodbye.");
    }
    else
    {
        Console::WriteLine("Please give SMTP server name!");
    }
}
emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Compiles and runs fine, there is nothing wrong with it!

This is the example code from the link that Narue directed you to in your other thread isn't it?

Exactly what problems are you having?

JasonHippy
Master Poster
772 posts since Jan 2009
Reputation Points: 590
Solved Threads: 125
 

Threads merged.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

I'm getting stupid errors. When you click the button this message should be sent I'm sure i placed everything in the right place, i get no errors compile with

#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Net::Mail;
using namespace System::Net::Mime;
using namespace System::Threading;
using namespace System::ComponentModel;


However i place the rest on the code on the button click event i think that's where i went wrong and where should i place it

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Dude, what are the errors? Nobody can help if you don't tell us what's wrong.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

1) Error 4 error C2065: 'mailSent' : undeclared identifier c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 148

2) Error 5 error C2065: 'SendCompletedCallback' : undeclared identifier c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 180

3) Error 1 error C2267: 'SendCompletedCallback' : static functions with block scope are illegal c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 131

4) Error 3 error C2601: 'main' : local function definitions are illegal c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 152

5) Error 2 error C2601: 'SendCompletedCallback' : local function definitions are illegal c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 131

6) Error 6 error C3350: 'System::Net::Mail::SendCompletedEventHandler' : a delegate constructor expects 2 argument(s) c:\users\administrator\documents\visual studio 2010\projects\nyeotech system cleaner\nyeotech system cleaner\Bad.h 180

some of the errors came up twice I just put it once. But I'm definitely sure it's about where i placed the code

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 
#pragma endregion
	private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
				

				 static bool mailSent;

				 static void SendCompletedCallback(Object^ sender, AsyncCompletedEventArgs^ e)
				 {
					 // Get the unique identifier for this asynchronous 
					 // operation.
					 String^ token = (String^) e->UserState;

					 if (e->Cancelled)
					 {
						 Console::WriteLine("[{0}] Send canceled.", token);
					 }
					 if (e->Error != nullptr)
					 {
						 Console::WriteLine("[{0}] {1}", token, 
							 e->Error->ToString());
					 } else
					 {
						 Console::WriteLine("Message sent.");
					 }
					 mailSent = true;
				 }

				 int main(array<String^>^ args)
				 {
					 if (args->Length > 1)
					 {
						 // Command line argument must the the SMTP host.
						 SmtpClient^ client = gcnew SmtpClient(args[1]);
						 // Specify the e-mail sender.
						 // Create a mailing address that includes a UTF8 
						 // character in the display name.
						 MailAddress^ from = gcnew MailAddress("jane@contoso.com",
							 "Jane " + (wchar_t)0xD8 + " Clayton",
							 System::Text::Encoding::UTF8);
						 // Set destinations for the e-mail message.
						 MailAddress^ to = gcnew MailAddress("ben@contoso.com");
						 // Specify the message content.
						 MailMessage^ message = gcnew MailMessage(from, to);
						 message->Body = "This is a test e-mail message sent" +
							 " by an application. ";
						 // Include some non-ASCII characters in body and 
						 // subject.
						 String^ someArrows = gcnew String(gcnew array<wchar_t>{L'\u2190', 
							 L'\u2191', L'\u2192', L'\u2193'});
						 message->Body += Environment::NewLine + someArrows;
						 message->BodyEncoding = System::Text::Encoding::UTF8;
						 message->Subject = "test message 1" + someArrows;
						 message->SubjectEncoding = System::Text::Encoding::UTF8;
						 // Set the method that is called back when the send
						 // operation ends.
						 client->SendCompleted += gcnew
							 SendCompletedEventHandler(SendCompletedCallback);
						 // The userState can be any object that allows your 
						 // callback method to identify this send operation.
						 // For this example, the userToken is a string constant.
						 String^ userState = "test message1";
						 client->SendAsync(message, userState);
						 Console::WriteLine("Sending message... press c to" +
							 " cancel mail. Press any other key to exit.");
						 String^ answer = Console::ReadLine();
						 // If the user canceled the send, and mail hasn't been 
						 // sent yet,then cancel the pending operation.
						 if (answer->ToLower()->StartsWith("c") && mailSent == false)
						 {
							 client->SendAsyncCancel();
						 }
						 // Clean up.
						 delete message;
						 client = nullptr;
						 Console::WriteLine("Goodbye.");
					 }
					 else
					 {
						 Console::WriteLine("Please give SMTP server name!");
					 }
				 }



			 }
	};
}

And that's how i placed the code. Other part is in the right place at the very top

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

Any ideas yet?

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

I'd suggest learning C++/CLI. You clearly don't realize that functions can't be nested, which means you lack critical prerequisite knowledge to write this program. When I said emailing in .NET was easy, that was assuming you actually know how to write basic C++/CLI code.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Just tell me how to solve this I will start improving my knowledge, i just moved on from visual basic. So how do i solve this.

emreozpalamutcu
Junior Poster in Training
78 posts since Dec 2010
Reputation Points: 10
Solved Threads: 0
 

#using statement doesnot work with rad studio XE, says "unknown preprocessor directive" what should i do? i would like to know how to link dlls in rad studio xe

cent91
Newbie Poster
17 posts since Oct 2009
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: