1

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();
                }
            }
        }
Community
  • 1
  • 1
Daniel Netzer
  • 2,151
  • 14
  • 22

2 Answers2

0

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.

Roy Dictus
  • 32,551
  • 8
  • 60
  • 76
  • yep, thats exactly what i'm asking. its a console application for a server that handles registration and login for a game i'm working on. been trying to use this http://stackoverflow.com/questions/20164298/how-to-build-a-url and try to construct my url but I cant seem to make it work sadly. – Daniel Netzer May 30 '16 at 10:13
  • Then isn't the link you want to embed in your outgoing email message always the same? – Roy Dictus May 30 '16 at 10:14
  • the link itself is a const but the ActivationCode is auto generated on registration and is a variable. the Url that I want to send is "http://example.com/CS_Activation.aspx?ActivationCode=" + activationCode – Daniel Netzer May 30 '16 at 10:17
  • If the activation code is generated, there is no need for passing a previous URL, is there? You can just generate the activation code, store it in your DB and embed it in the URL you generate. – Roy Dictus May 30 '16 at 10:20
  • already did that, and sending the activation code itself to the function. I will add some more code to clarify the question a bit. – Daniel Netzer May 30 '16 at 10:29
  • exactly what i'm about to try right now, I appreciate you walk through very much :D motivates me. – Daniel Netzer May 30 '16 at 10:41
0

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.

Edgars Pivovarenoks
  • 1,526
  • 1
  • 17
  • 31