Send Email with Attachment using C# & SMTP

//Create New MailMessage Object
MailMessage mailMessage = new MailMessage();

//Set SMTP From Address
mailMessage.From = new MailAddress(smtpFrom);

//Set Receiver’s Address
mailMessage.To.Add("Mail Receiver's Address ( Can use ';' separated for bulk addresses"“);

//Enable HTML Tags inside the mail Body
mailMessage.IsBodyHtml = true;

//Set the subject and body of the message
mailMessage.Subject = “Subject”;

//If your are using some multi-line textbox to body you can use mailMessage.Body=BodyTextBox.Text.Replace(Environment.NewLine, "</br>") to separate line by line

//Create WebClient object to Access Attachment
WebClient webClient = new WebClient();

//Parse net credentials, if you want to parse default network creadentials you can use like this,
webClient.Credentials = CredentialCache.DefaultNetworkCredentials;
But some times if there has some permission issues with that default network credentials this will not work,
So better option is use one service account for access web client like below

webClient.Credentials = new NetworkCredential("username","Password","Domain");

//Set document to byte array
byte[] data = webClient.DownloadData("Attachement url");

//get it into Memory stream
MemoryStream memoryStreamOfFile = new MemoryStream(data);

//Add Attachment to mail
mailMessage.Attachments.Add(new System.Net.Mail.Attachment(memoryStreamOfFile, "Document Name"));

//Create the SMTP client object and send the Email, that’s all
SmtpClient smtpClient = new SmtpClient("smtpServer");


Hope you learned throw this, Have a nice day, will meetup with new thing,
Udara Eshan


Author: eshan1604

Hi Guys, I'm Udara Eshan Ariyarathne from Sri Lanka. Currently i'm working as a Software Engineer (Microsoft Technologies). I'm sharing some IT related articles such as Development ,Configuration and Designing things what i have learn from day to day works. Hope it useful all of us. "Enjoying IT" Thanks & Regards, Udara Eshan Ariyarthne

Leave a Reply

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

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

Google+ photo

You are commenting using your Google+ 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 )


Connecting to %s