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);
}
}
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;
}
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '<'
Error CS1012 Too many characters in character literal
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '<'
Error CS1012 Too many characters in character literal
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '/'
Error CS1525 Invalid expression term '<'
Error CS1525 Invalid expression term '/'
Error CS1010 Newline in constant Error CS1002 ; expected