博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
简单的网页发邮件例子
阅读量:5278 次
发布时间:2019-06-14

本文共 3123 字,大约阅读时间需要 10 分钟。

 

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

 

前端:

                
smtp:
from addr:
to addr:
title:
content:
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
d = new System.Collections.Generic.Dictionary
(); 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
d = new System.Collections.Generic.Dictionary
(); 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

你可能感兴趣的文章
bzoj 2257 (JSOI 2009) 瓶子与燃料
查看>>
11)Java abstract class 和 interface
查看>>
使用xrdp或Xmanager 远程连接 CentOS6
查看>>
Linux误删恢复
查看>>
Unity调用Windows窗口句柄,选择文件和目录
查看>>
HashMap循环遍历方式
查看>>
React Native 入门 调试项目
查看>>
C# 通过 Quartz .NET 实现 schedule job 的处理
查看>>
关于java之socket输入流输出流可否放在不同的线程里进行处理
查看>>
目前为止用过的最好的Json互转工具类ConvertJson
查看>>
Day13
查看>>
tensorflow saver简介+Demo with linear-model
查看>>
Luogu_4103 [HEOI2014]大工程
查看>>
Oracle——SQL基础
查看>>
项目置顶随笔
查看>>
Redis的安装与使用
查看>>
P1970 花匠
查看>>
java语言与java技术
查看>>
NOIP2016提高A组五校联考2总结
查看>>
iOS 项目的编译速度提高
查看>>