Wednesday, February 3, 2016

Remove HTML tags from string using JavaScript

I encountered a problem in Firefox that it doesn't support InnerText when I try to extract only text from an HTML.
    document.getElementById("mainDiv").InnerText
It works fine in chrome. So tried to remove the HTML tags from the InnerHTML string. Below is the solution for that.

    var html = document.getElementById("mainDiv");

    var text = html.replace(/<\S[^><]*>/g, '');

Setting up SMTP on IIS 7 to send mail from pickup directory

1. Start -> Administrative Tools -> Server manager

2. Go to Features, select "add features", Tick the "SMTP Server" (if it is not already installed), choose to install the required "Remote Server Administration Toos"

3. Go to Servies, Check to confirm that "Simple Mail Transfer Protocol (SMTP)" service is running, if not Start the service.



4. Start -> Administrative Tools -> Internet Information Services(iis) 6.0

5. Make sure that SMTP Virtual Server server is running, if not, right click, then choose "start"

6. Open SMTP Virtual Server properties by right clicking it.

7. On General tab, Set IP address of the Web server instead of "All Unassigned".

8. In Access tab, click on Relay button, this will open Relay Restrictions dialog.

9. In relay computers list, add the loopback IP address i.e 127.0.0.1 and IP address of the Web server, so that they can pass/relay emails through the SMTP server.

10. In IIS7,  go to website, double click "SMTP E-mail", Click on "Deliver e-mail to SMTP server", check the "Use localhost" checkmark

11. Your code should be like,

using (MailMessage message = new MailMessage())
{
    message.Subject = this.Subject;
    message.Body = this.Body;
    message.IsBodyHtml = this.BodyIsHtml;
    message.From = this.From;

    foreach (MailAddress address in this.To) message.To.Add(address);
    foreach (MailAddress address in this.CC) message.CC.Add(address);
    if (this.Bcc != null) message.Bcc.Add(this.Bcc);

    foreach (Attachment attachment in this.Attachments) message.Attachments.Add(attachment);

    SmtpClient smtp = new SmtpClient();
    smtp.Send(message);
}

12. And in Web.Config,

<system.net>
  <!-- Mail settings -->
  <mailSettings>
    <smtp deliveryMethod="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation="C:\inetpub\mailroot\Pickup"/>
    </smtp>
  </mailSettings>
</system.net>

The above steps could solve the following common error you may encounter,
  • "Mailbox unavailable. The server response was: 5.7.1 Unable to relay for"