C# email

This is a discussion on C# email within the C# Programming forums, part of the General Programming Boards category; I am trying to send email with this Code: System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(); message.To.Add("mixalissen@hotmail.com"); message.Subject = "This is the ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    44

    C# email

    I am trying to send email with this
    Code:
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                message.To.Add("mixalissen@hotmail.com");
                message.Subject = "This is the Subject line";
                message.From = new System.Net.Mail.MailAddress("erg@efrge.com");
                message.Body = "This is the message body";
                System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
                smtp.Send(message);
    but I get


    System.Net.Mail.SmtpException was unhandled
    Message="Failure sending mail."
    Source="System"
    StackTrace:
    at System.Net.Mail.SmtpClient.Send(MailMessage message)
    at eMail.Program.Main(String[] args) in C:\Documents and Settings\mixalis\Τα έγγραφά μου\Visual Studio 2005\Projects\eMail\eMail\Program.cs:line 18
    at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args)
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
    at Microsoft.VisualStudio.HostingProcess.HostProc.Run UsersAssembly()
    at System.Threading.ThreadHelper.ThreadStart_Context( Object state)
    at System.Threading.ExecutionContext.Run(ExecutionCon text executionContext, ContextCallback callback, Object state)
    at System.Threading.ThreadHelper.ThreadStart()

    any ideas???

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,574
    I'm not knowledgeable around C#, but It figure that your SmtpClient instance is needs more information.

    For one, you'll need to have a smtp server installed at localhost for that to work, and it would need to be setup so that no credentials would be needed. Try instead to pass your ISP smtp server address and credentials, just like you had it setup in your email client. That should work. MSDN has all information on the SmtpClient class.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    You are missing a lot of stuff.
    First of all, I suggest you to type these at the top of your code:
    Code:
    using System.Net;
    using System.Net.Mail;
    Then you could just create instances more easily (without this whole string)
    Alright,
    In order to get it working, you'll have to supply your network credential: you'll be doing it this way:
    Code:
    smtp.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
    Now, if you want to make it work you must run an SMTP server on your localhost or use existent one (e.g: smtp.gmail.com)
    Anyways, here's a little code I wrote:
    Code:
                SmtpClient client = new SmtpClient("smtp.gmail.com", 25);
                client.Credentials = new NetworkCredential("USERNAME", "PASSWORD");
    
                MailMessage mail = new MailMessage(new MailAddress("FROM@gmail.com"), new MailAddress("TO@gmail.com"));
    
                mail.Subject = "Hello world!";
                mail.Body = "Hello world!";
    
                client.EnableSsl = true; // gmail uses SSL
    
                client.Send(mail);

  4. #4
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    First of all, I suggest you to type these at the top of your code:
    I suggest you don't. Don't pollute the global namespace!
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    242
    Quote Originally Posted by Magos View Post
    I suggest you don't. Don't pollute the global namespace!
    Why not? it's annoying to type this over and over again when you create an instance of a new class in this namespace.

  6. #6
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    Readability. To quote Eric Gunnerson:

    Most people agree that the average line of source code is read far more times than it is written, which means that being able to understand the line is more important than being able to write it quickly.
    A more concrete example, a random code sample found through googling. The author omitted the namespace notation (and also omitted the "using" notation) so the reader has no indication in which namespace the classes Serializable, Int32 & List can be found (apart from your own experience, but not all cases are as easy as this).

    And there's always the classic case of collisions. Try to compile this sample:
    Code:
    using System;
    
    class Console
    {
    }
    
    Console.PrintLine("Hello");
    Not so fun when the compiler starts screaming, eh? Now try this:

    Code:
    class Console
    {
    }
    
    System.Console.PrintLine("Hello");
    Much better. Not only do you have a working code, it's much clearer in the code you're printing to the system console.

    Again, an overly simplified example but the principle stands.
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Spam Filters. ISP based email is dying.
    By Mario F. in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 03-05-2008, 12:05 PM
  2. Email Server on c#!
    By andhikacandra in forum C# Programming
    Replies: 0
    Last Post: 10-09-2007, 09:49 PM
  3. Open Source Email Server!
    By andhikacandra in forum Tech Board
    Replies: 2
    Last Post: 10-02-2007, 11:51 PM
  4. MAPI....how to specify FROM email
    By freestyle in forum Windows Programming
    Replies: 0
    Last Post: 05-13-2003, 09:55 AM
  5. email client with C++
    By mwagiru in forum C++ Programming
    Replies: 0
    Last Post: 02-14-2002, 01:17 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21