Thursday 25 June 2020

Test Email without sending Email to User - C# / C-Sharp - DotNet

Sometimes we may want to test email functionality without sending email to actual user. Email will be saved locally so that we can verify it sending out emails in real scenario.

In below code, SmtpClient settings are configured in App.Config. We don't need to retrieve and assign these values to SmtpClient object. These configurations should be picked up automatically by SmtpClient from App.Config. It helps to keep our code clean.

Full source code : Click here to download solution

There are two parts of Code:
  1. Code to Send Email
  2. App.Config Configuration to be used to test this code.

Code to Send Email

We need to define "PickupDirectoryLocation", which will be the path where email will be saved. This should be absolute path for the disk else it will throw exception. It can also be a shared path if you want to save emails on Shared network drive.

using System;
using System.Net.Mail;

namespace SendEmailUsingC_Sharp
{
    class Program
    {
        static void Main(string[] args)
        {
            var smtpClient = new SmtpClient();
            var mailMessage = new MailMessage();
            try
            {
                mailMessage.To.Add("receiversEmail@gmail.com");
                mailMessage.From = new MailAddress("abc@xyz.com", "Test User");
                mailMessage.Subject = "Test Email using C-Sharp";
                mailMessage.Body = @"<h1>Test Email</h1>
                                    <br/>
                                    This is Test Email.";

                mailMessage.IsBodyHtml = true;
                try
                {
                    smtpClient.Send(mailMessage);
                    Console.WriteLine("Email sent successfully.");
                }
                catch (SmtpException smtpSpecificException)
                {
                    Console.WriteLine(smtpSpecificException.Message);
                }
            }
            catch (Exception ex) //General Exception
            {
                Console.WriteLine(ex.Message);
            }
            Console.ReadLine(); // This is just to pause Console Screen.
        }
    }
}



App.Config Configuration 

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <system.net>
    <!--Pickup Directory Configuration. Pickup Directory Location must be an absolute/full path to directory or It can also be network shared path.-->
    <mailSettings>
      <smtp deliveryMethod="SpecifiedPickupDirectory"  from="FromAddress@gmail.com">
        <specifiedPickupDirectory pickupDirectoryLocation="F:\Ovais\Blog\Emails"/>
      </smtp>
    </mailSettings>
   
    <!--Original Email Configuration. Keep only one configuration Uncommented.-->
    <!--<mailSettings>
      <smtp deliveryMethod="Network"  from="FromAddress@gmail.com">
        <network defaultCredentials="true" host="smtp.gmail.com" enableSsl="true" port="587" userName="username@gmail.com" password="userPassword"/>
      </smtp>
    </mailSettings>-->
  </system.net>
</configuration>

Tuesday 28 October 2014

SharePoint - Unable to create New WebApplication

Problem

I recently faced a problem with SharePoint, that I created new Web Application and it was showing a popup of "It shouldn't take long", then after some time it showed a Connection failure page. I browsed to the virtual directory folder for the new web application and found that the folder was totally empty.

Solution
Then what I did to solve this problem:


1. Open IIS
2. Go to Applicatin Pools
 3. Select Central Admin application pool and right click and select "Advance Settings".
4. There was a property named "Shutdown Time Limit", it was set to "90" by default.
    I changed it to 400 and clicked OK.
 
It restarted the applicaition pool automatically. Then again I created new web application from central admin and it worked for me.

Saturday 20 September 2014

Item Created time zone displays wrong programatically - SharePoint List

Problem:
Recently I faced an issue, that I was having a Log field with Version enabled list. Time was being displayed correctly in the SharePoint list but while retrieving CREATED field time programactially for all versions of item, it was displaying wrong time.

I was using below statement to retrieve time for CREATED date:
version.Created.ToString("dd/MM/yyyy hh:mm tt")
My timezone was though set to +3 Region, but it was showing GMT time with +0 Regional time settings.

Solution:
Then I change the above line to:

System.TimeZone.CurrentTimeZone.ToLocalTime(version.Created).ToString("dd/MM/yyyy hh:mm tt")
This done the trick for me.

Hope this helps someone!

Sunday 10 November 2013

Button not working after first time - Telerik SharePoint

Recently I faced an issue while developing "Export to Excel" functionality for Telerik grid in Webpart for SharePoint 2010. Export to excel was not working once the user has clicked the button. After some googling, I found that by using following JavaScript snippet in the Webpart or Page, this issue can be resolved and it worked for me :

