Monday, August 31, 2015

C# Email Utility

http://www.csharphelp.com/2007/09/c-email-utility/

C# Email Utility

Introduction
This useful C# .NET email utility configureseasily identifying the receiver, sender, message content and mailserver requiring minimal time for implementation.

Creating the Workspace
To start the Email Utility project, open aMicrosoft Visual Studio 2005, .NET Framework Version 2.0 developmentenvironment and create a Console Application. Add two C# classes, onecalled EmailHandler.cs and another called AppHandler.cs and anApplication Configuration File called the default, App.config.

Code
As with any Microsoft C# project, theapplication consists of the configuration properties and theapplication. By default the App.config file is XML with a primaryelement named .configuration.. To the default .configuration. element,set the properties required for email configuration:
. Recipient
. Sender
. Copy List
. Priority
. Body Encoding
. Subject
. Message Body
. SMTP Server


App.config
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Email.Recipient" value="John.Smith@abc.com, Joe.Wilson@abc.com" />
<add key="Email.Sender" value="Bob.Jones@xyz.com " />
<add key="Email.Copy" value="mycclist@xyz.com" />
<add key="Email.Priority" value="MailPriority.Normal "/>
<add key="Email.Encoding" value="Encoding.ASCII"/>
<add key="Email.Subject" value=".NET Email Utility Sample"/>
<add key="Email.Message" value="This email provides an example of sending a .NET email using C#."/>
<add key="Email.Smtp" value="mailhost.xyz.com" />
</appSettings>
</configuration>

The Email Handler
The Email Handler provides a simple method forsending the email using the parameters defined in the applicationconfiguration file.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;
using System.IO;

namespace EmailApp
{
public sealed class EmailHandler
{
private static char[] charSeparators = new char[] { };
private static String[] result;
public static void SendMailMessage(string smtpHost, MailAddress from, string to, string subject, string message)
{
try
{
MailMessage mailMsg = new MailMessage();
result = to.Split(charSeparators, StringSplitOptions.RemoveEmptyEntries);
for (int count = 0; count < result.Length; count++)
{ mailMsg.To.Add(new MailAddress(result[count])); }
mailMsg.From = from;
mailMsg.Subject = subject;
mailMsg.Body = message;
mailMsg.Priority = AppHandler.EmailPriority;
mailMsg.BodyEncoding = AppHandler.EmailEncoding;
SmtpClient smtpClient = new SmtpClient(smtpHost);
smtpClient.Send(mailMsg);
}
catch (Exception exc)
{}
}}}
The AppHandler class makes the application parameters accessible as static variables in the EmailHandler class.

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace EmailApp
{
public static class AppHandler
{
private static MailAddress emailRecipient;
private static String emailSender;
private static String emailCopy;
private static String emailPriority;
private static String emailEncoding;
private static String emailSubject;
private static String emailMessage;
private static String emailSmtp;
public static MailAddress EmailRecipient
{get { return emailRecipient; }
set { emailRecipient = value; }}
public static String EmailSender
{get { return emailSender; }
set { emailSender = value; }}
public static String EmailCopy
{get { return emailCopy; }
set { emailCopy = value; }}
public static String EmailPriority
{get { return emailPriority; }
set { emailPriority = value; }}
public static String EmailEncoding
{get { return emailEncoding; }
set { emailEncoding = value; }}
public static String EmailSubject
{get { return emailSubject; }
set { emailSubject = value; }}
public static String EmailMessage
{get { return emailMessage; }
set { emailMessage = value; }}
}}

The EmailHandler usage involves setting the configuration parameters and calling one line of code.

AppHandler.EmailRecipient = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["Email.Recipient"]);
AppHandler.EmailSender = System.Configuration.ConfigurationManager.AppSettings["Email.Sender"];
AppHandler.EmailCopy = System.Configuration.ConfigurationManager.AppSettings["Email.Copy"];
AppHandler.EmailPriority = System.Configuration.ConfigurationManager.AppSettings["Email.Priority"];
AppHandler.EmailEncoding = System.Configuration.ConfigurationManager.AppSettings["Email.Encoding"];
AppHandler.EmailSubject = System.Configuration.ConfigurationManager.AppSettings["Email.Subject"];
AppHandler.EmailMessage = System.Configuration.ConfigurationManager.AppSettings["Email.Message"];
AppHandler.EmailSmtp = System.Configuration.ConfigurationManager.AppSettings["Email.Smtp"];

