My Linux Stuff - Complete Blog For Linux Articles

My Linux Stuff - Complete Blog For Linux Articles

A Website For Complete Linux OS,Step by Step linux Installtion, Linux Tips and Tricks and Linux Stuff and so on... Connect and sharing here....

TOP 50 ENGINEERING COLLEGES IN INDIA 2014

TOP 50 ENGINEERING COLLEGES IN INDIA 2014

This below survey was taken many form many colleges in India. These Top 50 Engineering Colleges in India have Good Infrastructure, Good Environment, Educations , Staff, Placement , Research Activities and other Facilities are good.

Top 10 Government Engineering Colleges in India

Top 10 Government Engineering Colleges in India

These Government Engineering Colleges in India are really good for all kind of stuff like Education , research , Placement and New Innovation Ideas etc... But Getting seat in these colleges are heavy competition in students .....

Top 10 Colleges In India 2014

Top 10 Colleges In India 2014

Indian Institute Of Technology Delhi,Indian Institute Of Technology Bombay,Indian Institute Of Technology Kanpur,Indian Institute Of Technology Madras,Indian Institute Of Technology Kharagpur,Indian Institute Of Technology Roorkee,University Of Delhi,Indian Institute Of Technology Guwahati,University Of Calcutta,University Of Mumbai, National Institute Of Technology,Trichy.

2014 LATEST SURVEY TOP RANKING ENGINEERING COLLEGES IN INDIA

2014 LATEST SURVEY TOP RANKING ENGINEERING COLLEGES IN INDIA

This below survey was taken many form many colleges in India. These Top 100 Engineering Colleges in India have Good Infrastructure, Good Environment, Educations , Staff, Placement , Research Activities and other Facilities are good. If you want to do Engineering as your dream and try out these colleges

Subscribe Now!

Enter your email address:

Wednesday, March 30, 2016

Sending an Email With and Without Attachments Using C#

Sending an Email is important thing for every Dot net developer working in web applications project. Sometimes required to need send with attachments in email. Sending an email with attachment is easy and simple in C# to send. Follow the below steps to implement in your projects.

In this post I am discussing to send an email in two different ways
    1.   Send e-mail via SMTP (Gmail) using C#.
    2.   Send e-mail via Host Address using C#.



Note: Add these namespace in your code behind  or Controller in C#.
using System.Net;
using System.Net.Mail;

    1.   Send e-mail via SMTP (Gmail) using C#.
Function for Without Attachment
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content
                SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
    client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    client.EnableSsl = true;
                client.Send(message);
            }
        }
Function for With Attachment
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

//Attachment goes here
    System.Net.Mail.Attachment attach;
    attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   attach.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach);

                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content
                SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
    client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    client.EnableSsl = true;
                client.Send(message);
            }
        }
Function for With Multiple Attachments
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

//Attachment 1
    System.Net.Mail.Attachment attach;
    attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   attach.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach);

//Attachment 2
    System.Net.Mail.Attachment attach2;
    Attach2 = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   Attach2.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach2); 
                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content
                SmtpClient client = new SmtpClient("smtp.gmail.com"));
client.Port = 587;
    client.Credentials = new System.Net.NetworkCredential("your mail@gmail.com", "your password");
    client.EnableSsl = true;
                client.Send(message);
            }
        }

   2.   Send e-mail via Host Address using C#.

Function for Without Attachment
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content

SmtpClient client = new SmtpClient();
                client.Host = "10.10.18.10"; // Mention your server host IP Address Here
    client.EnableSsl = true;
                client.Send(message);
            }
        }
Function for With Attachment
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

//Attachment goes here
    System.Net.Mail.Attachment attach;
    attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   attach.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach);

                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content
                SmtpClient client = new SmtpClient();
                client.Host = "10.10.18.10"; // Mention your server host IP Address Here
    client.EnableSsl = true;
    client.Send(message);
            }
        }

Function for send an email Multiple Attachments Using c#
protected void sendEmail()
        {
            using (MailMessage message = new MailMessage())
            {
                message.From = new MailAddress("from_address@gmail.com");
                message.To.Add(new MailAddress("to_address@test.med.ca"));
                message.CC.Add(new MailAddress("copy@domain.com"));
                message.Subject = "Special Medication Request from " + txtPhyName.Text.ToString() + " , " + txtPosTile.Text; //Subject Here

//Attachment 1
    System.Net.Mail.Attachment attach;
    attach = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   attach.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach);

//Attachment 2
    System.Net.Mail.Attachment attach2;
    Attach2 = new System.Net.Mail.Attachment("c:/www/root/filename.png //file extension");
   Attach2.Name = "map.png"; // Here You can describe your filename with or without extension
    message.Attachments.Add(attach2); 
                message.Body = "Drug Name: " + txtdrugName.Text.ToString() + "\n" + "Quantity: " + txtQtyReq.Text;  //Body Of Email Content
                SmtpClient client = new SmtpClient();
                client.Host = "10.10.18.10"; // Mention your server host IP Address Here
    client.EnableSsl = true;
                client.Send(message);
            }
        }

Using above function call in any events to send an email. We can use these in Contact us Form and many more areas where ever required.

If you have any problem in coding comment it here. As soon as possible we will reply to you.


Hope all you understood clearly and it will help you. Let enjoy coding

Sending an Email With and Without Attachments Using C#

Sending an Email With and Without Attachments Using C#

Related Posts Plugin for WordPress, Blogger...