Friday, July 31, 2009

How to send Emails with Gmail Account with Attachment

First you need to change the settings in your Gmail account

step 1: Login to your Gmail Account with from which you will be sending e-mails.

step 2: Go to Gmail settings then click on Forwarding and POP/IMAP

step 3: In IMAP Access Check Enable IMAP

step 4: Then go to your application use below code

void AttachFile(string attachmentFile)
{
System.Net.Mail.MailAddress toAddress = new System.Net.Mail.MailAddress("your-reciving-email@gmail.com");

System.Net.Mail.MailAddress fromAddress = new System.Net.Mail.MailAddress("fromAddress@yahoo.com");
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage(fromAddress, toAddress);

mm.Subject = "Email Subject";
System.Net.Mail.Attachment mailAttachment = new System.Net.Mail.Attachment(printScreen);
mm.Attachments.Add(mailAttachment);
mm.IsBodyHtml = true;

mm.BodyEncoding = System.Text.Encoding.UTF8;
sendMail(mm);
}

NOTE1:
How do I specify multiple recipients? Printer Friendly
Because the
To, CC, and Bcc properties are MailAddress collections, to add
additional recipients, all we need to do is call .Add(...) on the
respective properties.


Below is an example that demonstrates adding multiple To, CC, and Bcc addresses.