EmailHandler.SendEmail(AppHandler.EmailSmtp, AppHandler.EmailSender, AppHandler.EmailRecipient, AppHandler.EmailSubject, AppHandler.EmailMessage);

Variable or Dynamic ListBox

A ListBox can implement generics for a dynamic set of columns and rows.  Here's how:

http://www.hightechtalks.com/csharp/

Dear all,

Please can I make my listbox in a variable manner?
I have 3 listboxes and I want to make the number for listbox is a variable ?
I need to display an 2d-array in 3 listboxes .

for (int k = 0; k != array_cols; k++)
{ for (int i = 0; i != array_rows; i++)
{ arraytimes[i, j] = Multi;
Multi = Multi * 10;
listBox($k).Items.Add(arraytimes[i, k]); }}

Have you considered a ListBox that binds to generic Lists as the datasource? This design might be more efficient than a ListBox with variables that binds to an array.

List<dynamic> dynList = new List<dynamic>() {
new {Id = 1, Name = "Elevator", Company="Vertical Pop" },
new {Id = 2, Name = "Stairs", Company="Fitness" }
};

listBox.DataSource = dynList;
listBox.DisplayMember = "Name";
listBox.ValueMember = "Id";


Negative rowIndex for a DataGridViewRow

Negative rowIndex problem for new DataGridViewRow

I create and add a new row to an unbound DataGridView.  Then, if I try and
access an item in the Cells collection of the row, as in my commented out
code, I get an index out of range exception, rowIndex.  I'm not specifying a
row index, so I assume it is inferring it from the row object, whose
rowIndex property is -1.  However, when I access the cell through a row in
the grids Rows collection, I'm happy, as in my other code.  Can anyone
explain this behaviour?

The exception:

Specified argument was out of the range of valid values.

Parameter name: rowIndex

The code:

            ExportLineRow newRow = new ExportLineRow(); // Derived from
DataGridViewRows.

            newRow.ExportLine = newLIne;

            newRow.CreateCells(dgvLayout);

            dgvLayout.Rows.Add(newRow);

            int cx = 1;

            foreach (Column o in
newRow.ExportLine.RecordLayout.Columns.Values)

Re: Negative rowIndex problem for new DataGridViewRow

Hi Brady,

With the DataGridView, the rows could be accessed iteratively up to a
size of myGrid.Rows.Count

Here is a webpage with an example of how to access a row/column
reference as you describe.

http://msdn2.microsoft.com/en-us/library/system.windows.forms.datagridview.rowcount.aspx

On Jan 27, 2008 1:00 PM, Brady Kelly <brady <at> chasesoftware.co.za> wrote:
> I create and add a new row to an unbound DataGridView.  Then, if I try and
> access an item in the Cells collection of the row, as in my commented out
> code, I get an index out of range exception, rowIndex.  I'm not specifying a
> row index, so I assume it is inferring it from the row object, whose
> rowIndex property is -1.  However, when I access the cell through a row in
> the grids Rows collection, I'm happy, as in my other code.  Can anyone
> explain this behaviour?
>
>
>
> The exception:
>
>
>
> Specified argument was out of the range of valid values.
>
> Parameter name: rowIndex
>
>

Re: Negative rowIndex problem for new DataGridViewRow

Hi Kara,

Thanks for the link, but I've determined my problem is somehow related to me
trying to add row objects derived from DataGridViewRow.  I suspect I'm not
overriding a required member somewhere, because I can add one custom row,
but all other rows after that break when I add them.

>
> Hi Brady,
>
> With the DataGridView, the rows could be accessed iteratively up to a
> size of myGrid.Rows.Count
>
>
> Here is a webpage with an example of how to access a row/column
> reference as you describe.
>
> http://msdn2.microsoft.com/en-
> us/library/system.windows.forms.datagridview.rowcount.aspx

