RSS

Sending E-mail with attachments

25 Nov
 using System;
using System.IO;
using System.Net.Mail;
using System.Net.Mime;

namespace email4
{
class Program
{
static void Main(string[] args)
{
MailMessage m = new MailMessage();
SmtpClient sc = new SmtpClient();

try
{
m.From = new MailAddress("sender_email_address", "sender_display_name");
m.To.Add(new MailAddress("receiver_email_address", "receiver_display_name"));
m.CC.Add(new MailAddress("receiver_email_address", "receiver_display_name"));
//similarly BCC
m.Subject = "Test1";
m.IsBodyHtml = true;
m.Body = " This is a Test Mail";

FileStream fs = new FileStream("C:\\Documents and Settings\\All Users\\Documents\\test.pdf",
FileMode.Open, FileAccess.Read);
Attachment a = new Attachment(fs, "test.pdf",
MediaTypeNames.Application.Octet);
m.Attachments.Add(a);

sc.Host = "smtp.gmail.com";
sc.Port = 587;
sc.Credentials = new
System.Net.NetworkCredential("gmail_account_id", "gmail_account_password");
sc.EnableSsl = true;
sc.Send(m);

Console.ReadLine();

}
catch (Exception ex)
{
Console.WriteLine(ex.Message);

}
}
}
}


 
Advertisements
 
Leave a comment

Posted by on November 25, 2011 in C#.Net

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: