简单的网页发邮件例子

 

注意(要确定服务器的SMTP Server 的端口号)

 

前端:

<!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': 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>
View Code

 

后台: 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;
        }
    }

}
View Code

 

 

 

Smtp类:

 

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

}
View Code

 

转载于:https://www.cnblogs.com/caitouge/p/5306295.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值