<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="qq邮件.WebForm1" Async="true" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <title></title> </head> <body> <form id="form1" runat="server"> <div style="100%"> <h1>请输入您的邮箱</h1> <table style="100%"><tr><td> <asp:TextBox ID="EmailText" runat="server" Width="100%" Height="50"></asp:TextBox></td></tr><tr><td> <asp:Button ID="Button1" runat="server" Text="提交" OnClick="Button1_Click" Width="100%" /></td></tr></table> </div> </form> </body> </html>
前端代码,Async="true"
为异步处理需要配置的。从控件拉一个文本框和按钮,前端完成。
protected void Button1_Click(object sender, EventArgs e) { string title = ""; string text = ""; if (EmailText.Text == "2233298547@qq.com") { title = "你好哇,zss"; text = "我可能有点喜欢你了"; } else if (EmailText.Text == "p1056722912@qq.com") { title = "飞儿子,你好"; text = "我是你爸爸"; } else { Random rd = new Random(); int i = rd.Next(1, 4);//(生成1~4之间的随机数,不包括4) if (i == 1) { title = "曾经沧海难为水,除却巫山不是云"; text = "爱情是具有排他性的,曾经看过沧海那么再看天底下的水便都变得平常了,看过巫山上的云,其他地方的云彩也不能称为云了。爱过一个人便如同弱水三千只取一瓢饮,意味着以后遇到再优秀的人也不能动心。"; } else if (i == 2) { title = "《德川家康》"; text = "人生有三大诱惑:少年时贪玩,荒废了学业没打下扎实的基础;青年时贪情,在朋友和情人面前迷失了自我;中年时贪功,躺在功劳簿上骄傲自满地犯懒。"; } else if (i == 3) { title = "宝贝"; text = "我不打算去见我超级想见的人了,我在等那个超级想见我的人。"; } } SenMailByQQemail(EmailText.Text, text, title); }
我在按钮里定义了,发生邮件的title标题,和text内容。用于随机发送邮件。将定义好的信息发送到seMailByQQemail类。(接收方邮箱,内容,标题)。
public static bool SenMailByQQemail(string to, string body, string title) { bool retrunBool = false; MailMessage mail = new MailMessage(); SmtpClient smtp = new SmtpClient(); smtp.EnableSsl = true; //设置加密连接 smtp.UseDefaultCredentials = false; string strFromEmail = "779959311@qq.com";//你的邮箱 string strEmailPassword = "12456";//QQPOP3/SMTP授权码 try { mail.From = new MailAddress("sks<" + strFromEmail + ">"); mail.To.Add(new MailAddress(to)); mail.BodyEncoding = Encoding.UTF8;//文章编码 mail.IsBodyHtml = true;//文章是否html格式 mail.SubjectEncoding = Encoding.UTF8; mail.Priority = MailPriority.Normal; mail.Body = body; mail.Subject = title; smtp.Host = "smtp.qq.com"; smtp.Port = 587;//由于aliyun限制了25端口号,所以只能使用ssl协议587端口 smtp.DeliveryMethod = SmtpDeliveryMethod.Network; smtp.Credentials = new System.Net.NetworkCredential(strFromEmail, strEmailPassword); //发送邮件 smtp.Send(mail); //同步发送 retrunBool = true; } catch (Exception ex) { retrunBool = false; } // smtp.SendAsync(mail, mail.To); //异步发送 (异步发送时页面上要加上Async="true" ) return retrunBool; }