1

How can I send an activation email when new users register in my website using asp.net c#? I have tried the steps described this link but I have faced this error:

System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.

Is there any solution to this or any other way to send an email?

this is my code

using (MailMessage mm = new MailMessage("sender@gmail.com", txtEmail.Text))
{
    mm.Subject = "Account Activation";
    string body = "Hello " + txtUsername.Text.Trim() + ",";
    body += "<br /><br />Please click the following link to activate your account";
    body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";
    body += "<br /><br />Thanks";
    mm.Body = body;
    mm.IsBodyHtml = true;
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.EnableSsl = true;
    NetworkCredential NetworkCred = new NetworkCredential("sender@gmail.com", "<password>");
    smtp.UseDefaultCredentials = true;
    smtp.Credentials = NetworkCred;
    smtp.Port = 587;
    smtp.Send(mm);
}   

The proposed duplicate did not solve the problem. I am already using port 587.

Community
  • 1
  • 1
Bayan
  • 82
  • 7
  • 2
    1) You need to show some code. 2) You need to talk to an *ACTUAL EMAIL SERVER*, and the server needs to be able to accept connections from you. Based in this error `net_io_connectionclosed`, it sounds like that's *NOT* happening. – paulsm4 May 18 '16 at 18:28
  • 4
    Look at this link for detals on your "net_io_connectionclosed" error: [SmtpException: Unable to read data from the transport connection: net\_io\_connectionclosed](http://stackoverflow.com/questions/20228644/smtpexception-unable-to-read-data-from-the-transport-connection-net-io-connect) – paulsm4 May 18 '16 at 18:29
  • i have edited the post please have a look were is the wrong?? @paulsm4 – Bayan May 19 '16 at 16:04
  • Your code is *not* necessarily the problem E-mail servers can be very picky about who they'll talk to, and how they'll talk to you. Juan Pablo Melgarejo's response bypasses the problem by using Google's e-mail server, `smtp.gmail.com`, SUGGESTIONS: 1) Contact your e-mail administrator. Make sure your settings are correct; review his e-mail logs for errors. 2) Try Google e-mail: https://www.emailarchitect.net/easendmail/kb/csharp.aspx?cat=2 – paulsm4 May 19 '16 at 17:36
  • `smtp.UseDefaultCredentials = true;` should be `smtp.UseDefaultCredentials = false;` - because you just gave it non-default credentials – Rob May 20 '16 at 01:46

2 Answers2

1

See the code below:

public static class email_utility
{
    public static async Task<bool> send_email(this string body, string subject, string email, int try_count)
    {
        return await Task.Run(() =>
        {
            var cli = new SmtpClient();
            using (var message = new MailMessage(config.sender_email, email))
            {
                message.Subject = subject;
                message.SubjectEncoding = UTF8Encoding.UTF8;
                message.Body = body;
                message.BodyEncoding = UTF8Encoding.UTF8;
                message.DeliveryNotificationOptions = DeliveryNotificationOptions.Never;
                for (var count = 0; count < try_count; ++count)
                {
                    try
                    {
                        lock (config.sender_email)
                        {
                            cli.Send(message);
                            return true;
                        }
                    }
                    catch (SmtpFailedRecipientsException)
                    {
                        return false;
                    }
                    catch (SmtpException)
                    {
                        Thread.Sleep(config.send_timeout);
                    }
                }
                return false;
            }
        });
    }
}
Riad Baghbanli
  • 3,105
  • 1
  • 12
  • 20
1

Try this, it works perfectly and you only have to change the fields that are before the var smtp = newSMTPCient.

using System.Net;
using System.Net.Mail;

var fromAddress = new MailAddress("from@gmail.com", "From Name");
var toAddress = new MailAddress("to@example.com", "To Name");
const string fromPassword = "fromPassword";
const string subject = "Subject";
const string body = "Body";

var smtp = new SmtpClient
           {
               Host = "smtp.gmail.com",
               Port = 587,
               EnableSsl = true,
               DeliveryMethod = SmtpDeliveryMethod.Network,
               UseDefaultCredentials = false,
               Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
           };
using (var message = new MailMessage(fromAddress, toAddress)
                     {
                         Subject = subject,
                         Body = body
                     })
{
    smtp.Send(message);
}
  • FYI, the reason it's working perfectly for you (besides the fact that your code is correct!) is 1) you're using GMail as an e-mail relay, 2) you happen t have a valid gmail account, which you specified in your "From" address, 3) You're using port 587 and TLS encrypted SMTP. The first thing the OP needs to do is work with his network administrator and look at possible configuration/permissions issues with their "preferred" e-mail server. – paulsm4 May 19 '16 at 21:22