locked
Sending Registration link to an email RRS feed

  • Question

  • User-1994446809 posted

    Hello Forum,

    How can I send registration link to an email from web form?

    For example, if I am Registered on a particular platform and want to add other members, then there is a textbox and button to input the email and send an invite to the person.

    Saturday, July 25, 2020 1:37 PM

Answers

  • User-1994446809 posted

    Here is my code to send registration link to an email; I tried sending a link to an email and got an error.

    HTML:

     <div class="container-fluid" style="max-width: 450px;">
                <h2 class="form-signin-heading" style="font-size: medium; font-weight: bold; text-align: center; margin-top: 0px;">ADD NEW USER</h2>
                <hr />
                <div class="container2" style="max-width: 350px; margin: auto;">
                    <p style="font-size: smaller; font-weight: bold; color: #00003D;">
                        Send Registration Link to New User
                    </p>
    
                    <label for="txtUsername">Email</label>
                    <asp:TextBox ID="Emailtxtbx" runat="server" CssClass="form-control" placeholder="Invitee Email" />
                    <br />
                    &nbsp;
                    <asp:Button ID="Button1" runat="server" Text="SEND" Class="btn btn-primary" OnClick="Button1_Click" />
                    &nbsp;
                     <hr />
                    <div id="dvMessage" runat="server" visible="false" class="alert alert-danger">
                        <strong>Error!&nbsp;</strong><asp:Label ID="lblMessage" runat="server" />
                    </div>
                </div>
                <div id="Div1" runat="server" visible="false" class="alert alert-success">
                    <strong>Success!&nbsp;</strong><asp:Label ID="lblsuccess" runat="server" /><p><a href="LoginForm.aspx">Click to Login</a></p>
                </div>
            </div>

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Net;
    using System.Net.Mail;
    
    public partial class MemberInvite : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Session["UserID"] = User;
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (MailMessage mm = new MailMessage("sender@gmail.com", Emailtxtbx.Text))
            {
                mm.Subject = "Account Registration.";
                string body = "Hello " + Emailtxtbx.Text.Trim() + ",";
                body += "<br /><br />Please click the following link to Sign up";
                body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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);
            }
        }
    }

    Screenshot of the error I got:

    How can this be resolved, please?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 5:59 PM
  • User475983607 posted

    I tested the following code and it sends an email as expected.

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (MailMessage mm = new MailMessage(send, to))
        {
            mm.Subject = "Account Registration.";
            string body = "Hello " + to + ",";
            body += "<br /><br />Please click the following link to Sign up";
            body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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(send, password);
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 9:02 PM
  • User-1994446809 posted

    Okay. does it have to do with these lines which I have highlighted in yellow background?

     using (MailMessage mm = new MailMessage(send, to))
        {
            mm.Subject = "Account Registration.";
            string body = "Hello " + to + ",";
            body += "<br /><br />Please click the following link to Sign up";
            body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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(send, password);
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 9:04 PM

All replies

  • User475983607 posted

    The first step contacting your email service to find the service setting.  Then pick an client API like SmtpClient or SendGrid.  Read the reference documentation so you know how the libraries work.  Next, define detailed requirements around "Registration" and "Invite".  Create a flow chart that explains how "Registration" and "Invite" works.

    Lastly, write code to meet your requirements.  If your code does not function as expected then share the code on this form.  Explain the expected results and the actual results.

    Saturday, July 25, 2020 2:55 PM
  • User-1994446809 posted

    Here is my code to send registration link to an email; I tried sending a link to an email and got an error.

    HTML:

     <div class="container-fluid" style="max-width: 450px;">
                <h2 class="form-signin-heading" style="font-size: medium; font-weight: bold; text-align: center; margin-top: 0px;">ADD NEW USER</h2>
                <hr />
                <div class="container2" style="max-width: 350px; margin: auto;">
                    <p style="font-size: smaller; font-weight: bold; color: #00003D;">
                        Send Registration Link to New User
                    </p>
    
                    <label for="txtUsername">Email</label>
                    <asp:TextBox ID="Emailtxtbx" runat="server" CssClass="form-control" placeholder="Invitee Email" />
                    <br />
                    &nbsp;
                    <asp:Button ID="Button1" runat="server" Text="SEND" Class="btn btn-primary" OnClick="Button1_Click" />
                    &nbsp;
                     <hr />
                    <div id="dvMessage" runat="server" visible="false" class="alert alert-danger">
                        <strong>Error!&nbsp;</strong><asp:Label ID="lblMessage" runat="server" />
                    </div>
                </div>
                <div id="Div1" runat="server" visible="false" class="alert alert-success">
                    <strong>Success!&nbsp;</strong><asp:Label ID="lblsuccess" runat="server" /><p><a href="LoginForm.aspx">Click to Login</a></p>
                </div>
            </div>

    C#

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Configuration;
    using System.Data;
    using System.Data.SqlClient;
    using System.Net;
    using System.Net.Mail;
    
    public partial class MemberInvite : System.Web.UI.Page
    {
    
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                Session["UserID"] = User;
            }
        }
    
        protected void Button1_Click(object sender, EventArgs e)
        {
            using (MailMessage mm = new MailMessage("sender@gmail.com", Emailtxtbx.Text))
            {
                mm.Subject = "Account Registration.";
                string body = "Hello " + Emailtxtbx.Text.Trim() + ",";
                body += "<br /><br />Please click the following link to Sign up";
                body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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);
            }
        }
    }

    Screenshot of the error I got:

    How can this be resolved, please?

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 5:59 PM
  • User475983607 posted

    Here is my code to send registration link to an email; I tried sending a link to an email and got an error.

    It's far more helpful if you share the exception details rather than a screenshot of the link that we cannot click or see.

    How can this be resolved, please?

    Gmail requires that you turn on "Less secure app access" from your Google account.  You can find it in the Security section.

    Monday, July 27, 2020 6:15 PM
  • User-1994446809 posted

    Gmail requires that you turn on "Less secure app access" from your Google account.  You can find it in the Security section.

    I know about that and I already turned on "Less secure app access"

    It's far more helpful if you share the exception details rather than a screenshot of the link that we cannot click or see.

    EXCEPTION DETAILS:

    System.Net.Mail.SmtpException was unhandled by user code

      HResult=-2146233088

      Message=Failure sending mail.

      Source=System

      StackTrace:

           at System.Net.Mail.SmtpClient.Send(MailMessage message)

           at MemberInvite.Button1_Click(Object sender, EventArgs e) in c:\Users\GUEST 2\Documents\Visual Studio 2015\WebSites\ProjectJosCheck\MemberInvite.aspx.cs:line 43

           at System.Web.UI.WebControls.Button.OnClick(EventArgs e)

           at System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument)

           at System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)

           at System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument)

           at System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)

           at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

      InnerException:

           HResult=-2146232800

           Message=Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host.

           Source=System

           StackTrace:

                at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)

                at System.Net.FixedSizeReader.ReadPacket(Byte[] buffer, Int32 offset, Int32 count)

                at System.Net.Security._SslStream.StartFrameHeader(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)

                at System.Net.Security._SslStream.StartReading(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)

                at System.Net.Security._SslStream.ProcessRead(Byte[] buffer, Int32 offset, Int32 count, AsyncProtocolRequest asyncRequest)

                at System.Net.TlsStream.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.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode)

                at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception)

                at System.Net.Mail.SmtpClient.Send(MailMessage message)

           InnerException:

                ErrorCode=10054

                HResult=-2147467259

                Message=An existing connection was forcibly closed by the remote host

                NativeErrorCode=10054

                Source=System

                StackTrace:

                     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)

                InnerException:

    Monday, July 27, 2020 6:26 PM
  • User475983607 posted
    The host closed the connection. Try removing the default credentials line.
    Monday, July 27, 2020 7:06 PM
  • User475983607 posted

    I tested the following code and it sends an email as expected.

    protected void Button1_Click(object sender, EventArgs e)
    {
        using (MailMessage mm = new MailMessage(send, to))
        {
            mm.Subject = "Account Registration.";
            string body = "Hello " + to + ",";
            body += "<br /><br />Please click the following link to Sign up";
            body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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(send, password);
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }
    }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 9:02 PM
  • User-1994446809 posted

    Okay. does it have to do with these lines which I have highlighted in yellow background?

     using (MailMessage mm = new MailMessage(send, to))
        {
            mm.Subject = "Account Registration.";
            string body = "Hello " + to + ",";
            body += "<br /><br />Please click the following link to Sign up";
            body += "<br /><a href = '" + Request.Url.AbsoluteUri.Replace("Default.aspx", "SignUp.aspx?Id=" + Session["UserID"]) + "'>Click here for Sign up</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(send, password);
            smtp.Credentials = NetworkCred;
            smtp.Port = 587;
            smtp.Send(mm);
        }

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 27, 2020 9:04 PM
  • User-1994446809 posted

    Thanks mgebhard, its actually working.

    I didnt look thoroughly. I had to input my main gmail address and password.

    Monday, July 27, 2020 9:27 PM
  • User475983607 posted

    georgeakpan233

    Okay. does it have to do with these lines which I have highlighted in yellow background?

    String variables are used to hide my personal account information on the forum while maintaining the code structure .  If your account information is incorrect then that will certainly cause an exception.  

    public string send = "sender@gmail.com";
    public string to = "to@email.com";
    public string password = "password";

    Virus checkers and firewalls can also cause connection exceptions.  Try turning off your virus checker and firewall while testing.  Make sure you enabled 'less secure app access" as that will also cause an exception.

    Monday, July 27, 2020 9:29 PM
  • User-1994446809 posted
    I actually noticed it and effected the changes immediately after I sent the reply. I couldn't delete the reply which is why you still saw it.
    Thanks
    Tuesday, July 28, 2020 12:23 AM