Sending E-mails in .NET!?
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Sending E-mails in .NET!?

Hybrid View

  1. #1
    Join Date
    Feb 2007
    Posts
    43

    Angry Sending E-mails in .NET!?

    Hi, guys.

    I'm trying to send e-mails from my windows app, with a series of frustrations:
    1) found system::net::mail.
    -Got excited at first, but the problem is I need an SMTP host

    2) my webhost, yahoo, does SMTP!
    -problem is a) it says to use SSL and I don't know how in .NET; b) every username/password combo says Yahoo is disconnecting me, so apparently SSL is required, not optional?

    3) looked for "free" smtp servers
    -they all send their own message with the mail, which looks unprofessional. Also most use SSL, which I don't know how to use.

    Can anyone give me a clear and simple way to get some e-mails delivered from my app, or even give me some hints where/how to start finding info on my own? Ideally, does anyone know of a tutorial? Any ideas at all (even helpful guesses) would be GREATLY appreciated.

  2. #2
    ovidiucucu's Avatar
    ovidiucucu is offline Moderator/Reviewer Power Poster
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,023

    Re: Sending E-mails in .NET!?

    [ Moved thread ]

  3. #3
    Join Date
    May 2009
    Location
    Finland
    Posts
    7

    Re: Sending E-mails in .NET!?

    Hi,

    Example code for using SMTP can be found in MSDN, see
    SmtpClient Class

    To use SSL, check example from here:
    SmtpClient.EnableSsl Property
    Last edited by Loldemar; May 24th, 2009 at 04:06 PM. Reason: fixed typo

  4. #4
    Join Date
    May 2009
    Location
    Finland
    Posts
    7

    Re: Sending E-mails in .NET!?

    Okay I decided to do a C# code example using GMail account, and decided to make a new post for that for clarity reasons (sorry if this is considered spamming). About the bennyboy2's question about how to use SSL, it's quite easy. SmtpClient has a property EnableSsl, which you need to set to true. GMail needs to have your username and password, and those you can set in NetworkCredential object , and pass that to SmtpClient's Credentials-property.

    Below is a very simple sample that sends a test email message. It's very basic and doesn't do anything fancy, but it will get you started anyway.

    Code:
    using System;
    using System.Net.Mail;
    using System.Net;
    
    namespace SmtpTest
    {
      class Program
      {
        static void Main(string[] args)
        {
          Console.WriteLine("Sending test message...");
          
          // call SendTestMessage-method, with GMail server and port as parameters
          SendTestMessage("smtp.gmail.com", 587);
          
          Console.WriteLine("Message sent succesfully!");
                
          Console.ReadLine();
        }
    
        public static void SendTestMessage(string server, int port)
        {
          // message info
          string to = "*****.*****@gmail.com"; // put recipient
          string from = "*****.*****@gmail.com"; // put sender email here
          string subject = "SMTP client using GMail."; // message subject
          string body = "This is our test message, w00t!"; // message body
          
          MailMessage message = new MailMessage(from, to, subject, body);
          
          // Initialize client
          SmtpClient client = new SmtpClient(server, port);
    
          // enable SSL
          client.EnableSsl = true;
          
          // Credentials are necessary if the server requires the client 
          // to authenticate before it will send e-mail on the client's behalf.      
          // Here we define our network credetials (username and password)
          NetworkCredential SmtpUserInfo = new NetworkCredential("your.username@gmail.com", "password");
          
          // set the credentials
          client.Credentials = SmtpUserInfo;
    
          try
          {
            // send message
            client.Send(message);
          }
          catch (SmtpException se)
          {
            Console.WriteLine("SmtpException caught in SendTestMessage(): {0}", se.ToString());
          }
          catch (Exception e)
          {
            Console.WriteLine("Exception caught in SendTestMessage(): {0}", e.ToString());
          }
        }
      }
    }

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center