Introduction
Using this code snippet, the user would be able to send an email with attachment through Outlook, Yahoo, AOL, HOTMAIL and Gmail.
Background
There is already loads of information that is available on the internet for the same purpose. But not all code is placed in one place. So here is my small effort to accumulate all the code at one place and that is nothing better than CodeProject. Hopefully, it will be helpful for beginners or whoever is in need of it.
Using the Code
Name Space
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using Outlook = Microsoft.Office.Interop.Outlook;
Block Diagram (SMTP Setting for Various Client)
Below is the block diagram for the SMTP settings I have used to send out an email from various client providers.
Wherever needed, I have explained the code in detail. The code is self explanatory, anyway for the same, I have included comments. So here is the code snippet to achieve your purpose.
1. Using Outlook
To send an email using Outlook, we need to add a reference to the dynamic link library for Outlook which is called Microsoft.Office.Interop.Outlook.dll.
For the same, follow the below steps:
- Go to your solution explorer.
- Click on add a reference.
- Click on .NET Tab.
- Go through the DLL and select Microsoft.Office.Interop.Outlook.dll correctly.
- When you have selected the correct reference, you select the “
OK
” button and this reference will be added to your project under references.
- Now we need to add a reference in our class to the Outlook reference we have added to the project in our previous example.
using Outlook = Microsoft.Office.Interop.Outlook;
And finally, the code would look something like this:
public void sendEMailThroughOUTLOOK()
{
try
{
Outlook.Application oApp = new Outlook.Application();
Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
oMsg.HTMLBody = "Hello, Jawed your message body will go here!!";
String sDisplayName = "MyAttachment";
int iPosition = (int)oMsg.Body.Length + 1;
int iAttachType = (int)Outlook.OlAttachmentType.olByValue;
Outlook.Attachment oAttach = oMsg.Attachments.Add
(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName);
oMsg.Subject = "Your Subject will go here.";
Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients;
Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add("jawed.ace@gmail.com");
oRecip.Resolve();
oMsg.Send();
oRecip = null;
oRecips = null;
oMsg = null;
oApp = null;
}
catch (Exception ex)
{
}
}
2. Using HOTMAIL
To send an email using HotMail, we need to add a reference to the dynamic link library for Hotmail/Gmail/AOL/Yahoo which is called System.Net.Mail
.
For the same, follow the below steps:
- Go to solution explorer of your project
- Select add a reference
- Click on .NET Tab
- Go through the DLL and select
System.Net.Mail
- When you have selected the correct reference, you select the “OK” button and this reference will be added to your project under references.
- Now we need to add a reference in our class to the Hotmail/gmail/aol/yahoo reference we have added to the project.
using System.Net.Mail;
Note: The HOTMAIL SMTP Server requires an encrypted connection (SSL) on port 25.
And finally, the code would look something like this:
public void sendEMailThroughHotMail()
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("sender@hotmail.com");
mM.To.Add("rcver@gmail.com");
mM.Subject = "your subject line will go here";
mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
mM.Body = "Body of the email";
mM.IsBodyHtml = true;
SmtpClient sC = new SmtpClient("smtp.live.com");
sC.Port = 25;
sC.Credentials = new NetworkCredential("sender@hotmail.com","HotMailPassword");
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception ex)
{
}
}
3. Using Yahoo!
public void sendEMailThroughYahoo()
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("sender@yahoo.com");
mM.To.Add("recvEmailid@gmail.com");
mM.Subject = "your subject line will go here.";
mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
mM.Body = "Your Body of the email.";
mM.IsBodyHtml = false;
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("sender@yahoo.com", "password");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.mail.yahoo.com";
SmtpServer.Send(mM);
}
catch (Exception ex)
{
}
}
4. Using AOL
public void sendEMailThroughAOL()
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("sender@aol.com");
mM.To.Add("recvEmailid@gmail.com");
mM.Subject = "your subject line will go here.";
mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
mM.Body = "Your Body of the email.";
mM.IsBodyHtml = false;
SmtpClient SmtpServer = new SmtpClient();
SmtpServer.Credentials = new System.Net.NetworkCredential("sender@aol.com", "AOLpassword");
SmtpServer.Port = 587;
SmtpServer.Host = "smtp.aol.com";
SmtpServer.Send(mM);
}
catch (Exception ex)
{
}
}
5. Using Gmail
Note: The GMAIL SMTP Server requires an encrypted connection (SSL) on port 487.
public void sendEMailThroughGmail()
{
try
{
MailMessage mM = new MailMessage();
mM.From = new MailAddress("sender@gmail.com");
mM.To.Add("rcver@gmail.com");
mM.Subject = "your subject line will go here";
mM.Attachments.Add(new Attachment(@"C:\\attachedfile.jpg"));
mM.Body = "Body of the email";
mM.IsBodyHtml = true;
SmtpClient sC = new SmtpClient("smtp.gmail.com");
sC.Port = 587;
sC.Credentials = new NetworkCredential("sender@gmail.com", "GmailPassword");
sC.EnableSsl = true;
sC.Send(mM);
}
catch (Exception ex)
{
}
}
Already, I am using the above code snippet for my automation purposes and it's working fine for me!!
http://jawedm.blogspot.com
Thank you!!
Feel free to provide your comments and vote.