Monday, 12 January 2015

Get unread emails from Outlook on Web page

After completing my work on sending email web service. I thought if i want to read emails from my outlook then is there any way to display that on a normal HTML page ?

After re-searching about outlook and .net pipe, i have found a way to do that.
For displaying outlook emails on webpage. you have to write macro code on outlook. For this you need to open outlook and press ALT+F11 and write below code :

Sub MyRule(Item As Outlook.MailItem)

Dim strURL As String
Dim oApp As Object
Set oApp = CreateObject("InternetExplorer.Application")
strURL = "http://localhost:51049/Default2.aspx"  //Enter your path
oApp.navigate (strURL)
oApp.Visible = True

End Sub


Save the above code and open your visual studio and add webform :
Add below Javascript in your webform :

<script>
        function PopulateEmails() {
            var objOutlook = new ActiveXObject("Outlook.Application");
            var session = objOutlook.Session;
 
            for (var folderCount = 1; folderCount <= session.Folders.Count; folderCount++) {
                var folder = session.Folders.Item(folderCount);
                if (folder.Name.indexOf("Main folder Name") >= 0) {
                    for (var subFolCount = 1; subFolCount <= folder.Folders.Count; subFolCount++) {
                        var sampleFolder = folder.Folders.Item(subFolCount);
                        if (sampleFolder.Name.indexOf("Folder Name to display unread message") >= 0) {
                            for (var itmCount = 1; itmCount <= sampleFolder.Items.Count; itmCount++) {
                                var itm = sampleFolder.Items.Item(itmCount);
                                if (!itm.UnRead)
                                    continue;
                                var sentBy = itm.SenderName;
                                var sentDate = itm.SentOn;
                                var receivedBy = itm.ReceivedByName;
                                var receivedDate = itm.ReceivedTime;
                                var subject = itm.ConversationTopic;
                                var contents = itm.Body;
 
                                var tbl = document.getElementById('tblContents');
                                if (tbl) {
                                    var tr = tbl.insertRow(tbl.rows.length);
 
                                    if (tbl.rows.length % 2 != 0)
                                        tr.className = "alt";
 
                                    var tdsentBy = tr.insertCell(0);
                                    var tdsentDate = tr.insertCell(1);
                                    var tdreceivedBy = tr.insertCell(2);
                                    var tdreceivedDate = tr.insertCell(3);
                                    var tdsubject = tr.insertCell(4);
                                    var tdcontents = tr.insertCell(5);
 
                                    tdsentBy.innerHTML = sentBy;
                                    tdsentDate.innerHTML = sentDate;
                                    tdreceivedBy.innerHTML = receivedBy;
                                    tdreceivedDate.innerHTML = receivedDate;
                                    tdsubject.innerHTML = subject;
                                    tdcontents.innerHTML = contents;
                                }
                                itm.UnRead = false;
                            }
                            break;
                        }
                    }
                    break;
                }
            }
            return;
        }
 
    </script>


Add below code in body of web form :


<div id="divResult">
           <input type="button" value="Read" onclick="PopulateEmails();" /><br />
           <table cellpadding="3" cellspacing="3" border="1" id="tblContents">
               <tr>
                   <th>Sent By</th>
                   <th>Sent on</th>
                   <th>Recived By</th>
                   <th>Recived on</th>
                   <th>Subject</th>
                   <th>Content</th>
               </tr>
           </table>
       </div>

Now run web form in IE.
If you get an error related to ActiveXObject creation error then follow below steps :

 1) Go To Settings->Internet Options->Security Tabs.
  2)  You will see different zones:i)Internet ii)Local Intranet iii)Trusted Sites iv)Restricted Sites.
  3)  Depending on your requirement select one zone. I am running my application in localhost so i selected Local intranet and then click Custom Level button.
  4) Opens security settings window. Please enable Initialize and script Activex controls not marked as safe for scripting option.It should work.

enter image description here

enter image description here









No comments:

Post a Comment