1
votes

I have been foloowing this guide http://www.aspsnippets.com/Articles/Send-user-Confirmation-email-after-Registration-with-Activation-Link-in-ASPNet.aspx to create a system that send validation email on register and I seem to have stumbled into a small issue since the guide is suitable for Web Application and I need to modify it for Console Application,

this piece of code is suitable for web application but i'm trying to change it to suit a console application.

body += "<br /><a href = '" +Request.Url.AbsoluteUri.Replace("CS.aspx", "CS_Activation.aspx?ActivationCode=" + activationCode) + "'>Click here to activate your account.</a>";

Tried reading about WebClient or the Uri options but I dont seem to figure out how to fetch the absolute Uri and add the required string to generate the activation link from a Console application.

Will add more code if required.

Tried following this SO How to build a Url? but I cant seem to make it work properly.

Activation Code Generation and saving it on DB.

private static void SendActivationEmail(string email, string userName, MySqlConnection mysqlCon)
    {

        string activationCode = Guid.NewGuid().ToString();
        using (mysqlCon)
        {
            using (MySqlCommand cmd = new MySqlCommand("UPDATE accounts SET ActivationCode=@ActivationCode " +
                                                       "WHERE Username=@Username;"))
            {
                using (MySqlDataAdapter sda = new MySqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Username", userName);
                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                    cmd.ExecuteNonQuery();
                    mysqlCon.Close();
                }
            }
        }
2

2 Answers

0
votes

Are you building a console application that has the user register for a certain service? I don't really get what you are trying to do here.

If you have a console application, there is no such thing as a Request and thus certainly no Request.Url. You therefore need another way to find or construct the URL to embed in your outgoing email.

0
votes

In ASP.NET your application is hosted and always has Url, on the other hand in Console application you will have to set it manually.

To expand on Roy Dictus comment :

Perhaps it would be good idea to add a parameter to application Configuration, call it MyWebActivationBaseUrl and set value to say "http://myserver.com/Activate.aspx?ac=".

Then use it to build individual activation urls.