Click here to Skip to main content
14,386,319 members
Rate this:
Please Sign up or sign in to vote.
See more:
i have a page where users login , i also have forgot username link which sends users their username via email. I am using an outlook account to test which is open. i check the email address against one in my database table for users and send email to user if the address is present in db table. i am getting the following error '
Quote:
An exception of type 'System.Net.Mail.SmtpFailedRecipientException' occurred in App_Web_2woqyecp.dll but was not handled in user code
'
Quote:
Additional information: Mailbox unavailable. The server response was: 5.7.1 Unable to relay
and if i user another email address not from the outlook the email is not sent but i dont get an error

What I have tried:

public void forgot()
    {
        string username = "";

        SqlConnection con = new SqlConnection(strConnString);
        SqlCommand cmd = new SqlCommand(spgetuser, con);
        cmd.Parameters.AddWithValue("@email", txtEmailP.Text);

        con.Open();
        using (SqlDataReader dr = cmd.ExecuteReader())
        {

            if (dr.Read())
            {

                username= dr["username"].ToString();

            }

        }
        con.Close();


        if (!string.IsNullOrEmpty(username))
        {
            try
            {
                string emailFrom = "me@example.com";

                string body = "<div style='border: medium solid grey; width: 500px; height: 266px;font-family: arial,sans-serif; font-size: 17px;'>";
                body += "<h3 margin-top:0px;'>Username Recovery</h3>";
                body += "<br />";
                body += ("Your Username is: " + username);
                body += "<br />";
                body += "<br />";
                body += "Kinds Regards";
                body += "<br />";
                body += "</div>";

                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
                //message.To.Add(emailTo);
                //message.Subject = subject;
                message.From = new System.Net.Mail.MailAddress(emailFrom);
                message.IsBodyHtml = true;
                message.Body = body;
                message.Priority = System.Net.Mail.MailPriority.High;
                message.Subject = "username Recovery";

                message.From = new System.Net.Mail.MailAddress(emailFrom);


                SmtpClient SmtpMail = new SmtpClient();
                SmtpMail.EnableSsl = true;
                SmtpMail.Port = 25;
                SmtpMail.Host = "myhost";
               
                System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate(object s,
                          System.Security.Cryptography.X509Certificates.X509Certificate certificate,
                          System.Security.Cryptography.X509Certificates.X509Chain chain,
                          System.Net.Security.SslPolicyErrors sslPolicyErrors)
                {
                    return true;
                };

                // compare session username to username in database
                SqlCommand command = new SqlCommand(spcheck,con)
                command.Parameters.AddWithValue("@email", txtEmailP.Text);

                using (SqlConnection connection =
                           new SqlConnection(strConnString))
                {

                    connection.Open();

                    command.Connection = connection;

                    SqlDataReader reader = command.ExecuteReader();

                    // Call Read before accessing data.
                    while (reader.Read())
                    {

                        var to = new MailAddress(reader["Email"].ToString());
                        message.To.Add(to);

                    }

                    // Passing values to smtp object        
                    SmtpMail.Send(message);

                    ScriptManager.RegisterStartupScript(this, typeof(string), "Msg","alert('request has been sent');", true);


                    Response.Redirect("~/lgn.aspx");

                    // Call Close when done reading.
                    reader.Close();
                }



            }

                

            catch (Exception ex)
            {
                throw ex;
            }


        }
    }


    protected void Button1_Click(object sender, EventArgs e)
    {

        this.forgot();
    }
Posted
Updated 13-May-19 10:12am
v2
Comments
F-ES Sitecore 13-May-19 8:53am
   
Sounds like your smtp server isn't properly configured to send the email you want to send, or the settings you are using (port and ssl) aren't correct for your host. We don't know how your smtp server is configured so can't give any specific advice.
Member 14183767 13-May-19 8:59am
   
hi , if i use my email address(work email) i am able to receive the email with username , but if i try using another email adr like the one from outlook it doesnt work
Maciej Los 13-May-19 9:07am
   
Please, use "Reply" widget (on the right side of nick /login) to be sure that notification system will inform user about your reply.
Ger Hayden 13-May-19 10:31am
   
Bipin Joshi has a working example here. http://www.binaryintellect.net/articles/df920caf-ba69-4714-938f-fbb358532c0f.aspx which I followed for my own work.
Matias Lopez 13-May-19 13:45pm
   
A little question... App_Web_2woqyecp.dll: It's external/internal reference? Where/How is it referenced?
Member 14183767 14-May-19 4:01am
   
hi , i am not sure where this dll is coming from , its not in my bin folder
George Swan 14-May-19 3:20am
   
My tip, for what it's worth, is not to use System.Net.Mail, it doesn't support a lot of the modern protocols. The NuGet package MailKit is much less problematical.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 2

Does not compute.
//message.To.Add(emailTo);
//message.Subject = subject;
   
Comments
Member 14183767 14-May-19 3:59am
   
hi i have this code
message.Subject = "username Recovery";

// Call Read before accessing data.
while (reader.Read())
{

var to = new MailAddress(reader["Email"].ToString());
message.To.Add(to);

}

// Passing values to smtp object
SmtpMail.Send(message);

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100