Sunday 24 November 2013

How to send an email with multiple attachments with validations in ASP.NET using C#/VB.NET

Hi Friends, in this article I will explain about How to send an email with multiple attachments in ASP.NET using C#/VB.NET.
In ASP.NET:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>How to send an email with multiple attachments in ASP.NET using C#/VB.NET</title>
    <style type ="text/css" >
    .color
    {
        color:Red;
    }
    </style>


</head>
<body>
    <form id="form1" runat="server">
    <div>
   
    <table style ="border:solid 2px gray">
    <tr ><td colspan ="2">
<asp:Label style ="color:red;" runat="server">How to send an email with multiple attachments.</td> </asp:label></tr>
    <tr>
    <td><asp:Label ID="Label1" runat="server" >Your Email:</asp:Label></td>
    <td> <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox>
    <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ErrorMessage="*" ControlToValidate ="txtFrom" CssClass ="color"></asp:RequiredFieldValidator>   
    </td>
    </tr>
     <tr>
    <td><asp:Label ID="Label8" runat="server" >Password:</asp:Label></td>
    <td> <asp:TextBox ID="txtPwd" runat="server" TextMode="Password" ></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ErrorMessage="*" ControlToValidate ="txtPwd" CssClass ="color"></asp:RequiredFieldValidator>       
    </td>
    </tr>
    <tr>
    <td><asp:Label ID="lbl1" runat="server" >To:</asp:Label></td>
    <td>
    <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ErrorMessage="*" ControlToValidate ="txtTo" CssClass ="color"></asp:RequiredFieldValidator>   
    </td>
    </tr>
     <tr>   
     <tr>
    <td><asp:Label ID="Label2" runat="server" >CC:</asp:Label></td>
    <td> <asp:TextBox ID="txtCC" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ErrorMessage="*" ControlToValidate ="txtCC" CssClass ="color"></asp:RequiredFieldValidator>   
    </td>
    </tr>
     <tr>
    <td><asp:Label ID="Label3" runat="server" >Subject:</asp:Label></td>
    <td> <asp:TextBox ID="txtSub" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server" ErrorMessage="*" ControlToValidate ="txtSub" CssClass ="color"></asp:RequiredFieldValidator>   
    </td>
    </tr>
     <tr>
    <td><asp:Label ID="Label4" runat="server" >Body:</asp:Label></td>
    <td> <asp:TextBox ID="txtBody" runat="server"></asp:TextBox>
     <asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ErrorMessage="*" ControlToValidate ="txtBody" CssClass ="color"></asp:RequiredFieldValidator>   
    </td>
    </tr>
    <tr>
    <td><asp:Label ID="label5" runat="server">Attachments 1:</asp:Label></td>
    <td><asp:FileUpload ID="fileAttach1" runat ="server" multiple="true" />
    </td>
    </tr>
    <tr>
    <td><asp:Label ID="label6" runat="server">Attachments 2:</asp:Label></td>
    <td><asp:FileUpload ID="fileAttach2" runat ="server" /></td>
    </tr>
    <tr>
    <td><asp:Label ID="label7" runat="server">Attachments 3:</asp:Label></td>
    <td><asp:FileUpload ID="fileAttach3" runat ="server" /></td>
    </tr>
    </table>
    <asp:Button ID="btnSend" runat="server" Text="Send Email" onclick="btnSend_Click" />   
    </div>  
    </form>
</body>
</html>

In C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Net;
using System.Net.Mail;
using System.Configuration;

public partial class sendMailHardCode : System.Web.UI.Page
{
    protected void btnSend_Click(object sender, EventArgs e)
    {
        MailMessage mail_mes = new MailMessage();
        mail_mes.To.Add(txtTo.Text);
        mail_mes.From = new MailAddress(txtFrom.Text);
        mail_mes.Subject = txtSub.Text;
        mail_mes.Body = txtBody.Text;
        mail_mes.IsBodyHtml = true;

        //Attach the files to send using FileUpload Control
        if (fileAttach1.HasFile)
        {
            mail_mes.Attachments.Add(new Attachment(fileAttach1.PostedFile.InputStream, fileAttach1.FileName));
        }
        if (fileAttach2.HasFile)
        {
            mail_mes.Attachments.Add(new Attachment(fileAttach2.PostedFile.InputStream, fileAttach2.FileName));
        }
        if (fileAttach3.HasFile)
        {
            mail_mes.Attachments.Add(new Attachment(fileAttach3.PostedFile.InputStream, fileAttach3.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential(txtFrom.Text, txtPwd.Text);
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        try
        {
            smtp.Send(mail_mes);
        }
        catch (Exception ex)
        {
            throw ex;
        }
           
    }
}



In VB.NET:
Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Configuration

Partial Public Class sendMailHardCode
    Inherits System.Web.UI.Page
    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim mail_mes As New MailMessage()
        mail_mes.[To].Add(txtTo.Text)
        mail_mes.From = New MailAddress(txtFrom.Text)
        mail_mes.Subject = txtSub.Text
        mail_mes.Body = txtBody.Text
        mail_mes.IsBodyHtml = True

        'Attach the files to send using FileUpload Control
        If fileAttach1.HasFile Then
            mail_mes.Attachments.Add(New Attachment(fileAttach1.PostedFile.InputStream, fileAttach1.FileName))
        End If
        If fileAttach2.HasFile Then
            mail_mes.Attachments.Add(New Attachment(fileAttach2.PostedFile.InputStream, fileAttach2.FileName))
        End If
        If fileAttach3.HasFile Then
            mail_mes.Attachments.Add(New Attachment(fileAttach3.PostedFile.InputStream, fileAttach3.FileName))
        End If
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        'Or Your SMTP Server Address
        smtp.Credentials = New System.Net.NetworkCredential(txtFrom.Text, txtPwd.Text)
        'Or your Smtp Email ID and Password
        smtp.EnableSsl = True
        Try
            smtp.Send(mail_mes)
        Catch ex As Exception
            Throw ex
        End Try

    End Sub
End Class


The output of the above page as shown in the below  figure.When you give all the values and click send email button then the mail is sent with attached files what you choose.



No comments:

Post a Comment

© 2012-2018 Aspdotnet-Kishore.blogspot.com. All Rights Reserved.
The content is copyrighted to Kishore and may not be reproduced on other websites without permission from the owner.