Send Mail with multiple Attachments using asp.net
To
Subject
Message

Attach a file:

















.cs code

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Linq;
using System.Net.Mail;

public partial class Default2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        MailMessage mail = new MailMessage();
        mail.To.Add(txtto.Text);
        mail.From = new MailAddress("[email protected]");
        mail.Subject = txtsubject.Text;
        mail.Body = txtmessage.Text;
        mail.IsBodyHtml = true;

        //Attach file using FileUpload Control and put the file in memory stream
        if (fileUpload1.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload1.PostedFile.InputStream, fileUpload1.FileName));
        }
        if (fileUpload2.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload2.PostedFile.InputStream, fileUpload2.FileName));
        }
        if (fileUpload3.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload3.PostedFile.InputStream, fileUpload3.FileName));
        }
        if (fileUpload4.HasFile)
        {
            mail.Attachments.Add(new Attachment(fileUpload4.PostedFile.InputStream, fileUpload4.FileName));
        }
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com"; //Or Your SMTP Server Address
        smtp.Credentials = new System.Net.NetworkCredential("[email protected]", "password");
        //Or your Smtp Email ID and Password
        smtp.EnableSsl = true;
        smtp.Send(mail);
   
    }

} 

ReplyQuick ReplyReport