Click here to Skip to main content
15,883,870 members
Articles / Programming Languages / C# 4.0

PopClient - A POP3 companion to SmtpClient

Rate me:
Please Sign up or sign in to vote.
4.94/5 (55 votes)
19 Nov 2010CPOL16 min read 222K   4.9K   111  
PopClient is an asynchronous POP3 library with support for SSL and attachments
<Window x:Class="PopClientDemo.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:properties="clr-namespace:PopClientDemo.Properties"
        xmlns:nsmvvm="clr-namespace:NS.MVVM"
        Title="{Binding Title}" Height="730" Width="980">

    <Grid>
        <Grid x:Name="MailGrid" Background="{StaticResource MainBackgroundBrush}" Opacity="{Binding MailGridOpacity}">
            <Grid.RowDefinitions>
                <RowDefinition Height="Auto" />
                <RowDefinition />
                <RowDefinition Height="Auto" />
                <RowDefinition />
                <RowDefinition Height="Auto" />
            </Grid.RowDefinitions>
            
            <ToolBar Grid.Row="0" Style="{StaticResource ToolBarStyle}" ToolBarTray.IsLocked="True" IsEnabled="{Binding IsToolbarAvailable}">
                <Button Margin="3,0,3,0" Command="{Binding RefreshCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="32">
                            <Image.Style>
                                <Style TargetType="Image">
                                    <Style.Triggers>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/RefreshGray.png" />
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="True">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/Refresh.png" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <TextBlock VerticalAlignment="Center">Refresh</TextBlock>
                    </StackPanel>
                </Button>
                <Button Margin="3,0,3,0" Command="{Binding CancelCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="32">
                            <Image.Style>
                                <Style TargetType="Image">
                                    <Style.Triggers>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/CancelGray.png" />
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="True">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/Cancel.png" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <TextBlock VerticalAlignment="Center">Cancel</TextBlock>
                    </StackPanel>
                </Button>
                <Button Margin="3,0,3,0" Command="{Binding ShowChatLogCommand}">
                    <StackPanel Orientation="Horizontal">
                        <Image Width="32">
                            <Image.Style>
                                <Style TargetType="Image">
                                    <Style.Triggers>
                                        <Trigger Property="IsEnabled" Value="False">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/ShowLogGray.png" />
                                        </Trigger>
                                        <Trigger Property="IsEnabled" Value="True">
                                            <Setter Property="Image.Source" Value="/PopClientDemo;component/Icons/ShowLog.png" />
                                        </Trigger>
                                    </Style.Triggers>
                                </Style>
                            </Image.Style>
                        </Image>
                        <TextBlock VerticalAlignment="Center">Chat Log</TextBlock>
                    </StackPanel>
                </Button>
            </ToolBar>
            
            <Grid Grid.Row="1">
                <ListView ItemsSource="{Binding Mails}" x:Name="mailListView" 
                          Style="{StaticResource MailListViewStyle}"
                          Background="{StaticResource MainBackgroundBrush}">
                    <ListView.View>
                        <GridView AllowsColumnReorder="true" 
                                  ColumnHeaderContainerStyle="{StaticResource GridViewColumnHeaderStyle}">
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Message.From}" Header="From" Width="250"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Message.Subject}" Header="Subject" Width="300"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=ReceivedTime}" Header="Date" Width="200"/>
                            <GridViewColumn DisplayMemberBinding="{Binding Path=Message.Attachments.Count}" Header="Attachments" Width="100"/>
                        </GridView>
                    </ListView.View>
                </ListView>
            </Grid>
            
            <GridSplitter Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Center"
                          Background="Gray" ShowsPreview="True" Height="4" />

            <Grid Grid.Row="3" DataContext="{Binding ElementName=mailListView, Path=SelectedItem.Message}">
                <Grid.RowDefinitions>
                    <RowDefinition />
                    <RowDefinition Height="Auto" />
                </Grid.RowDefinitions>
                <TextBox Grid.Row="0" AcceptsReturn="True" VerticalScrollBarVisibility="Auto" Text="{Binding Body}" Style="{StaticResource MailBodyStyle}" />
                <ListBox Grid.Row="1" ItemsSource="{Binding Attachments}" Background="{StaticResource MainBackgroundBrush}">
                    <ListBox.ItemsPanel>
                        <ItemsPanelTemplate>
                            <StackPanel Orientation="Horizontal" />
                        </ItemsPanelTemplate>
                    </ListBox.ItemsPanel>
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <Grid Background="{StaticResource AttachmentItemBackgroundBrush}" Height="40" Width="70"
                                  Tag="{Binding ElementName=MailGrid, Path=DataContext}">
                                <Grid.ContextMenu>
                                    <ContextMenu>
                                        <MenuItem Header="Save" 
                                                  DataContext="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}, Path=PlacementTarget}"
                                                  Command="{Binding Tag.SaveFileCommand}"
                                                  CommandParameter="{Binding DataContext}" />
                                    </ContextMenu>
                                </Grid.ContextMenu>
                                <StackPanel Margin="5" Tag="{Binding Name}">
                                    <StackPanel.ToolTip>
                                        <ToolTip DataContext="{Binding Path=PlacementTarget, RelativeSource={x:Static RelativeSource.Self}}"
                                                 Content="{Binding Tag}" />
                                    </StackPanel.ToolTip>
                                    <TextBlock Text="{Binding Name}" TextWrapping="Wrap" />
                                </StackPanel>
                            </Grid>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
            </Grid>

            <StatusBar Grid.Row="4" Style="{StaticResource StatusBarStyle}">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="0,0,5,0">Mail stats:</TextBlock>
                    <TextBlock Margin="0,0,5,0" MinWidth="200" Text="{Binding MailStatsText}" />
                    <TextBlock Margin="0,0,5,0">Fetch status:</TextBlock>
                    <TextBlock Margin="0,0,5,0" Text="{Binding FetchStatusText}" />
                </StackPanel>
            </StatusBar>

        </Grid>

        <Grid x:Name="LoginGrid" Width="500" Height="400" Visibility="{Binding LoginGridVisibility}" Style="{StaticResource LoginGridStyle}"
               FocusManager.FocusedElement="{Binding ElementName=hostTextBox}">
            <Border BorderThickness="0,0,5,5" BorderBrush="Black" Background="{StaticResource MainBackgroundBrush}" CornerRadius="15">
                <Border.Effect>
                    <DropShadowEffect />
                </Border.Effect>
                <StackPanel>
                    <StackPanel Background="{StaticResource HeadingBackgroundBrushKey}">
                        <TextBlock HorizontalAlignment="Center" Style="{StaticResource TextBlockHeadingStyle}" Margin="5">POP Settings</TextBlock>
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Host:</TextBlock>
                        <TextBox x:Name="hostTextBox" Width="332" Height="34" Text="{Binding Source={x:Static properties:Settings.Default}, Path=Host}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Port:</TextBlock>
                        <TextBox Width="70" Height="34" Text="{Binding Source={x:Static properties:Settings.Default}, Path=Port}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Username:</TextBlock>
                        <TextBox Width="332" Height="34" Text="{Binding Source={x:Static properties:Settings.Default}, Path=Username}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Password:</TextBlock>
                        <PasswordBox Width="332" Height="34" 
                                     nsmvvm:PasswordBoxBindingBehavior.EnableBinding="True"
                                     nsmvvm:PasswordBoxBindingBehavior.BoundPassword="{Binding PopPassword, Mode=TwoWay}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Enable SSL:</TextBlock>
                        <CheckBox VerticalAlignment="Center" IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=EnableSsl}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" ToolTip="Disabled in the demo to protect against accidental deletions.">                        
                        <TextBlock Width="150" Foreground="{StaticResource {x:Static SystemColors.GrayTextBrushKey}}">Delete on Pop:</TextBlock>
                        <CheckBox VerticalAlignment="Center" IsEnabled="False" IsChecked="{Binding Source={x:Static properties:Settings.Default}, Path=DeleteMailAfterPop}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5">
                        <TextBlock Width="150">Timeout:</TextBlock>
                        <TextBox Width="70" Height="34" Text="{Binding Source={x:Static properties:Settings.Default}, Path=Timeout}" />
                    </StackPanel>
                    <StackPanel Orientation="Horizontal" Margin="5" HorizontalAlignment="Right">
                        <Button Command="{Binding FetchMailCommand}" Width="100" Margin="5">OK</Button>
                        <Button Command="{Binding ExitCommand}" Width="100" Margin="5">Cancel</Button>
                    </StackPanel>
                </StackPanel>
            </Border>
        </Grid>
    </Grid>
</Window>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions