hello every one
i'm trying to send emails via my application
i used smtp, but it throw exception
someone told me to login using pop3 first, but it throws the exception
"'220 servername ESMTP"
here is my code

static void DoPopAuth(string host, string user, string passphrase)
        {
            TcpClient cli = new TcpClient();
            cli.ReceiveTimeout = TimeoutMs;
            cli.SendTimeout = TimeoutMs;
            cli.Connect(host, 587);
            using (Stream peer = cli.GetStream())
            {
                StreamWriter wtr = new StreamWriter(peer); // ASCII?  
                Debug.Assert("\x000D\x000A" == wtr.NewLine, "CRLF!");
                StreamReader rdr = new StreamReader(peer); // ASCII?  
                String line;
                //  
                line = rdr.ReadLine();
                if (line == null)
                    throw new EndOfStreamException("Closed by peer at: Connect");
                Debug.WriteLine("Received: '{0}'", line);
                if (!line.StartsWith(SuccessCode, StringComparison.Ordinal))
                    throw new InvalidOperationException("Error at connect: '" + line + "'");
                // TODO check if host supports APOP. Regex(".*{<.*@.*>}");  
                //  
                wtr.WriteLine("USER {0}", user);
                wtr.Flush();
                line = rdr.ReadLine();
                if (line == null)
                    throw new EndOfStreamException("Closed by peer at: USER");
                Debug.WriteLine("Received: '{0}'", line);
                if (!line.StartsWith(SuccessCode, StringComparison.Ordinal))
                    throw new InvalidOperationException("Error on USER: '" + line + "'");
                //  
                wtr.WriteLine("PASS {0}", passphrase);
                wtr.Flush();
                line = rdr.ReadLine();
                if (line == null)
                    throw new EndOfStreamException("Closed by peer at: PASS");
                Debug.WriteLine("Received: '{0}'", line);
                if (!line.StartsWith(SuccessCode, StringComparison.Ordinal))
                    throw new InvalidOperationException("" + line + "'");
                // If we got here success!  
            }
        }

and i call this method here
DoPopAuth("mail1.dnsegypt.net", "mymail", "mypass");
any help would be apprecited

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
//using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace celluloid
{
    public partial class newregistration : System.Web.UI.Page
    {
        MailMessage mail = new MailMessage();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential("mail@gmail.com", "password");
            SmtpServer.Port = 587;
            SmtpServer.Host = "smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            mail = new MailMessage();
            try
            {
                mail.To.Add(TextBox4.Text.ToString());
                mail.Subject = "hi";
                mail.From = new MailAddress("mail@gmail.com", "Admin@celluloid", System.Text.Encoding.UTF8);
                mail.Body = "u have been registered to celluloid";
              
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress("mail@gmail.com");
                SmtpServer.Send(mail);
                Response.Redirect("newhome.aspx");
              
            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

Edited 6 Years Ago by __avd: Added [code] tags. For easy readability, always wrap programming code within posts in [code] (code blocks).

start quote:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
//using System.Windows.Forms;
using System.Net.Mail;
using System.Net.Mime;
namespace celluloid
{
    public partial class newregistration : System.Web.UI.Page
    {
        MailMessage mail = new MailMessage();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button2_Click(object sender, EventArgs e)
        {

            SmtpClient SmtpServer = new SmtpClient();
            SmtpServer.Credentials = new System.Net.NetworkCredential("mail@gmail.com", "password");
            SmtpServer.Port = 587;
            SmtpServer.Host = "smtp.gmail.com";
            SmtpServer.EnableSsl = true;
            mail = new MailMessage();
            try
            {
                mail.To.Add(TextBox4.Text.ToString());
                mail.Subject = "hi";
                mail.From = new MailAddress("mail@gmail.com", "Admin@celluloid", System.Text.Encoding.UTF8);
                mail.Body = "u have been registered to celluloid";

                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
                mail.ReplyTo = new MailAddress("mail@gmail.com");
                SmtpServer.Send(mail);
                Response.Redirect("newhome.aspx");

            }
            catch (Exception ex)
            {
                throw;
            }
        }
    }
}

end quote.

first of all, thank you for your reply
second, I've tried your method, it gave me an exception that the server doesn't support secure connection
i've set SmtpServer.EnableSsl = false;
it gave me an exception that

"Mailbox name not allowed. The server response was: sorry, that domain isn't allowed to be relayed thru this MTA (#5.7.1)"

so, waht do you suggest for me to do,
i've emailed the mail server administrator and he told me that every thing is ok and he gave me the ports to use, but i still have this problem.
thank you again

Edited 3 Years Ago by mike_2000_17: Fixed formatting

This article has been dead for over six months. Start a new discussion instead.