Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to send email in Asp.net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 87
    Comment on it

    To send an email in c#, we will create an application in which user can login via gmail id and password and send the mails. Now Firstly we will understand what is required to create email application in c#.

    If we want to send the email in c# we have to use SMTP.

    SMTP:This is network protocol (Simple mail transfer protocol), which is used to send the mail via a secure connection.

    We have to use “System.web.util” namespace to include this protocol in our application.

    It uses “SEND” method to send the mail. In SMTP SEND method, we can either pass four arguments(FROM,TO,SUBJECT,MESSAGETEXT) or we can pass mailmessage.

    SMTP uses Port No:587 and host as smtp.gmail.com to send the emails.

    Syntax:

    Smtp.Send(txSenderId.Text, txtRecipientId.Text, txtSubject.Text, txtMsgBody.Text );

    OR

    Smtp.Send(MailMsg);//Here MailMsg is the object of MailMessage Class

    MailMessage: we use MailMessage class to represent an email message and Sysrtem.net.mail namespace is used to include this class in our application. So Mailmessage class is used to send the emails using FileAttachment,Bcc,Cc,From,To,MessageBody.

    First we will create an instance of MailMessage class and use the properties as per our need ,Then we will pass that instance in the SEND method of SMTP protocol.

    Lets's create an application:

    Include the following code for the design part in your aspx page:

    <html xmlns="http://www.w3.org/1999/xhtml">
    <head id="Head1" runat="server">
    <title>Send Mail using with gmail crendentials in asp.net</title>
        <style type="text/css">
            .auto-style1 {
                width: 203px;
            }
            .auto-style2 {
                width: 203px;
                height: 27px;
            }
            .auto-style3 {
                height: 27px;
            }
        </style>
    </head>
    <body>
    <form id="form1" runat="server">
    <div>
    <table>
    
    <tr>
    <td style="background-color: #FFCCCC" class="auto-style1">
    Sender EmailId:
    </td>
    <td>
    <asp:TextBox ID="txSenderId" runat="server" Height="30px" Width="287px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="background-color: #FFFFCC" class="auto-style1">
    Sender Password:
    </td>
    <td>
    <asp:TextBox ID="txtSenderpwd" runat="server" TextMode="Password" Height="29px" Width="288px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="background-color: #FFCCCC" class="auto-style1">
    Subject:
    </td>
    <td>
    <asp:TextBox ID="txtSubject" runat="server" Height="30px" Width="288px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="background-color: #FFFFCC" class="auto-style1">
    Recipient EmailId:
    </td>
    <td>
    <asp:TextBox ID="txtRecipientId" runat="server" Height="32px" Width="288px"></asp:TextBox>
    </td>
    </tr>
    <tr>
    <td style="background-color: #FFFFCC" class="auto-style2">
         File Attachment:
    </td>
    <td class="auto-style3">
     <asp:FileUpload ID="fileAttachment" runat="server" Height="40px" Width="294px" />
    </td>
    </tr>
    <tr>
    <td valign="top" style="background-color: #FFCCCC" class="auto-style1" >
    Message Body:
    </td>
    <td>
    <asp:TextBox ID="txtMsgBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" Height="213px" Width="379px" ></asp:TextBox>
    </td>
    </tr>
     <tr>
    <td class="auto-style1">
    </td>
    <td>
    <asp:Button ID="btnSend" Text="Send" runat="server" onclick="btnSubmit_Click" Height="40px" Width="112px" />
    </td>
    </tr>
    </table>
    </div>
    </form>
    </body>
    </html>

    Refer to the below screenshot for the view:

    Include the following code on Send button click event in your aspx.cs page:

      protected void btnSend_Click(object sender, EventArgs e)
            {
     try
                {
                    MailMessage MailMsg = new MailMessage();
                   
                    MailMsg.From = new MailAddress(txSenderId.Text);
                    MailMsg.To.Add(txtRecipientId.Text);
                    MailMsg.Subject = txtSubject.Text;
                    MailMsg.Body = txtMsgBody.Text;
                    if (fileAttachment.HasFile) 
                    {
                        string FileName = Path.GetFileName(fileAttachment.PostedFile.FileName);
                        MailMsg.Attachments.Add(new Attachment(fileAttachment.PostedFile.InputStream, FileName));
                    }
                   
                    SmtpClient smtp = new SmtpClient();
                    smtp.Host = "smtp.gmail.com";
                    smtp.Port = 587;
                    smtp.Credentials = new System.Net.NetworkCredential(txSenderId.Text, txtSenderpwd.Text);
                    smtp.EnableSsl = true;
                    smtp.Send(MailMsg);
                    MailMsg = null;
                    ClientScript.RegisterStartupScript(GetType(), "Usermsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='GmailWebForm.aspx';}</script>");
                }
                catch (Exception ex)
                {
                    Console.WriteLine("{0} Exception caught.", ex);
                    
                }
            }
    

    Refer to the below screenshot for the Output:  you will get an alert message "Mail sent thank you".

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: