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 );
}
}