发送邮件
1.制作客户端邮件发送系统(winform版)
前台:
后台
private void button1_Click(object sender, EventArgs e)
{
MailMessage msg = new MailMessage();
msg.Subject = this.textBox2.Text;
msg.Body = this.textBox3.Text;
msg.From = new MailAddress("1059625015@qq.com");
msg.To.Add(textBox1.Text);
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com"; //发件方服务器地止
client.Port = 25; //发件方端口
NetworkCredential credential = new NetworkCredential();
credential.UserName = "1059625015";
credential.Password = "57483985shang146";
client.Credentials = credential; //把证书交给代理
Attachment att = new Attachment(this.textBox4.Text);
msg.Attachments.Add(att);
client.Send(msg);
}
private void button2_Click(object sender, EventArgs e)
{
if (this.openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.textBox4.Text = this.openFileDialog1.FileName;
}
}
2.实现用户注册时,向其油箱发送激活码邮件,并进行状态处理。
login前台:
<div>
<table style="width:100%;">
<tr>
<td>
用户名:</td>
<td>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
密码:</td>
<td>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Email:</td>
<td>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="注册" />
</td>
<td>
</td>
</tr>
</table>
</div>
login后台:
protected void Button1_Click(object sender, EventArgs e)
{
string userName = TextBox1.Text.Trim();
string pwd = TextBox2.Text.Trim();
string email = TextBox3.Text.Trim();
string activeCode = Guid.NewGuid().ToString().Substring(0,6);
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "insert into T_Eamil (FUserName,FPassword,FEmail,FActive,FActiveCode) values
(@name,@pwd,@email,@active,@activeCode) select @@IDENTITY";
cmd.Parameters.Add(new SqlParameter("name", userName));
cmd.Parameters.Add(new SqlParameter("pwd",pwd));
cmd.Parameters.Add(new SqlParameter("email", email));
cmd.Parameters.Add(new SqlParameter("active", false));
cmd.Parameters.Add(new SqlParameter("activeCode", activeCode));
int num=Convert.ToInt32(cmd.ExecuteScalar());
sendMail(email, activeCode, num);
Response.Redirect("loginMessage.aspx");
conn.Close();
conn.Dispose();
}
protected void sendMail(string email,string activeCode,int id)
{
MailMessage msg = new MailMessage();
msg.From = new MailAddress("1059625015@qq.com");
msg.To.Add(email);
msg.Subject = "请激活注册";
StringBuilder contentBuilder = new StringBuilder();
contentBuilder.Append("请单击以下连接完成激活!");
contentBuilder.Append("<a href='http://localhost:2533/index.aspx?id=" + id.ToString() + "&activeCode="
+ activeCode + "'>激活</a>");
msg.Body = contentBuilder.ToString();
msg.IsBodyHtml = true;
SmtpClient client = new SmtpClient();
client.Host = "smtp.qq.com";
client.Port = 25;
NetworkCredential credential = new NetworkCredential();
credential.UserName = "1059625015";
credential.Password = "57483985shang146";
client.Credentials = credential;
client.Send(msg);
}
loginMessage前台
<div>
恭喜您注册成功。。。。。。。。。。。。。
</div>
index后台
protected void Page_Load(object sender, EventArgs e)
{
int id = Convert.ToInt32(Request["id"]);
string activeCode = Request["activeCode"].ToString();
int num = 0;
string conStr = ConfigurationManager.ConnectionStrings["conStr"].ConnectionString;
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from T_Eamil where Id=@id";
cmd.Parameters.Add(new SqlParameter("id",id));
SqlDataReader read=cmd.ExecuteReader();
if (read.Read())
{
if (read.GetString(read.GetOrdinal("FActiveCode")) == activeCode)
{
num = 1;
}
else
{
num = 0;
Response.Write("验证码不正确");
}
}
else
{
Response.Write("用户不存在!!");
}
conn.Close();
conn.Dispose();
if (num == 1)
{
SqlConnection conn1 = new SqlConnection(conStr);
conn1.Open();
SqlCommand cmd1 = conn1.CreateCommand();
cmd1.CommandText = "update T_Eamil set FActive='True' where Id=@id1";
cmd1.Parameters.Add(new SqlParameter("id1", id));
cmd1.ExecuteNonQuery();
conn1.Close();
conn1.Dispose();
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构