<script type="text/javascript" language="javascript">

    //sharepoint postback to work after clicking on telerik export
    if (typeof (_spBodyOnLoadFunctionNames) != 'undefined' && _spBodyOnLoadFunctionNames != null) {
        _spBodyOnLoadFunctionNames.push("supressSubmitWraper");
    }

    function supressSubmitWraper() {
        _spSuppressFormOnSubmitWrapper = true;
    }
  
</script>

Hope it can help someone!!

Wednesday 24 April 2013

How to setup / configure Microsoft Office Web Apps 2013 with SharePoint 2013

How to setup / configure Microsoft Office Web Apps 2013 with SharePoint 2013

Recently, I was asked by by Team lead to setup Single Server Test Environment for SharePoint 2013 configuration with Microsoft Office Web Apps 2013.

First I thought , I just have download and run setup of Microsoft Office Web Apps 2013 and I am done with it. But when I started the proceedings, I faced some issues which I will be listing down to help someone and save their precious time rather than googling and trying to find the solution of the problems which someone have to face while configuring Microsoft Office Web Apps 2013. There are other blogs which avaialable but those blogs have not yet discussed any of the issues I was facing at the time of configuration.

Microsoft Web Apps : It is used to access and read / edit office documents like Ms-Word, Excel, OneNote and PowerPoint online , or you can say it opens the office documents online in browser rather than opening in Client Application of Office.

By default in SharePoint , excel usually opens without the need of WebApps but documents like Word and PowerPoint are first downloaded and then are opened in Client App.

Now lets start proceedings :
  1. Before you start, let me tell you that  Microsoft Office Web Apps 2013 requires a seperate server: a.) It should be a seperate server than the SharePoint 2013 with Windows Server either Windows Server 2008 R2 or Windows Server  2012.    b.) Microsoft Office must not be installed on the server on which you are going to install Microsoft Office Web Apps 2013.  c.) Server must be on domain. d.) Add the domain user in the Administrator group server on which you are going to install Office Web App 2013. e.) Login / RDC the server on which you are going to install Office WebApp Server 2013 with the (domain + Administrator ) rights user and then start the further proceedings.
  2. First we have to download setup for the Microsoft Office Web Apps 2013, visit the link below http://www.microsoft.com/en-us/download/details.aspx?id=35489 and download "wacserver.img". Or you can click download link : http://www.microsoft.com/en-us/download/confirmation.aspx?id=35489
  3.  As you can see file extension is ".img", so its a package which you need to open / extract. You can use "Extract Now", which can be downloaded from the following link:http://www.softpedia.com/get/Compression-tools/ExtractNow.shtml
  4. After "wacserver.img" file has bee downloaded. If you are using Extract Now for extraction , drag and drop the "wacserver.img" file on the white area in the Extract Now and then extract the file where you want to.
  5. Office Web App Server 2013 does not have any pre-requisites installer file, so we have to install all the pre-requisites one by one. Basic pre-requisites are :
  •  Windows PowerShell 3.0
  •  Dot Net Framework 4.5 ( Which is already available in / installed by default in Windows                                                   Server 2012)
  • IIS 7.5 , Asp.net 4.0 (in Windows Server 2012 its IIS 8 )
  • All the above can be installed from Server Manager :

7. Now, we are ready to run setup for Microsoft Office WebApp 2013, run the setup file in the package you have downloaded.
(If you try to install the Office Web Apps on the same server on which you sharepoint is installed it will not allow you to continue, pluse if you have installed microsoft office , it will not allow you to continue proceedings until you uninstall the Microsoft Office.)

8.Once setup is completed successfully, you need to setup a farm site on the same server on which you have installed the Office Web App Server. So for that you need to execute some commands on Windows Power Shell. (open Windwos Power Shell By Run as Administrator)

9.Execute following command on Windows Power Shell: (this step can be performed before you run setup for the office web app server )
Add-WindowsFeature Web-Server,Web-Mgmt-Tools,Web-Mgmt-Console,Web-WebServer,Web-Common-Http,Web-Default-Doc,Web-Static-Content,Web-Performance,Web-Stat-Compression,Web-Dyn-Compression,Web-Security,Web-Filtering,Web-Windows-Auth,Web-App-Dev,Web-Net-Ext45,Web-Asp-Net45,Web-ISAPI-Ext,Web-ISAPI-Filter,Web-Includes,InkandHandwritingServices

