0

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)"

2
  • You need to find an SMTP server that is willing to send for you. Commented Dec 24, 2012 at 18:46
  • stackoverflow.com/questions/14024715/… Commented Mar 29, 2014 at 14:06

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have revised my question to remove ambiguity. Now please review the post, I am using gmail address to send email but getting the error stated in the post.
I can't see anything wrong in your post. Might be a firewall/environment issue.
I have tried to send email by disabling firewall, but that makes no use. Any idea, what critical point I am missing :(
0

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 Comments

My application is running on my home machine, I have tried to send eamil by disabling the windows firewall, but that don't provide solution. I directly accessed my gmail account via gmail.com with the credentials that is fine. But when I tried to configure Outlook for my gmail account, that was not established and the following eror was displayed while testing connection: "Log onto incoming mail server (POP3): Outlook cannot connect to your incoming (POP3) e-mail server. If you continue to receive this message, contact your server administrator or Internet service provider (ISP)."
Send test e-mail message: Cannot send the message. Verify the e-mail address in your account properties. The server responded: 530 5.7.0 Must issue a STARTTLS command first. l5sm48012964wia.10
Did you configure Outlook to send using TLS? If so, then it would appear that you are unable to create a secure connection from your machine to google. Possibly your ISP is blocking connections, or there's something else with you machine that's preventing secure connections.
0

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);

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.