Thursday 26 May 2016

How to send email Asynchronously in ASP.Net using Background Thread

HTML Markup

<table border="0" cellpadding="0" cellspacing="0">
    <tr>
        <td style="width: 80px">
            To:
        </td>
        <td>
            <asp:TextBox ID="txtTo" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Subject:
        </td>
        <td>
            <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td valign = "top">
            Body:
        </td>
        <td>
            <asp:TextBox ID="txtBody" runat="server" TextMode = "MultiLine" Height = "150" Width = "200"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            File Attachment:
        </td>
        <td>
            <asp:FileUpload ID="fuAttachment" runat="server" />
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Email:
        </td>
        <td>
            <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
            Gmail Password:
        </td>
        <td>
            <asp:TextBox ID="txtPassword" runat="server" TextMode = "Password"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>
            &nbsp;
        </td>
    </tr>
    <tr>
        <td>
        </td>
        <td>
            <asp:Button Text="Send" OnClick="SendAsyncEmail" runat="server" />
        </td>
    </tr>
</table>



Namespaces
You will need to import the following namespaces
C#

using System.IO;
using System.Net;
using System.Net.Mail;
using System.Threading;

VB.Net

Imports System.IO
Imports System.Net
Imports System.Net.Mail
Imports System.Threading


Sending email asynchronously in background using Thread


C#

protected void SendAsyncEmail(object sender, EventArgs e)
{
    string to = txtTo.Text;
    string from = txtEmail.Text;
    string password = txtPassword.Text;
    string subject = txtSubject.Text;
    string body = txtBody.Text;
    HttpPostedFile postedFile = fuAttachment.PostedFile;
    Thread email = new Thread(delegate()
    {
        SendEmail(to, from, password, subject, body, postedFile);
    });
 
    email.IsBackground = true;
    email.Start();
    ClientScript.RegisterStartupScript(GetType(), "alert", "alert('Email sent.');", true);
}
 
private void SendEmail(string to, string from, string password, string subject, string body, HttpPostedFile postedFile)
{
    using (MailMessage mm = new MailMessage(from, to))
    {
        mm.Subject = subject;
        mm.Body = body;
        if (postedFile.ContentLength > 0)
        {
            string fileName = Path.GetFileName(postedFile.FileName);
            mm.Attachments.Add(new Attachment(postedFile.InputStream, fileName));
        }
        mm.IsBodyHtml = false;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";
        smtp.EnableSsl = true;
        NetworkCredential NetworkCred = new NetworkCredential(from, password);
        smtp.UseDefaultCredentials = true;
        smtp.Credentials = NetworkCred;
        smtp.Port = 587;
        smtp.Send(mm);
    }
}

VB.Net

Protected Sub SendAsyncEmail(sender As Object, e As EventArgs)
    Dim [to] As String = txtTo.Text
    Dim from As String = txtEmail.Text
    Dim password As String = txtPassword.Text
    Dim subject As String = txtSubject.Text
    Dim body As String = txtBody.Text
    Dim postedFile As HttpPostedFile = fuAttachment.PostedFile
    Dim email As New Thread(Sub() SendEmail([to], from, password, subject, body, postedFile))
    email.IsBackground = True
    email.Start()
    ClientScript.RegisterStartupScript([GetType](), "alert", "alert('Email sent.');", True)
End Sub
 
Private Sub SendEmail([to] As String, from As String, password As String, subject As String, body As String, postedFile As HttpPostedFile)
    Using mm As New MailMessage(from, [to])
        mm.Subject = subject
        mm.Body = body
        If postedFile.ContentLength > 0 Then
            Dim fileName As String = Path.GetFileName(postedFile.FileName)
            mm.Attachments.Add(New Attachment(postedFile.InputStream, fileName))
        End If
        mm.IsBodyHtml = False
        Dim smtp As New SmtpClient()
        smtp.Host = "smtp.gmail.com"
        smtp.EnableSsl = True
        Dim NetworkCred As New NetworkCredential(from, password)
        smtp.UseDefaultCredentials = True
        smtp.Credentials = NetworkCred
        smtp.Port = 587
        smtp.Send(mm)
    End Using
End Sub

No comments:

Post a Comment