ASP.NET Tutorial/Development/Email

Материал из .Net Framework эксперт
Версия от 14:56, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

Send an email out (VB.net)

   <source lang="csharp">

<%@ Page Language="VB" %> <%@ Import Namespace="System.Net.Mail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   Sub Page_Load()
       Dim client As New SmtpClient()
       client.Host = "localhost"
       client.Port = 25
       client.Send("a@a", "b@b.ru", "subject", "message body")
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Send Mail</title>

</head> <body>

   <form id="form1" runat="server">
   Email sent!
       
   </form>

</body> </html></source>


Send email from your page (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <%@ Import Namespace="System.Net.Mail" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   void Page_Load()
   {
       SmtpClient client = new SmtpClient();
       client.Host = "localhost";
       client.Port = 25;
       client.Send("a@a.ru", "s@s.ru","Subject", "Email body");
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head id="Head1" runat="server">

   <title>Send Mail</title>

</head> <body>

   <form id="form1" runat="server">
   Email sent!
   </form>

</body> </html></source>


Sending mail from a Web page (C#)

   <source lang="csharp">

<%@ Page Language="C#" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   protected void Page_Load(object sender, EventArgs e)
   {
       System.Net.Mail.MailMessage message =
           new System.Net.Mail.MailMessage("webmaster@a.org", "webmaster@a.org");
       message.Subject = "Sending Mail with ASP.NET 2.0";
       message.Body =
           "This is a sample email which demonstrates sending email using ASP.NET 2.0";
       System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("localhost");
       smtp.Send(message);
   }

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>


Sending mail from a Web page (VB)

   <source lang="csharp">

<%@ Page Language="VB" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server">

   Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
       Dim message As New System.Net.Mail.MailMessage("webmaster@a.org", "webmaster@a.org")
       message.Subject = "Sending Mail with ASP.NET 2.0"
       message.Body = "sending email"
       Dim smtp As New System.Net.Mail.SmtpClient("localhost")
       smtp.Send(message)
   End Sub

</script> <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server">

   <title>Untitled Page</title>

</head> <body>

   <form id="form1" runat="server">
   </form>

</body> </html></source>


The MailDefinition class uses the email server configured by the smtp element in the web configuration file.

   <source lang="csharp">

File: Web.Config <configuration>

 <system.net>
   <mailSettings>
     <smtp deliveryMethod="PickupDirectoryFromIis"/>
   </mailSettings>
 </system.net>
 <system.web>
   <authentication mode="Forms" />
 </system.web>

</configuration>

To connect to a mail server located on another machine File: Web.Config <configuration>

 <system.net>
   <mailSettings>
     <smtp>
       <network
           host="mail.YourServer.ru"
           userName="admin"
           password="secret" />
     </smtp>
   </mailSettings>
 </system.net>
 <system.web>
   <authentication mode="Forms" />
 </system.web>

</configuration></source>