To implement mail concept in your asp.net application first we need to add this reference to our application System.Web.Mail namespace
What is System.Web.Mail
The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.
How we can get this reference (System.Web.Mail) for that we need to add reference to our application.
After that design your aspx page like this
using System.Web.Mail;
After that write the following code in button click
Demo
I hope it helps you
What is System.Web.Mail
The System.Web.Mail namespace contains classes that enable you to construct and send messages using the CDOSYS (Collaboration Data Objects for Windows 2000) message component. The mail message is delivered either through the SMTP mail service built into Microsoft Windows 2000 or through an arbitrary SMTP server.
How we can get this reference (System.Web.Mail) for that we need to add reference to our application.
a) On the Project menu, click Add Reference.
b) On the .NET tab, locate System.Web.dll, and then click Select.
c) Click OK in the Add References.
<html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Send Mail using asp.net</title> </head> <body> <form id="form1" runat="server"> <div> <table style=" border:1px solid" align="center"> <tr> <td colspan="2" align="center"> <b>Send Mail with Attachment using asp.net</b> </td> </tr> <tr> <td> From: </td> <td> <asp:TextBox ID="txtFrom" runat="server"></asp:TextBox> </td> </tr> <tr> <td> To: </td> <td> <asp:TextBox ID="txtTo" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Subject: </td> <td> <asp:TextBox ID="txtSubject" runat="server"></asp:TextBox> </td> </tr> <tr> <td> Attach a file: </td> <td> <asp:FileUpload ID="fileUpload1" runat="server" /> </td> </tr> <tr> <td valign="top"> Body: </td> <td> <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Columns="30" Rows="10" ></asp:TextBox> </td> </tr> <tr> <td> </td> <td> <asp:Button ID="btnSubmit" Text="Send" runat="server" onclick="btnSubmit_Click" /> </td> </tr> </table> </div> </form> </body> </html> |
After that add this namcespace in your codebehind
using System.Web.Mail;
protected void btnSubmit_Click(object sender, EventArgs e) { try { MailMessage Msg = new MailMessage(); // Sender e-mail address. Msg.From = txtFrom.Text; // Recipient e-mail address. Msg.To = txtTo.Text; // Subject of e-mail Msg.Subject = txtSubject.Text; if (fileUpload1.HasFile) { // File Upload path String FileName = fileUpload1.PostedFile.FileName; //Getting Attachment file MailAttachment mailAttachment = new MailAttachment(FileName, MailEncoding.Base64); //Attaching uploaded file Msg.Attachments.Add(mailAttachment); } Msg.Body = txtBody.Text; // your remote SMTP server IP. SmtpMail.SmtpServer = "10.120.0.21"; SmtpMail.Send(Msg); Msg = null; Page.RegisterStartupScript("UserMsg", "<script>alert('Mail sent thank you...');if(alert){ window.location='SendMailWithAttachment.aspx';}</script>"); } catch (Exception ex) { Console.WriteLine("{0} Exception caught.", ex); } } } |
I hope it helps you
No comments:
Post a Comment