By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,816 Members | 2,053 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,816 IT Pros & Developers. It's quick & easy.

Send Email Message using C#

Atran
100+
P: 319
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 "ArgumentExecption was unhandled" error, it is been yellow, it stop the application from running.
Here is the code:

Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Net.Mail;
  3. using System.Collections.Generic;
  4. using System.Text;
  5.  
  6. namespace ConsoleApplication1
  7. {
  8.     class Program
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             string to = "myEmail@hotmail.com";
  13.             string from = "myEmail@hotmail.com";
  14.             MailMessage message = new MailMessage(from, to);
  15.             message.Subject = "New Car";
  16.             message.Body = @"Hello Friend, I buy a new car today.";
  17.             SmtpClient client = new SmtpClient("MyServerName");
  18.             client.UseDefaultCredentials = true;
  19.             client.Send(message);
  20.         }
  21.     }
  22. }
  23.  
The code is true, but it doesnt send, and it shown the yello error when the code arrived to "client.Send(message)".
Can anyone help me.
May 27 '07 #1
Share this Question
Share on Google+
8 Replies


P: 27
post the exact output of your exception.
May 27 '07 #2

P: 6
What line is it throwing the error on?
May 28 '07 #3

Atran
100+
P: 319
At "client.Send(message);" line, the program stop running, this is the exact error:

Expand|Select|Wrap|Line Numbers
  1. A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
  2.  

This is the all output text:

Expand|Select|Wrap|Line Numbers
  1. '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.
  2. '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.
  3. '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.
  4. '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.
  5. '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.
  6. '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.
  7. '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.
  8. '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.
  9. '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.
  10. The thread 0x194 has exited with code 0 (0x0).
  11. The thread 0xb88 has exited with code 0 (0x0).
  12. 'ConsoleApplication1.vshost.exe' (Managed): Loaded 'C:\Documents and Settings\Ramsin Khoshaba\Local Settings\Application Data\Temporary Projects\ConsoleApplication1\bin\Debug\ConsoleApplication1.exe', Symbols loaded.
  13. '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.
  14. A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll
  15.  
  16.  
thanks for anyhelp.
May 28 '07 #4

P: 27
Your code runs fine on my system.
Likely your credentials.
Or maybe your hostname cannot be resolved??
May 28 '07 #5

Frinavale
Expert Mod 5K+
P: 9,731
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 "ArgumentExecption 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
May 28 '07 #6

Atran
100+
P: 319
Thanks for yours help, But it still not work, I try that from many ways.
and how to connect to the mail server?
May 28 '07 #7

P: 38
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.SmtpClient 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
May 28 '07 #8

P: 27
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?
May 28 '07 #9

Post your reply

Sign in to post your reply or Sign up for a free account.