0

I saw some examples here, but I'm actually doing the same thing, however, it does not fully work. So, this is my question:

I have the code in my controller to generate URL:

string link = @Url.Action("Index", "Details", new System.Web.Routing.RouteValueDictionary(new { id = newId }), "http", Request.Url.Host);
var strUrl = "<a href='"
    + @Url.Action("Index", "Details", new System.Web.Routing.RouteValueDictionary(new { id = Id }), "http", Request.Url.Host)
    + "'>Click here</a>";

Then I send an email:

var response = EmailHelper.SendApproveTicketEmail(myModel.Id, strUrl);

This is my Send Email code:

public static SaveResult SendEmail(int ID, string Url)
{
    var email = new Email
    {
        Recipients = Configuration.EmailTo,
        Body = Configuration.EmailBody + ID.ToString() + "." + System.Environment.NewLine + "Click the following link to open the details page:" + ticketUrl,
        Sender = Configuration.EmailFrom,
        Subject = Configuration.EmailSubject,
        Cc = Configuration.EmailCC
    };

    return FormatResult(_client.SendEmail(Configuration.ApplicationName, 0, email));
}

The following email is received:

The following item needs to be approved:0011 Click the following link to open the item details page:<a href='http://localhost:51335/Details/Index/001'>Go to your item</a>

The URL is built successfully, however, it is not clickable.

What am I missing?

5
  • There's not nearly enough information to provide an answer.First, what is the content of the email? How is it not clickable? How are you sending it? What is the content of ticketUrl? Are you sending email from a Razor file??? Commented Feb 1, 2018 at 22:48
  • I just updated the question with SendEmail method Commented Feb 1, 2018 at 22:52
  • 1
    Read my comment again, this is still significantly below enough information. Commented Feb 1, 2018 at 22:53
  • The SendEmail method isn't relevant to the issue. Commented Feb 1, 2018 at 22:53
  • Total guess because you haven't posted your email code: check that MailMessage.IsBodyHtml is set true Commented Feb 2, 2018 at 0:41

1 Answer 1

2

When an HTML anchor is rendered, it requires at minimum an href and some sort of object to click on. That object can either be text or an image. The problem is that you have specified no text, so there is nothing for the user to click on.

var ticketUrl = "<a href='"
    + @Url.Action("Index", "TicketDetail", new System.Web.Routing.RouteValueDictionary(new { id = newTicketId }), "http", Request.Url.Host)
    + "'></a>";

You need to add something between <a></a> to form a hyperlink.

var ticketUrl = "<a href='"
    + @Url.Action("Index", "TicketDetail", new System.Web.Routing.RouteValueDictionary(new { id = newTicketId }), "http", Request.Url.Host)
    + "'>Click Me</a>";

Another thing to check is whether or not your MailMessage.IsBodyHtml is set to true. If it is false, your email will be processed as plain text instead of HTML, which means it won't process hyperlinks.

Sign up to request clarification or add additional context in comments.

5 Comments

I followed your advice here and updated the code in the question. However, the link is not clickable
There could also be other issues (such as a <div/> overlapping the link) that cause it to be non-clickable, but nobody can tell you what they are if you don't post a sample of the HTML and CSS of the email.
I found the reason. When composing email message, I forgot to specify that it is an Html message
Ok, I have updated my answer to include that reason. Next time it would be better if you pay attention to people's comments and add the code that they request - nobody can really answer the question you asked, we can really only guess as to what is wrong. You didn't even really explain what you meant by "not clickable" - if you would have mentioned that you could see the whole <a href=""></a> tag in the email, that would have been a huge clue.
Thank you for trying to help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.