How to email a simple plain text on windows form applications?

I using visual studio 2010 C++ Windows form application with net framework 4 and I want to send a simple text email without any login requirement. The code will read the text in richtextbox1 and email it to my email. which is custom email for example : example@custom.com
Try using Winsock with a SMTP protocol. I'm not sure if it comes with Winsock.
Last edited on
Can you give a code, I'm doing research on this topic for 13 hours now and I sick and tired of searching. I search the whole Google and there isn't a single answer.
Last edited on
Just do a quick search on SMPT with Winsock in Google and I'm sure you'll come up with something. I don't think it's possible to send an email without a login or server, which is a good thing except for the fact that you spent 13 hours searching for how, and probably explains your 13 hours of research without any answers.
Last edited on
I found one answer but I couldn't get it to work let me show it to you: http://msdn.microsoft.com/en-us/library/system.net.mail.smtpclient.aspx#Y2901

It's there i get 8 errors no idea why can you try that code and tell me if it works for you
Did you enable the CLR option?
I think it's enabled.
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!");
}
}


I got this part of the code in button click event and i think all of it is not meant to be in here but the rest of the code is in right place for sure.
It compiled fine for me. What kind of errors are you getting?
i got another from another guy it compiles fine however when i click to send i get this error: An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

And this the error:

using namespace System::Net::Mail;

private: System::Void button1_Click(System::Object^ sender, System::EventArgs^ e) {
SmtpClient^ sc = gcnew SmtpClient();
sc->Host = "localhost";
sc->Port = 25;

MailMessage^ mm = gcnew MailMessage();
mm->From = gcnew MailAddress("user@source");
mm->To->Add(gcnew MailAddress("example@custom.com"));

mm->Body = richTextBox1->Text;

sc->Send(mm);
}
};
}
Disable C++ Exceptions? If that fixes it temporarily then it could be a problem in the source code with exceptions.
Last edited on
Topic archived. No new replies allowed.