Email error with sending buttons, texts and logo in mail body

Donald Symmons 2,861 Reputation points
2023-04-17T14:14:21.26+00:00

I revisited a question I asked about adding image logo to an email. I got method on how to do this but I could not add my HTML buttons and texts to the email. In my code, I have an HTML button with some texts, and on the button click, there is a URL redirection to activation page where new user account will be activated. Please I need help on this. Thank you Here is my Code that sends mail to new user upon signing up

private void SendActivationEmail(int Uid)
    {
        string activationCode = Guid.NewGuid().ToString();
        using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\JosmadeLocal_DB.mdf;Integrated Security = True"))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO ActivationTable VALUES(@Uid, @ActivationCode)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Uid", Uid);
                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        using (MailMessage mm = new MailMessage("support@outlook.com", mailtxtbx.Text))
        {
            mm.Subject = "Account Activation";

            mm.AlternateViews.Add(Mail_Body());
            string body = "Hello " + txtname.Text.Trim() + ",";
            body += "<br /><br />Welcome!";
            body += "<br /><br />Please click the Button below to confirm your email";
            body += "<br /><br /><a style='display: block; width: 170px; height: 27px; background: #32CD32;padding: 7px;font-family: Nunito; text-align:center; border-radius: 5px;color: white;font-weight: 600;text-decoration: none;' href = '"
            + Request.Url.AbsoluteUri.Replace("signup", "Activation.aspx?ActivationCode=" + activationCode) + "'>Activate Account</a>";
            body += "<br /><br />Thanks";
            body += "<br /><br />Admin";
            mm.Body = body;
            mm.IsBodyHtml = true;

            SmtpClient SMTP = new SmtpClient("myhosting.secureserver.net", 25);
            SMTP.UseDefaultCredentials = false;
            SMTP.Credentials = new NetworkCredential()
            {
                UserName = "support@outlook.com",
                Password = "XXXXXXXXXXXXXXX"
            };
            SMTP.EnableSsl = false;
            SMTP.Send(mm);
        }
    }

But when I did this below, I get an error on the line. Mail Error

Here is how I tried to change the code; adding image logo to it

    private void SendActivationEmail(int Uid)
    {
        string activationCode = Guid.NewGuid().ToString();
        using (SqlConnection con = new SqlConnection("Data Source = (LocalDB)\\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\\JosmadeLocal_DB.mdf;Integrated Security = True"))
        {
            using (SqlCommand cmd = new SqlCommand("INSERT INTO ActivationTable VALUES(@Uid, @ActivationCode)"))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.CommandType = CommandType.Text;
                    cmd.Parameters.AddWithValue("@Uid", Uid);
                    cmd.Parameters.AddWithValue("@ActivationCode", activationCode);
                    cmd.Connection = con;
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
        }
        using (MailMessage mm = new MailMessage("support@outlook.com", mailtxtbx.Text))
        {
            mm.Subject = "Account Activation";
            mm.AlternateViews.Add(Mail_Body());
            mm.IsBodyHtml = true;

            SmtpClient SMTP = new SmtpClient("myhosting.secureserver.net", 25);
            SMTP.UseDefaultCredentials = false;
            SMTP.Credentials = new NetworkCredential()
            {
                UserName = "support@outlook.com",
                Password = "XXXXXXXXXXXXXXX"
            };
            SMTP.EnableSsl = false;
            SMTP.Send(mm);
        }
    }
    private AlternateView Mail_Body()
    {
        string path = Server.MapPath(@"~/images/myImage.png");
        string user = txtname.Text;
        LinkedResource Img = new LinkedResource(path, MediaTypeNames.Image.Jpeg);
        LinkedResource Lbl = new LinkedResource(path, MediaTypeNames.Text.Html);
        Img.ContentId = "MyImage";
        Lbl.ContentId = "myuser";
        string str = @"    
            <table>    
                <tr>    
                    <td>    
                      <img src=cid:MyImage  id='img' alt='' width='100px' height='60px'/>     
                    </td>    
                </tr> 
                 <tr>    
                    <td>    
                           'Hello' <label Text=cid:myuser id='lbl' font-size='10pt'/>
                    </td>    
                </tr>  
                <tr>    
                    <td>    
                           'Welcome !'
                    </td>    
                </tr>  
                <tr>    
                    <td>    
                         'Please click the Button below to confirm your email' 
                    </td>    
                </tr>  
                <tr>    
                    <td>    
                          <a style='display: block; width: 170px; height: 27px; background: #32CD32;padding: 7px;font-family: Nunito; text-align:center; border-radius: 5px;color: white;font-weight: 600;text-decoration: none;' href = '"
            + Request.Url.AbsoluteUri.Replace("signup", "Activation.aspx?ActivationCode=" + activationCode) + "'>Activate Account</a>
                    </td>    
                </tr>  
                <tr>    
                    <td>
                           'Thanks'
                    </td>    
                </tr>
                <tr>    
                    <td>
                        '- The Admin'
                    </td>    
                </tr>   
            </table>    
            ";
        AlternateView AV =
        AlternateView.CreateAlternateViewFromString(str, null, MediaTypeNames.Text.Html);
        AV.LinkedResources.Add(Img);
        return AV;
    }
.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,648 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,417 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,648 questions
{count} votes

Accepted answer
  1. QiYou-MSFT 4,311 Reputation points Microsoft Vendor
    2023-04-20T09:27:34.26+00:00

    Hi @Donald Symmons

    In fact, you can do this:

    <body>
        <form id="form1" runat="server">
            <div id="div1" runat="server">
                <img src="image/img28.jpg" width="100" height="100"/>
            </div>
            <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button"  />
        </form>
    </body>
     
    

    This way your image also has a URL link: Test3

    One step of your project is to get the username, and the URL of this image can be generated on this web page.

    Best Regards

    Qi You


2 additional answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    18 deleted comments

    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more