Simple Web mail example and mail example

Source: Internet
Author: User
Tags mail example mailmessage smtpclient

Simple Web mail example and mail example
Front end:

<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Title> </title>
<Script src = "jquery-1.8.0.min.js" type = "text/javascript"> </script>
<Script type = "text/javascript">
Function sendemail (){
Var smtp = $ ('# txtSmtp'). val ();
Var content = $ ('# txtContent'). val ();
Var title = $ ('# txtTitle'). val ();
Var from = $ ('# txtFrom'). val ();
Var to = $ ('# txtTo'). val ();
$. Post ("Handler. ashx ", {'smtp ': smtp, 'content': content, 'title': title, 'from': from, 'to': },
Function (data ){
Var n = eval ('+ data + ')');
If (n. success ){
Alert (n. message );
}
});

}
</Script>
</Head>
<Body>
<Table>
<Tr> <td> smtp: </td>
<Td> <input type = "text" id = "txtSmtp" value = "Smtp Server"/>
</Td>
</Tr>

<Tr> <td> from addr: </td>
<Td> <input type = "text" id = "txtFrom" value = "xxx@xxx.com"/>
</Td>
</Tr>

<Tr> <td> to addr: </td>
<Td> <input type = "text" id = "txtTo" value = "xxx@xxx.com"/>
</Td>
</Tr>

<Tr> <td> title: </td>
<Td> <input type = "text" id = "txtTitle" value = "title"/>
</Td>
</Tr>

<Tr> <td> content: </td>
<Td> <input type = "text" id = "txtContent" value = "Content"/>

</Td>
</Tr>
<Tr>
<Td>
<Input type = "button" id = "btnSend" value = "send" onclick = "sendemail ()"/>
</Td>
</Tr>
</Table>
</Body>
</Html>

Background: ashx

 

<% @ WebHandler Language = "C #" class = "Handler" %>

Using System;
Using System. Web;
Using Utility;
Public class Handler: IHttpHandler {

Public void ProcessRequest (HttpContext context)
{
Context. Response. ContentType = "text/plain ";
String smtp = HttpContext. Current. Request. Form ["smtp"]. ToString ();
String title = HttpContext. Current. Request. Form ["title"]. ToString ();
String content = HttpContext. Current. Request. Form ["content"]. ToString ();
String from = HttpContext. Current. Request. Form ["from"]. ToString ();
String to = HttpContext. Current. Request. Form ["to"]. ToString ();


Try
{
EmailClient emailClient = new EmailClient (smtp); // localhost: 25
EmailClient. SendEmail (from, to, title, content );
System. Web. Script. Serialization. JavaScriptSerializer jss = new System. Web. Script. Serialization. JavaScriptSerializer ();
System. Collections. Generic. Dictionary <string, object> d = new System. Collections. Generic. Dictionary <string, object> ();
D. Add ("message", "success ");
D. Add ("success", true );
Context. Response. Write (jss. Serialize (d ));
}
Catch (Exception ex)
{
System. Web. Script. Serialization. JavaScriptSerializer jss = new System. Web. Script. Serialization. JavaScriptSerializer ();
System. Collections. Generic. Dictionary <string, object> d = new System. Collections. Generic. Dictionary <string, object> ();
D. Add ("message", ex. Message );
D. Add ("success", true );
Context. Response. Write (jss. Serialize (d ));
}


}
 
Public bool IsReusable {
Get {
Return false;
}
}

}

 

Smtp class:

Public class EmailClient
{
Private string smtpServer;
Private string senderAddress;


Public EmailClient (string smtpServer)
{
This. smtpServer = smtpServer;
This. senderAddress = string. Empty;
}

Public void SendEmail (string fromAddress, string toAddress, string subject, string messageBody)
{
SmtpClient smtp = new SmtpClient (smtpServer );

MailMessage email = new MailMessage ();

Email. From = new MailAddress (fromAddress );
Email. To. Add (toAddress );
Email. Subject = subject;
Email. Body = messageBody;

Smtp. Send (email );
}

}

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.