Tag Archives: C#

Colour converter based on Status string.

Declare a class and inherit from the IValueConverter
using System; 
using System.Net; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Documents; 
using System.Windows.Ink; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Animation; 
using System.Windows.Shapes; 
using System.Windows.Data;

public class StatusColorConverter:IValueConverter 
    {

        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            string status = (string)value; 
            if (value == null) 
            { 
                return new SolidColorBrush(Colors.LightGray); 
            } 
            if (string.IsNullOrEmpty(status)) 
            { 
                return new SolidColorBrush(Colors.LightGray); 
            } 
            else if (status.Trim() == "Open") 
            { 
                return new SolidColorBrush(Colors.Orange); 
            } 
            else if (status.Trim() == "Closed") 
            { 
                return new SolidColorBrush(Colors.Green); 
            } 
            else 
            { 
                return new SolidColorBrush(Colors.LightGray); 
            } 
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture) 
        { 
            throw new NotImplementedException(); 
        } 
    }
Using it in XAML, Below are just the main elements for the understanding. The original XAML page will have more declaration
<navigation:Page xmlns:local="clr-namespace:ProjectName.Converters"
           Title="Page Title Here">
    <navigation:Page.Resources>
        <local:StatusColorConverter x:Key="statusColor" />
    </navigation:Page.Resources>
</navigation:Page>
<Grid>
<Border BorderBrush="White" BorderThickness="2" Background="{Binding Path=Status,Converter={StaticResource colorConvert}}" 
CornerRadius="10,10,10,10" Width="200" Height="150">
<TextBlock Text="{Binding Path=StatusText}" />
</Border>
</Grid>

Read Outlook email by filter criteria

Outlook object model provides a more extensible object collections to send and read items. This blog speaks about how to read a email from outlook default profile and process the attachment. The Outlook Item object has a method called Find which allows to filter the mail based on subject, To, cc etc. The below code will get all the mail item with the specified subject. If the item has an attachment it will save it to the path specified.

To begin with Add a Outlook interop assembly to the reference in the project

image

Once added please copy paste the below code to run the sample

Code Snippet
  1. try
  2.             {
  3.                 Microsoft.Office.Interop.Outlook.Application MyApp = new Microsoft.Office.Interop.Outlook.Application();
  4.                 Microsoft.Office.Interop.Outlook.NameSpace MailNS = MyApp.GetNamespace("mapi");
  5.                 MailNS.Logon(MyApp.DefaultProfileName, Missing.Value, false, true);
  6.  
  7.                 Microsoft.Office.Interop.Outlook.MAPIFolder MyInbox = null;
  8.                 MyInbox = MailNS.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);
  9.                 Microsoft.Office.Interop.Outlook._MailItem InboxMailItem = null;
  10.  
  11.                 Microsoft.Office.Interop.Outlook.Items oItems = MyInbox.Items;
  12.                 ////string Query = "[Subject] = '" + Properties.Settings.Default.EmailSubject + "'";
  13.  
  14.                 string Query = "[Subject] = 'my sample search text'";
  15.                 InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.Find(Query);
  16.                 while (InboxMailItem != null)
  17.                 {
  18.                     ListViewItem myItem = lvwMails.Items.Add(InboxMailItem.SenderName);
  19.                     myItem.SubItems.Add(InboxMailItem.Subject);
  20.                     string AttachmentNames = string.Empty;
  21.                     foreach (Microsoft.Office.Interop.Outlook.Attachment item in InboxMailItem.Attachments)
  22.                     {
  23.                         AttachmentNames += item.DisplayName;
  24.                         ////item.SaveAsFile(this.GetAttachmentPath(item.FileName, Properties.Settings.Default.ReceiveAttachmentPath));
  25.                         item.SaveAsFile(this.GetAttachmentPath(item.FileName, "c:\\test\\attachments\\"));
  26.                     }
  27.                     myItem.SubItems.Add(AttachmentNames);
  28.                     InboxMailItem.Delete();
  29.                     InboxMailItem = (Microsoft.Office.Interop.Outlook._MailItem)oItems.FindNext();
  30.                 }
  31.  
  32.                ////MailMsg.Send();
  33.                 MailNS.Logoff();
  34.                 MailNS = null;
  35.                 MyInbox = null;
  36.                 InboxMailItem = null;
  37.                 ((Microsoft.Office.Interop.Outlook._Application)MyApp).Quit();
  38.                 ////MyApp.Quit();
  39.                 MyApp = null;
  40.             }
  41.             catch (Exception ex)
  42.             {
  43.                 MessageBox.Show(ex.Message);
  44.             }

the getattachmentpath method implementation is below. This method will check for any existing attachment with the same name and adds running number if exists

Code Snippet
  1. private string GetAttachmentPath(string AttachfileName, string AttachPath)
  2.         {
  3.             string filename = Path.GetFileNameWithoutExtension(AttachfileName);
  4.             string ext = Path.GetExtension(AttachfileName);
  5.             int app = 0;
  6.             if (File.Exists(AttachPath + filename + ext))
  7.             {
  8.                 while (File.Exists(AttachPath + filename + app + ext))
  9.                 {
  10.                     app++;
  11.                 }
  12.                 filename = filename + app;
  13.             }
  14.             return AttachPath + filename + ext;
  15.         }