locked
Send & receive mail using C#.net RRS feed

  • Question

  • Hi,

         anybody can guide me in such I want to send and receive mail with proxy server userid and password and I dont my server address; smpt port no and how to find this.
    pls help

    do things different
    • Moved by CoolDadTxModerator Friday, September 5, 2008 1:27 PM Unrelated to IDE, moving (Moved from Visual C# IDE to Visual C# General)
    • Moved by Peter RitchieModerator Friday, September 5, 2008 5:37 PM networking question (Moved from Visual C# General to .NET Framework Networking and Communication)
    Friday, September 5, 2008 6:28 AM

All replies

  • You can do this by using SMTP server using C#.
    private void SendBtn_Click(object sender, System.EventArgs e)
    {
        // change cursor into wait cursor
        Cursor cr = Cursor.Current;
        Cursor.Current = Cursors.WaitCursor;

        // create server SMTP with port 25
        TcpClient SmtpServ = new TcpClient(ServSMTP.Text,25);
        string Data;
        byte[] szData;
        string CRLF = "\r\n";
        LogList.Items.Clear();           

        try
        {
            // initialization
            NetworkStream NetStrm = SmtpServ.GetStream();
            StreamReader  RdStrm= new StreamReader(SmtpServ.GetStream());
            LogList.Items.Add(RdStrm.ReadLine());
               
            // say hello to server and send response into log report
            Data = "HELLO server " + CRLF;               
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());
            // send sender data
            Data = "MAIL FROM: " + "<" + sFrom.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send receiver data
            Data = "RCPT TO: " + "<" + sTo.Text + ">" + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());

            // send DATA
            Data = "DATA " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // send content data
            Data = "SUBJECT: " + sSubject.Text + CRLF +
               sMessage.Text + CRLF + "." + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // quit from server SMTP
            Data = "QUIT " + CRLF;
            szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
            NetStrm.Write(szData,0,szData.Length);
            LogList.Items.Add(RdStrm.ReadLine());               

            // close connection
            NetStrm.Close();
            RdStrm.Close();
            LogList.Items.Add("Close connection");
            LogList.Items.Add("Send mail successly..");

            // back to normal cursor
            Cursor.Current = cr;

        }
        catch(InvalidOperationException err)
        {
            LogList.Items.Add("Error: "+ err.ToString());
        }
    }

    Hope this helped.

    Friday, September 5, 2008 12:25 PM
  • 1. You can't send and receive mail without having a server address and a port number for SMTP. 

    2. There's a much easier way to send mail via SMTP using C#.  Check out the System.Net.Mail namespace. Another .NET user, Dave Wanta, has created an excellent site on how to use the System.Net.Mail namespace, at www.systemnetmail.com.  Here's the MSDN link as well:

    http://msdn.microsoft.com/en-us/library/system.net.mail.aspx


    David Morton - http://blog.davemorton.net/
    Friday, September 5, 2008 2:45 PM
  • hi sethluri,

     canI what are namespace should I include

    do things different
    Monday, September 8, 2008 3:56 AM
  • Use System.Net,System.Net.Mail and System.IO Namespaces
    Gaurav Khanna
    • Edited by Khanna Gaurav Monday, September 8, 2008 2:09 PM change
    Monday, September 8, 2008 2:09 PM
  •  HI
     
                can you tell me how to retrieve the mail from the inbox  in ASP.net(C#)
    do things different
    Thursday, October 23, 2008 5:23 AM
    • Proposed as answer by Khanna Gaurav Monday, December 8, 2008 9:29 AM
    Thursday, October 23, 2008 2:06 PM