Send Email Message using C#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Atran
    Contributor
    • May 2007
    • 319

    Send Email Message using C#

    Hello Everybody, I want to send a message to somebody email using C# I have the right code but when I run the application it shown the "ArgumentExecpt ion was unhandled" error, it is been yellow, it stop the application from running.
    Here is the code:

    Code:
    using System;
    using System.Net.Mail;
    using System.Collections.Generic;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        class Program
        {
            static void Main(string[] args)
            {
                string to = "myEmail@hotmail.com";
                string from = "myEmail@hotmail.com";
                MailMessage message = new MailMessage(from, to);
                message.Subject = "New Car";
                message.Body = @"Hello Friend, I buy a new car today.";
                SmtpClient client = new SmtpClient("MyServerName");
                client.UseDefaultCredentials = true;
                client.Send(message);
            }
        }
    }
    The code is true, but it doesnt send, and it shown the yello error when the code arrived to "client.Send(me ssage)".
    Can anyone help me.
  • oohay251
    New Member
    • May 2007
    • 27

    #2
    post the exact output of your exception.

    Comment

    • myrrdin
      New Member
      • May 2007
      • 6

      #3
      What line is it throwing the error on?

      Comment

      • Atran
        Contributor
        • May 2007
        • 319

        #4
        At "client.Send(me ssage);" line, the program stop running, this is the exact error:

        Code:
        A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll

        This is the all output text:

        Code:
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\mscorlib\2.0.0.0__b77a5c561934e089\mscorlib.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Windows.Forms\2.0.0.0__b77a5c561934e089\System.Windows.Forms.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System\2.0.0.0__b77a5c561934e089\System.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Drawing\2.0.0.0__b03f5f7f11d50a3a\System.Drawing.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\Microsoft.VisualStudio.HostingProcess.Utilities.Sync\8.0.0.0__b03f5f7f11d50a3a\Microsoft.VisualStudio.HostingProcess.Utilities.Sync.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\Ramsin Khoshaba\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.vshost.exe', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_32\System.Data\2.0.0.0__b77a5c561934e089\System.Data.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Xml\2.0.0.0__b77a5c561934e089\System.Xml.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        The thread 0x194 has exited with code 0 (0x0).
        The thread 0xb88 has exited with code 0 (0x0).
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\Ramsin Khoshaba\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe', Symbols loaded.
        'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\WINDOWS\assembly\GAC_MSIL\System.Configuration\2.0.0.0__b03f5f7f11d50a3a\System.Configuration.dll', Skipped loading symbols. Module is optimized and the debugger option 'Just My Code' is enabled.
        A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
        thanks for anyhelp.

        Comment

        • oohay251
          New Member
          • May 2007
          • 27

          #5
          Your code runs fine on my system.
          Likely your credentials.
          Or maybe your hostname cannot be resolved??

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Originally posted by Atran
            Hello Everybody, I want to send a message to somebody email using C# I have the right code but when I run the application it shown the "ArgumentExecpt ion was unhandled" error, it is been yellow, it stop the application from running.
            Here is the code:

            Can anyone help me.
            Could it be that your mail provider requires you to log in in order to use their services?

            In this case you'll have to provide credentials in order to connect to the mail server.

            Check out this .NET article on how to send an email for an example on how to provide email credentials.

            Cheers!

            -Frinny

            Comment

            • Atran
              Contributor
              • May 2007
              • 319

              #7
              Thanks for yours help, But it still not work, I try that from many ways.
              and how to connect to the mail server?

              Comment

              • mwalts
                New Member
                • May 2007
                • 38

                #8
                Originally posted by Atran
                Thanks for yours help, But it still not work, I try that from many ways.
                and how to connect to the mail server?
                Connecting to the mail server is pretty clearly defined in the Quick Reference linked above.

                Based on your exception, I'm guessing you have .NET 2.0
                That means you should look under the "if you are using .NET Framework version 2.0" section.

                For connecting to the mail server, that is covered on the Net.Mail.SmtpCl ient lines.

                It really is all there, if there is a line you don't understand, you can post that here and we can probably help you

                -mwalts

                Comment

                • oohay251
                  New Member
                  • May 2007
                  • 27

                  #9
                  Well, doesn't your mail provider give you support on how to set up a mail client like Outlook or Eudora?
                  If so, what settings do they tell you to use?

                  If you have a network administrator what do they tell you to use?

                  Comment

                  Working...