{"id":30127,"date":"2021-01-18T03:17:22","date_gmt":"2021-01-17T19:17:22","guid":{"rendered":"https:\/\/web.mwwsb.com.my\/pjci\/?post_type=kb&p=30127"},"modified":"2022-09-08T20:17:33","modified_gmt":"2022-09-08T12:17:33","slug":"how-to-send-out-emails-using-asp-script","status":"publish","type":"kb","link":"https:\/\/www.casbay.com\/guide\/kb\/how-to-send-out-emails-using-asp-script","title":{"rendered":"Send out emails using ASP script"},"content":{"rendered":"\t\t
In Windows 2008, we prefer all users to send out email using CDOSYS mail component. This mail component will integrate into your ASP coding scripts which uses the mail server to send out email instead of CDONTS which is using IIS Virtual SMTP Server.<\/p>
To send email messages using ASP.NET, your application must:<\/p>
This guide will shows the code samples for C# and VB.NET to demonstrate how to do this, and both are functionally equivalent to each other.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
Firstly, create an HTML page that contains a basic interface where the user can specify the message’s sender, recipient, subject, and body. To do this, copy and paste the following code into a file named mail.aspx<\/em>: Secondly, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named mail.aspx.cs<\/em>. Replace the following text values:<\/p> Now you can use your web browser to load the\u00a0mail.aspx<\/em>\u00a0file. Complete the fields, click\u00a0Send message<\/b>, and if the values are valid, the server should send the message.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t Firstly, create an HTML page that contains a basic interface where the user can specify the message’s sender, recipient, subject, and body. To do this, copy and paste the following code into a file named mail.aspx<\/em>:<\/p> Second, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named\u00a0mail.aspx.vb<\/em>. Replace the following text values:<\/p> Now you can use your web browser to load the\u00a0mail.aspx<\/em>\u00a0file. Complete the fields, click\u00a0Send message<\/b>, and if the values are valid, the server should send the message.<\/p>\t\t\t\t\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
Secondly, create the code-behind page for the HTML page. To do this, copy and paste the following code into a file named mail.aspx.cs<\/em>. Replace the following text values:<\/p>EventWireup=\"true\" CodeFile=\"mail.aspx.cs\" Inherits=\"SendMail\" %>\n<html>\n<head id=\"Head1\" runat=\"server\"><title>E-mail test page<\/title><\/head>\n<body>\n <form id=\"form1\" runat=\"server\">\n Message sender: <asp:textbox id=\"txtFrom\" runat=\"server\" \/><br>\n Message recipient: <asp:textbox id=\"txtTo\" runat=\"server\" \/><br>\n Message subject: <asp:textbox id=\"txtSubject\" runat=\"server\" \/><br>\n Message body:<br\/>\n <asp:textbox id=\"txtBody\" runat=\"server\" height=\"150px\" textmode=\"multiline\" \/><br>\n <asp:button id=\"btn_SendMessage\" runat=\"server\" onclick=\"btn_SendMessage_Click\" text=\"Send message\" \/><br>\n <asp:label id=\"Label1\" runat=\"server\" text=\"\" \/>\n <\/form>\n<\/body>\n<\/html><\/pre>
using System;\nusing System.Web.UI.WebControls;\nusing System.Net.Mail;\n\npublic partial class SendMail : System.Web.UI.Page\n{\n protected void btn_SendMessage_Click(object sender, EventArgs e)\n {\n SmtpClient smtpClient = new SmtpClient(\"domain.example.com<\/em><\/span>\", 25);\n\n smtpClient.Credentials = new System.Net.NetworkCredential(\"user@example.com<\/em><\/span>\", \"password<\/em><\/span>\");\n smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;\n\n MailMessage mailMessage = new MailMessage(txtFrom.Text, txtTo.Text);\n mailMessage.Subject = txtSubject.Text;\n mailMessage.Body = txtBody.Text;\n\n try\n {\n smtpClient.Send(mailMessage);\n Label1.Text = \"Message sent\";\n }\n catch (Exception ex)\n {\n Label1.Text = ex.ToString();\n }\n }\n}\n<\/pre>VB.NET example<\/h3>\t\t<\/div>\n\t\t\t\t<\/div>\n\t\t\t\t
<%@ Page Language=\"VB\" AutoEventWireup=\"true\" CodeFile=\"mail.aspx.vb\" Inherits=\"SendMail\" %>\n<html>\n<head id=\"Head1\" runat=\"server\"><title>E-mail test page<\/title><\/head>\n<body>\n <form id=\"form1\" runat=\"server\">\n Message sender: <asp:textbox id=\"txtFrom\" runat=\"server\" \/><br>\n Message recipient: <asp:textbox id=\"txtTo\" runat=\"server\" \/><br>\n Message subject: <asp:textbox id=\"txtSubject\" runat=\"server\" \/><br>\n Message body:<br\/>\n <asp:textbox id=\"txtBody\" runat=\"server\" height=\"150px\" textmode=\"multiline\" \/><br>\n <asp:button id=\"btn_SendMessage\" runat=\"server\" onclick=\"btn_SendMessage_Click\" text=\"Send message\" \/><br>\n <asp:label id=\"Label1\" runat=\"server\" text=\"\" \/>\n <\/form>\n<\/body>\n<\/html><\/pre>
Imports System\nImports System.Web.UI.WebControls\nImports System.Net.Mail\n\nPublic Class SendMail\n Inherits System.Web.UI.Page\n \n Protected Sub btn_SendMessage_Click(ByVal sender As Object, ByVal e As EventArgs)\n\n Dim smtpClient As SmtpClient = New SmtpClient(\"domain.example.com<\/em><\/span>\", 25)\n\n smtpClient.Credentials = New System.Net.NetworkCredential(\"user@example.com<\/em><\/span>\", \"password<\/em><\/span>\")\n smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network\n\n Dim mailMessage As MailMessage = New MailMessage(txtFrom.Text, txtTo.Text)\n mailMessage.Subject = txtSubject.Text\n mailMessage.Body = txtBody.Text\n\n Try \n smtpClient.Send(mailMessage)\n Label1.Text = \"Message sent\"\n Catch ex As Exception\n Label1.Text = ex.ToString\n End Try\n\n End Sub\nEnd Class\n<\/pre>