0
votes

I am trying to send email through an application in asp.net using c#. I searched a lot and mostly found the following code to send email using c# in asp.net:

        MailMessage objEmail = new MailMessage();

        objEmail.From = new MailAddress(txtFrom.Text);
        objEmail.To.Add(txtTo.Text);
        objEmail.CC.Add(txtCC.Text);
        objEmail.Bcc.Add(txtBCC.Text);
        objEmail.Subject = txtSubject.Text;

        try
        {

            SmtpClient mail = new SmtpClient();

            mail.EnableSsl = true;
            mail.DeliveryMethod = SmtpDeliveryMethod.Network;
            mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
            mail.Timeout = 20000;

            mail.UseDefaultCredentials = false;

            mail.Host = "smtp.gmail.com";
            mail.Port = 587;

            mail.Send(objEmail);

            Response.Write("Your Email has been sent sucessfully - Thank You");

        }
        catch (Exception exc)
        {
            Response.Write("Send failure due to : <br />" + exc.ToString());
        }

But constantly I am receiving the following error:

"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)"

3
You need to find an SMTP server that is willing to send for you.SLaks

3 Answers

0
votes

Your code looks reasonable, so you need to figure out what is preventing it from working.

Can you eliminate the possibility of a firewall problem? Most medium or large organizations tend to block ports 25, 465 and 587 - they simply don't want any unauthorized mail going out. If you suspect this might be the case, you could try if from outside the network (i.e. your home machine) although some ISPs will also block ports.

If the firewall isn't blocking your connections, verify that your credentials work - this can be done by sending via Thunderbird, Outlook, or equivalent. Try setting up a test using unencrypted mail - root thru your spam folder, paste a few into spamcop or equivalent, and look for an open relay. Alternatively, there are some commercial email providers that support unencrypted email.

3
votes

You're trying to use Gmail's SMTP server with an email address that doesn't belong to Gmail (according to the post's title). You need to update the host and port details to suit your email provider's SMTP details.

0
votes

You can check this out .. simple code and its works for gmail account ...

    try
    {

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress(TextFRM.Text);
        msg.To.Add(TextTO.Text);
        msg.Subject = TextSUB.Text;
        msg.Body = TextMSG.Text;


        SmtpClient sc = new SmtpClient("smtp.gmail.com");
        sc.Port = 25;
        sc.Credentials = new NetworkCredential(TextFRM.Text, TextPSD.Text);
        sc.EnableSsl = true;
        sc.Send(msg);
        Response.Write("Mail Send");
    }

    catch (Exception ex)
    {

        Response.Write(ex.Message);

    }