Re: Negative rowIndex problem for new DataGridViewRow

Brady

DId that work?  If not, could you zip up the part of your code with
the data grid view and email it to my email address

kara.hewett <at> gmail.com

I'll debug the code to see what's going on.

kara

On Jan 27, 2008 1:00 PM, Brady Kelly <brady <at> chasesoftware.co.za> wrote:
> I create and add a new row to an unbound DataGridView.  Then, if I try and
> access an item in the Cells collection of the row, as in my commented out
> code, I get an index out of range exception, rowIndex.  I'm not specifying a
> row index, so I assume it is inferring it from the row object, whose
> rowIndex property is -1.  However, when I access the cell through a row in
> the grids Rows collection, I'm happy, as in my other code.  Can anyone
> explain this behaviour?
>
>
>
> The exception:
>
>
>
> Specified argument was out of the range of valid values.
>
> Parameter name: rowIndex
>

Bytes Posts by Kara Hewett

I am a frequent bytes contributor.  Although this help is archived after awhile, the links can be found below.

How to add local database in installshield limited edition?
Replies: 2
Views: 524
Posted By Kara Hewett
Does the project use ODBC? If it uses ODBC, then please ensure that the ODBC connection has been created for the database.
WinForm: Drag & Drop is blocked
Replies: 
Views: 423
Posted By Kara Hewett
You can handle the PreviewMouseUp event to catch the moment when the user clicks on a column header:

[C#]

private void view_PreviewMouseUp(object sender,...
 
Replies: 5
Reading html tag Using Xml reader
Views: 958
Posted By Kara Hewett
Hi HtmlAgilityPack and regular expressions...

Hi

HtmlAgilityPack and regular expressions can be used to parse HTML very effectively. HTML can be formatted using "classes". You can then parse on the class such as:

XPathValues =...
 
Replies: 5
What can I do about this?
Views: 435
Posted By Kara Hewett
A few good examples of Raise and Control of Print...

A few good examples of Raise and Control of Print Dialog Boxes can be found here:

http://www.tutorialspoint.com/vb.net/vb.net_print_dialog.htm

http://support.microsoft.com/kb/322710/en-us/
 
Replies: 1
Database connection in Designer hosting and retriving data from databas.
Views: 437
Posted By Kara Hewett
What version of C# are you using? WPF provides...

What version of C# are you using? WPF provides fast data retrieval and binding to a database into data views.
 
Replies: 6
If it is possible? some of different fields merge in a calculation filed on a query
Views: 341
Posted By Kara Hewett
The Microsoft Visual Basic string class includes...

The Microsoft Visual Basic string class includes numerous methods that could be used for the merge of fields. The complete list is at the URL below. In addition to Left, Right and Mid, you could...
 
Replies: 0
WPF RxFramework Guide
Views: 485
Posted By Kara Hewett
WPF RxFramework Guide

Could someone recommend a comprehensive WPF Rx (Reactive Extensions) Framework? I've found many individual code snippets and tutorials but not an in-depth, comprehensive guide?
 
Replies: 3
C# Data Grid Toolkit
Views: 577
Posted By Kara Hewett
Thank you! Would you have a suggestion for WPF...

Thank you! Would you have a suggestion for WPF that has similar functionality to DevExpress WPF GridControl but free?
 
Replies: 3
C# Data Grid Toolkit
Views: 577
Posted By Kara Hewett
C# Data Grid Toolkit

Does anyone have a good C# DataGrid toolkit that is free but includes data sorting, grouping, summing, and configuring colors on the fly? Also I would like to support large datasets of millions of...
 
Replies: 2
how to connect/save/update data to the database ?
Views: 3,378
Posted By Kara Hewett
The connection to the database should use OLEDB. ...

The connection to the database should use OLEDB.

Sub DB_Utility()
Dim strConnection as String = "Provider=Microsoft.Jet.OLEDB.4.0;"
strConnection += "Data Source=Northwind.mdb"
...