10. And then for setting up a single server test enviroment farm for office web apps, execute following command:
New-OfficeWebAppsFarm –InternalURL "http://servername" –AllowHttp -EditingEnabled
(if you are not logged inn as (domain + admin ) rights it will show message of login failure and authentication failure)
11. To check whether the above executed command has setup the Farm for office web apps or not , open the following url in your browser:
http://servername/hosting/discovery
and it will return you xml like below screenshot:

12. Now its time to connect / configure your sharepoint server to utilize the office web app server. To do that you need to run a command on your sharepoint server. Now login / RDC your sharepoint server and open Management Shell from 
Programs > SharePoint Products > SharePoint 2013 Management Shell ( Run as Administrator)

13.Execute following command:
New-SPWOPIBinding -ServerName <WacServerName> -AllowHTTP
Where <WacServerName> will be replaced from the name given to your Office Web App Server. As in my case it was "SP-WebApps", so my command was:
New-SPWOPIBinding -ServerName SP-WebApps -AllowHTTP
and press enter. It will create a new binding for the SharePoint with office web app server and will show all bindings like below:
(If your server will not be on the same domain, this command might show an error of unable to contact the server)
14. Now execute following command on the sharepoint server management shell:
Get-SPWOPIZone
It will show the sharepoint webapps zone details, which will be:
internal-https (by default)
To change it to use normal http , execute following command
Set-SPWOPIZone –zone “internal-http”
Now again run Get-SPWOPIZone command for confirmation that it has been set correctly.
For other description explaination and advanced configuration you can visit:http://technet.microsoft.com/en-us/library/ff431687.aspx
That's all, we are now done with it.!! Open your SharePoint Site and open any document that is uploaded in library, it must now show / open in browser like below:
For the video demo:

Hope that will help someone!!

Thursday 18 April 2013

Unicode characters being displayed as Boxes and Questions marks

Unicode characters being displayed as Boxes and Questions marks

Recently I faced a problem while development of a multilingual website, which was in English and German language, it was showing some german characters as Box ad Questions marks just like :

(Internet Explorer)

(Google Chrome)

Reason

Reason was that, I was using notepad++ and notepad as the developmnet enviromnet as the development platform was PHP, so when I was saving the files it was being saved as ANSI coding and other than UTF-8.

Solution

What I did, I re-opened the files having unicode content/characters, in Visual Studio or in Notepad.

Visual Studio
In visual studio, right click on the source page on any white area and take properties, it will show you the coding it is being following, change that to UTF-8 and if it is already selected then just save the file as it is again, and then again check , your issue must have resolved, and it must now show the characters properly in browsers.

Notepad
In notepad, click File > Save As, and then at the time of saving the file, there must be a coding technique option dropdown, by default there could be ANSI selected. Change that to UTF-8,  and don't forget to change file type to "All files" , other wise it might concate ".txt" (dot text) extension with the file extension, as in my case, file extension was "file_name.tpl" , but it save it as "file_name.tpl.txt".

That was all, hope it could help someone!!!



Thursday 4 April 2013

How to Register your App with Facebook

How to Register your App / Website with Facebook

Following below steps to register your app / domain to integrate your app / website with facebook :
1. Login with facebook account or Register for new account on facebook.
2. Then go to Settings > Create an App.
3. Then click "Apps" tab.


 4. Then click "Create New App". Type name for your application and then click continue.
(It will check for the name validation, if it will be OK, it will show message "Valid"on the right side of the app name input box.)
 5. Then in the next screen it will ask for the human verifcation code / captcha. type in the code given in the picture and click continue.
6. It wil then take you to the app page, where you will note the "App ID" and "App Secret" key, which will be used by the facebook SDK to authenticate your application with facebook. and also you will provide the app domain / url , of your website, which will then include your website in the white list. Else it will not acknowledge / authenticate the requesting domain, if you will not provide the address. and will give / prompt message of invalid url.
7. After all these steps click "Save".

Hope that helps!!!!




Test Email without sending Email to User - C# / C-Sharp - DotNet

Sometimes we may want to test email functionality without sending email to actual user. Email will be saved locally so that we can verify ...