Saturday, 14 July 2012

Sending mail with multiple file attachments using gmail in Asp.Net

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Send mail with multiple file attachments using Gmail in Asp.Net</title>
    <script type="text/javascript">
        function addTypeFile() {
            if (!document.getElementById || !document.createElement)
                return false;

            var uploadArea = document.getElementById("upload-area");

            if (!uploadArea)
                return;

            var newLine = document.createElement("br");
            uploadArea.appendChild(newLine);

            var newTypeFile = document.createElement("input");
            newTypeFile.type = "file";
            newTypeFile.size = "60";

            if (!addTypeFile.lastAssignedId)
                addTypeFile.lastAssignedId = 100;

            newTypeFile.setAttribute("id", "file1" + addTypeFile.lastAssignedId);
            newTypeFile.setAttribute("name", "file1:" + addTypeFile.lastAssignedId);
            uploadArea.appendChild(newTypeFile);
            addTypeFile.lastAssignedId++;
        }
</script>

</head>
<body>
<form id="form1" runat="server" enctype="multipart/form-data">
<p>To:</p>
<p>
<asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
</p>
<p>Subject:</p>
<p>
<asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
</p>
<p>Message Body:</p>
<p>
<asp:TextBox ID="txtMsgBody" runat="server" TextMode="MultiLine" Columns="80" Rows="10"></asp:TextBox>
</p>
<p>
Attachment:
</p>
<p id="upload-area">
   <input id="File1" type="file" runat="server" size="60" />
</p>
<p>
<a href="#" onclick="addTypeFile()">Attach another file</a>
</p>
<p><asp:Button ID="btnSubmit" runat="server" Text="Send" OnClick="btnSubmit_Click" /></p>
<span id="Span1" runat="server" />
</form>
</body>
</html>

aspx.cs code

using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;

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

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
         try
        {
            List<Attachment> attachment = new List<Attachment>();
            HttpFileCollection uploads = HttpContext.Current.Request.Files;
            for (int i = 0; i < uploads.Count; i++)
            {

                if (uploads[i].FileName.Length == 0)
                    continue;
                attachment.Add(new Attachment(uploads[i].InputStream, uploads[i].FileName));
            }
            MailSender.SendMail(txtTo.Text, txtSubject.Text, txtMsgBody.Text, attachment);
            Span1.InnerHtml = "Mail send successfully." ;
        }
        catch (Exception ex)
        {
            Span1.InnerHtml = ex.Message;
        }
    }
    }

Class File

using System.Web;
using System.Net;
using System.Net.Mail;

/// <summary>
/// Summary description for MailSender
/// </summary>
public class MailSender
{
    public MailSender()
    {
        //
        // TODO: Add constructor logic here
        //
    }

    public static bool SendMail(String to, String subject, String messageBody, List<Attachment> attachmentCollection)
    {
        try
        {
            NetworkCredential networkCredential = new NetworkCredential("usermailid@gmail.com", "password");
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(networkCredential.UserName);
            mailMessage.To.Add(new MailAddress(to));
            mailMessage.Subject = subject;
            mailMessage.Body = messageBody;
            mailMessage.IsBodyHtml = true;

            foreach (Attachment attachment in attachmentCollection)
            {
                mailMessage.Attachments.Add(attachment);
            }

            mailMessage.Priority = MailPriority.High;
            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com");
            smtpClient.Port = 587;
            smtpClient.EnableSsl = true;
            smtpClient.UseDefaultCredentials = false;
            smtpClient.Credentials = networkCredential;
            smtpClient.Send(mailMessage);

            return true;
        }
        catch (Exception)
        {
            return false;
        }
    }
}

No comments:

Post a Comment