title: Sending mail with embedded URL in Struts 2
body: <p>For a project for my company, I have to send emails containing embedded URLs, which the user will be prompted to follow.</p>
  
  <p>For example, a person registers on the website, and then the Struts2 application sends an email to that person in which there is a URL to confirm the subscription.</p>
  
  <p>So far, the form submission, and sending the email (from inside the action), work just fine. The problem on which I'm stuck is that I can't find a way to generate the URL I'd like to embed in the mail body.</p>
  
  <p>I must be doing it the wrong way, but I was thinking about something like what follows:</p>
  
  <pre><code>public String send() throws Exception {
      StringBuffer body = new StringBuffer();
  
      HashMap&lt;String, String&gt; params = new HashMap&lt;String, String&gt;();
      params.put("id", "xxxxxyyyyyaaaaa");
  
      body.append("Veuillez vous rendre ici :");
      body.append(UrlManager.getUrlForAction("action", params));
  
      SendMail sendMail = new SendMail();
      sendMail.send("me@me.fr", "Information", body.toString());
  
      return SUCCESS;
  }
  </code></pre>
  
  <p>where there would be a <code>UrlManager</code> (something that could be made available by the framework) with a method <code>getUrlForAction()</code> that gets an action and its parameters as input and that outputs a String containing the corresponding URL (like <code>http://mysite.mycompany.com/confirm?id=xxxxxyyyyyaaaaa</code>).</p>
  
  <p>Does anyone have any ideas or pointers on how to do that?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I tried using <code>UrlProvider</code>, but I get a null pointer exception on the <code>determineActionUrl</code> call. Maybe I'm using it the wrong way.</p>
  
  <pre><code>HashMap&lt;String,Object&gt; params = new HashMap&lt;String,Object&gt;();
  params.put("id", data.getMd5());
  
  UrlProvider up = new ComponentUrlProvider(
                              new Component(ServletActionContext.getValueStack(ServletActionContext.getRequest())),
                              ServletActionContext.getRequest().getParameterMap());
  downloadUrl = up.determineActionURL("confirm", "/", "GET",
                                      ServletActionContext.getRequest(),
                                      ServletActionContext.getResponse(),
                                      params,
                                      "http", true, true, true, true);
  </code></pre>