Home > Net >  cannot send a message with an href in a message body in c# with System.net.mail
cannot send a message with an href in a message body in c# with System.net.mail

Time:11-11

I try to send an email in c# with System.Net.Mail

string MsgBody = string.Format(@"Votre espace Cloud a été créé sur Patrimoine Click.<br>Vous pouvez vous y connecter en cliquant <a href=""https://patrimoine-click.netexplorer.pro"">ici</a><br><br>Votre identifiant est : <strong>{0}</strong><br>Votre mot de passe par défaut est : <strong>{1}</strong><br><br>Nous vous invitons à le changer à la première connexion.<br><br>L'équipe PatrimoineClick", user.Email, string.Concat(user.FirstName, user.LastName, "_PATRIMOINECLICK"));

                    MailRequest m = new()
                    {
                        Subject = "Patrimoine Click : votre espace Cloud a été créé.",
                        ToEmail = user.Email,
                        Body = MsgBody,
                    };

                    string MsgErreur = _traitement.EnvoieMail(m);

The problem is the href in the MsgBody.

<a href=""https://patrimoine-click.netexplorer.pro"">ici

When I put it, the message is not sent but there's no error. When I remove it, the message is sent correctly.

I think there's a problem with quotes.

Any idea ?

Thanks,

CodePudding user response:

You must declare the HTML tags in your mailbody string as html message, like this:

string mailBody = "<!DOCTYPE html><html><body><center><p> click here <a href='www.example.com'>in this link</a>.</p></center></body></html>";

In this way your href will work :-)

CodePudding user response:

I recommend sending Mails using this method:

SmtpClient client = new SmtpClient("host", port);
MailMessage message = new MailMessage("from", "to", "subject", "body");

message.IsBodyHtml = true;

client.Send(message);

Then your code will work.

  • Related