[ C# ]

static void MultipleRecipients()
{
//create the mail message
MailMessage mail = new MailMessage();


//set the addresses
//to specify a friendly 'from' name, we use a different ctor
mail.From = new MailAddress("me@mycompany.com"
, "Steve James");

//since the To,Cc, and Bcc accept addresses, we can use the same technique as the From address
//since the To, Cc, and Bcc properties are collections, to add multiple addreses, we simply call .Add(...) multple times
mail.To.Add("you@yourcompany.com");
mail.To.Add("you2@yourcompany.com");
mail.CC.Add("cc1@yourcompany.com");
mail.CC.Add("cc2@yourcompany.com");
mail.Bcc.Add("blindcc1@yourcompany.com");
mail.Bcc.Add("blindcc2@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is the body content of the email.";

//send the message
SmtpClient smtp = new SmtpClient("127.0.0.1");
smtp.Send(mail);

string sendMail(System.Net.Mail.MailMessage mm)
{
try
{
string smtpHost = "smtp.gmail.com";

string userName = "your-email-address@gmail.com";//sending Id
string password = "your-password";
System.Net.Mail.SmtpClient mClient = new System.Net.Mail.SmtpClient();

mClient.Port = 587;
mClient.EnableSsl = true;
mClient.UseDefaultCredentials = false;
mClient.Credentials = new NetworkCredential(userName, password);

mClient.Host = smtpHost;
mClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
mClient.Send(mm);
}
catch (Exception ex)

{
MessageBox.Show(ex.Message);
}
}
NOTE2

Have to replace \r\n with html line break "br"
in order to show paragraph and line breaks.
string sNewBody = textBoxBody.Text.Replace("\r\
n", "
");
//mm.Body = textBoxBody.Text;
mm.Body = sNewBody;

Monday, July 27, 2009

Showing a Separate Tooltip for Each Item in a ListBox

http://www.codeproject.com/KB/combobox/ToolTipForListBox.aspx

Introduction

ListBox is a very common control used in Windows Forms. Most often we would need to show some information on each item in the ListBox when the user places the mouse pointer above that item. Sadly there is no straight forward way to do that. The reasons are:

  1. There is no separate mouse hover event for individual items in the list.
  2. As each item in the list is not a control (of course!!), we cannot set a tool tip for items of the list separately. A tooltip can be set only for the ListBox as such.

But don't give up!! If there is a will, there is a way…

Using the Code

The steps required are explained below:

  1. Drag and drop a ToolTip control into the form having the ListBox. I am calling it ListToolTip.

  2. We have to know the ToolTip text for each item in the ListBox. I have an array of strings as items in the ListBox. So I maintain the ToolTip text for each item in another string array. So if I know the index of the Item in the array of items, I can get the corresponding ToolTip text from the ToolTip array using the same index.

  3. Create a handler for the Mouse Move event of the ListBox. Calculate the index of the item over which the mouse pointer is placed now. (This is the tricky part!!)

    1. Calculate the Offset of the item from the top of the list box. This can be obtained by dividing the Y coordinate of the house position by the height of each item. Height of each item is a property of the list box:

      Collapse
      itemIndex = e.Y / objListBox.ItemHeight; 
    2. But the topmost item will not always be the first item in the list. If there is a scrollbar and user scrolls down, then the topmost item visible will change. This means that the index obtained above is not the index of the required item in the list of items. Fortunately ListBox has another property called TopIndex. If we add the TopIndex with the index obtained in the above step, then we will get the correct index.

      Collapse
      itemIndex += objListBox.TopIndex;
  4. Now we know the index of the item over which the mouse pointer is placed. Just take the Tooltip of that item from the Tooltip array and associate it with the ToolTip control.

    Collapse
     if ((itemIndex >= 0) && (itemIndex < m_arrItemToolTips.Length))
    {
    ListToolTip.SetToolTip(objListBox, m_arrItemToolTips[itemIndex]);

    }
  5. If the calculated index is out of the range of valid indexes, hide the ToolTip.

    Collapse
      else
    {
    ListToolTip.Hide(objListBox);
    }

The entire code for the mouse move event handler is given below:

Collapse
private void ItemsListBox_MouseMove(object sender, MouseEventArgs e)

{
try
{
ListBox objListBox = (ListBox)sender;
int itemIndex = -1;
if (m_arrItemToolTips != null)

{
if (objListBox.ItemHeight != 0)
{
itemIndex = e.Y / objListBox.ItemHeight;

itemIndex += objListBox.TopIndex;
}
if ((itemIndex >= 0) && (itemIndex < m_arrItemToolTips.Length))

{
ListToolTip.SetToolTip(objListBox, m_arrItemToolTips[itemIndex]);
}
else
{
ListToolTip.Hide(objListBox);

}
}
}
catch (Exception ex)
{
}
}

Thursday, July 16, 2009

C#: Resize An Image While Maintaining Aspect Ratio and Maximum Height


Function works, but need 2 changes:

1. Set image properties, otherwise resized pics lose image information like date, camera type etc.
2. Need to specify image type when saving
// This allows us to resize the image. It prevents skewed images and
// also vertically long images caused by trying to maintain the aspect
// ratio on images who's height is larger than their width

public void ResizeImage(string OriginalFile, string NewFile, int NewWidth, int MaxHeight, bool OnlyResizeIfWider)
{
System.Drawing.Image FullsizeImage = System.Drawing.Image.FromFile(
OriginalFile);


// Prevent using images internal thumbnail

FullsizeImage.RotateFlip(
System.Drawing.RotateFlipType.Rotate180FlipNone);

FullsizeImage.RotateFlip(
System.Drawing.RotateFlipType.Rotate180FlipNone);


if (OnlyResizeIfWider)
{
if (FullsizeImage.Width <= NewWidth) { NewWidth = FullsizeImage.Width; } } int NewHeight = FullsizeImage.Height * NewWidth / FullsizeImage.Width; if (NewHeight > MaxHeight)
{

// Resize with height instead

NewWidth = FullsizeImage.Width * MaxHeight / FullsizeImage.Height;

NewHeight = MaxHeight;
}


System.Drawing.Image NewImage = FullsizeImage.GetThumbnailImage(NewWidth, NewHeight, null, IntPtr.Zero);

//save all the photo properties
foreach (PropertyItem e in FullsizeImage.PropertyItems)
{
NewImage.SetPropertyItem(e);
}

// Save resized picture
NewImage.Save(NewFile, ImageFormat.Jpeg);
// Clear handle to original file so that we can overwrite it if necessary

FullsizeImage.Dispose();


}