CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2011
    Posts
    15

    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

  2. #2
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

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

    The .NET framework has a class (or rather a set of related classes) that makes sending mails via SMTP relatively simple. I think a good point to start reading about that is http://msdn.microsoft.com/en-us/libr...mtpclient.aspx.

    However, as you have a rich text box as the source of your mail text, you may want to send it formatted which isn't that easy anymore. In particular sending RTF-formatted text incurs some specific problems that have recently been discussed in http://www.codeguru.com/forum/showthread.php?t=512840, with not very optimistic conclusions. OTOH sending the formatted text as HTML, which is the common way of doing that, of course requires format conversion/HTML generation which is additional effort.

    BTW, when you talk about "without any login requirement", I think the best you can reasonably get of course is that yor mailing application holds the credentials to log into the SMTP server (or obtains them from Windows) and just the user doesn't need to enter any login. The only alternative to that, out on the net, that comes to my mind now are SMTP servers that allow anonymous login, but I think they'll always have a bad reputation (or will quickly get one) due to being (ab)used to distribute spam. A corporate SMTP server that only allows anonymous loging from inside the corporate network might be an alternative, but if you have that, then why not simply obtain login credentials for your program from the admin in the first place?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  3. #3
    Join Date
    Jan 2011
    Posts
    15

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

    thanks for the reply i have this code :

    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("bad@nysc");
    mm->To->Add(gcnew MailAddress("emre@nyeotech.com"));

    mm->Body = richTextBox1->Text;

    sc->Send(mm);
    }
    };
    }

    It compiles fine, but when i click the button I get this error: An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

  4. #4
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

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

    Quote Originally Posted by emreozpalamutcu View Post
    It compiles fine, but when i click the button I get this error: An unhandled exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
    And what is the descriptive message carried by the exception? It usually is right next to the exception type mentioned in the exception message box. These messages usually are quite informative in the .NET framework.

    And you definitely do have an SMPT server running on your localhost?
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

  5. #5
    Join Date
    Jan 2011
    Posts
    15

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

    here is the expection
    Additional information: Failure sending mail.

    thats all other info and what you mena smpt server running on my local host how to do that and if other people use this program do they have to do it to?

  6. #6
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

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

    Quote Originally Posted by emreozpalamutcu View Post
    here is the expection
    Additional information: Failure sending mail.

    thats all other info [...]
    Ok, I dug out the log files from the early tests of my own little command line mailer and it turned out that, in case the SMTP server is not found, the actual interesting info is hidden inside the inner exception which is available as the exception's InnerException property. Are you catching and displaying the exception yourself? (At least it looks like that because the .NET runtime's built-in exception message box displays much more information.) In that case I suggest you output e->ToString() rather than e->Message. The ToString() member of the .NET exceptions retrieves a bunch of useful information in a single call. I use it all the time.

    and what you mena smpt server running on my local host how to do that and if other people use this program do they have to do it to?
    If you don't know whether you have one, then you probably don't have one.

    Everybody (you, your users, me etc.) needs an SMTP server to send mail (as I already implied in post #2). An SMTP client like your program or my little command line mailer (or Outlook or Thunderbird for that matter) merely commits the mail(s) to the server which in turn initiates the actual process of transporting the mail over the net. So the SMTP server can be seen as kind of a post box.

    You, as well as your users, need the proper credentials (user name and password) to log into the server. To free the user from entering the credentials everytime he/she wants to send mails, the client usually is configured to remember the credentials. But of course that does not mean you don't need them. My own mailer, for instance, currently can acquire the credentials from the command line, its app config file or Windows (in case the user has the same credentials on Windows and the SMTP server).
    I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

    This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured