CSharp.Net-Informations.com



     CSharp Communication Tutorial

 * How to send email with attachment from C#


How to send email with attachment from C#

C# simplifies network programming in .Net framework. C# describes various protocols using communication programming like Socket communications , SMTP mail , UDP , URL etc. The System.Net classes uses to communicate with other applications by using the HTTP, TCP, UDP, Socket etc. In the previous program we saw how to SMTP email from C# describes how to send an email with text body . Here we are sending an email with an attachment.

  System.Net.Mail.Attachment attachment;
  attachment = new System.Net.Mail.Attachment("c:\\containers.xls");

mail.Attachments.Add(attachment);

The following C# source code shows how to send an email with an attachment from a Gmail address . The Gmail SMTP server name is smtp.gmail.com and the port using send mail is 587 . Here using NetworkCredential for password based authentication.

  SmtpServer.Credentials =

new System.Net.NetworkCredential("username", "password");


         C# Source Code Download           Print Source Code
         How to send email with attachment from C# - Download
        
C# Tutorial

using System;
using System.Windows.Forms;
using System.Net.Mail;

namespace WindowsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                MailMessage mail = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
                mail.From = new MailAddress("your_email_address@gmail.com");
                mail.To.Add("to_address");
                mail.Subject = "Test Mail - 1";
                mail.Body = "mail with attachment";

                System.Net.Mail.Attachment attachment;
                attachment = new System.Net.Mail.Attachment("you attachment file");
                mail.Attachments.Add(attachment);

                SmtpServer.Port = 587;
                SmtpServer.Credentials = new System.Net.NetworkCredential("username", "password");
                SmtpServer.EnableSsl = true;

                SmtpServer.Send(mail);
                MessageBox.Show("mail Send");
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.ToString());
            }
        }
    }
}
		

You have to provide the necessary information like your gmail username and password etc.

CSharp Communications Related Contents
*     How to send email from C#
*     How to send html email from C#
*     How to send cdo email from C#
*     How to find hostname of a computer
*     How to find IP Adress of a computer
*     How to read URL Content from webserver
*     How to C# Socket programming
*     C# Server Socket program
*     C# Client Socket program
*     C# Multi threaded socket programming
*     C# Multi threaded Server Socket programming
*     C# Multi threaded Client Socket programming
*     How to C# Chat server programming
*     How to C# Chat Server
*     How to C# Chat Client

CSharp Related Topics
*     An overview of Microsoft CSharp
*     C# Language Tutorial
*     C# Statements Tutorial
*     C# Collection Tutorial
*     C# String Tutorial
*     C# File Operations Tutorial
*     C# Excel Tutorial
*     C# Crystal Reports Tutorial
*     CSharp Communication Tutorial
*     C# Ado.Net Tutorial and Source Code
*     C# ADO.NET data Providers Tutorial
*     C# Dataset Tutorial
*     C# DataAdapater Tutorial
*     Csharp DataView Tutorial
*     Csharp Remoting Tutorial
*     C# XML Tutorial
Search here for more CSharp Source Code :


   Categories

    HOME
    VB.NET
    CSHARP

 
 
   













  |  Home   |  SiteMap   |  About   |
net-informations.com (C) 2010 All Rights Reserved