当前位置:网站首页>asp.net使用MailMessage发送邮件的方法
asp.net使用MailMessage发送邮件的方法
2022-04-23 14:11:00 【Rv1989】
1. 新建web窗体Email.aspx,在设计页面中添加table控件,设置table为六行两列,并添加相应的控件,整体设计如图所示:(软件:Vs2017)
控件名称及ID如下:
书写后台代码之前需要先了解MailMessage类中的各个属性:
From:发件人邮箱地址。
To:收件人的邮箱地址。
CC:抄送人邮箱地址。
Subject:邮件标题。
Body:邮件内容。
Attachments:邮件附件
此外MailMessage还需要用到SmtpClient类进行发送邮件。SmtpClient类的意思是:允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。SmtpClient类常用的一些属性和方法:
(SmtpClient 类 (System.Net.Mail) | Microsoft Docs)
1、Host: 获取或设置用于 SMTP 事务的主机的名称或 IP 地址。
2、EnableSsl:是否使用安全套接字层加密连接。
3、UseDefaultCredentials:是否随请求一起发送。
4、Credentials:获取或设置用于验证发件人身份的凭证。
5、Send():发送。
需要额外了解MailMessage.Fields属性:
作用:获取一个映射到 Microsoft 协作数据对象 (CDO) 字段的对象的集合。
(MailMessage.Fields 属性 (System.Web.Mail) | Microsoft Docs)
简单来说就是MailMessage.Fields属性可以将用户名和密码发送到简单邮件传输协议, (SMTP) 服务器进行身份验证。我们想要实现发送邮箱可以用到这个方法:
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "(你的用户名)");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "(密码)");
2.下面 Email.aspx.cs代码以网易邮箱为例,实现发送邮件:
1.需要引入命名空间:
using System.IO;//上传附件
using System.Web.Mail; //发送邮件
2.为Button1 添加 Button1_Click事件:
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMail = new MailMessage();//实例化一个邮件类objMail
objMail.From = mailFrom.Text;
objMail.To = mailTo.Text;
objMail.Subject = Subject.Text;
objMail.Body = Body.Text;
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "[email protected]");//个人邮箱
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "(授权码)");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
//先将客户端的文件上传到服务器端
string filename, filepath;
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
filepath = Server.MapPath("./") + filename;
FileUpload1.PostedFile.SaveAs(filepath);
//创建MailAttachment对象,添加附件
MailAttachment objAttach = new MailAttachment(filepath);
objMail.Attachments.Add(objAttach); //添加附件
SmtpMail.SmtpServer = "smtp.163.com";//对应邮箱发送邮件的服务器地址
SmtpMail.Send(objMail);//执行发送操作
}
3.授权码的获取:
授权码的在这里的作用就相当于密码,开通后即可使用foxmail、Outlook等第三方客户端进行收发邮件。
登录网易邮箱-->找到设置-->POP3/SMTP/IMAP-->按照提示开启IMAP/SMTP服务-->获取授权码
整体代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Web.Mail;
namespace course1.Login
{
public partial class Email : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMail = new MailMessage();//实例化一个邮件类objMail
objMail.From = mailFrom.Text;//objMail.From表示原邮件地址 即发送人的邮箱
objMail.To = mailTo.Text;//objMail.To 表示收件人地址 即收件人邮箱
objMail.Subject = Subject.Text;//objMail.Subject 表示邮件主题 即邮件的标题
objMail.Body = Body.Text;//objMail.Body 即邮件的内容
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "(邮箱账号)");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "(授权码)");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
//先将客户端的文件上传到服务器端
string filename, filepath;
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
filepath = Server.MapPath("./") + filename;
FileUpload1.PostedFile.SaveAs(filepath);
//创建MailAttachment对象,添加附件
MailAttachment objAttach = new MailAttachment(filepath);
objMail.Attachments.Add(objAttach); //添加附件
SmtpMail.SmtpServer = "smtp.163.com";//对应邮箱发送邮件的服务器地址
SmtpMail.Send(objMail);//执行发送操作
}
}
}
运行截图:
以上介绍的就是ASP.NET web页面实现发送邮件,希望对你有所帮助!!
版权声明
本文为[Rv1989]所创,转载请带上原文链接,感谢
https://blog.csdn.net/m0_64355496/article/details/124339171
边栏推荐
- Storage path of mod subscribed by starbound Creative Workshop at Star boundary
- setcontext getcontext makecontext swapcontext
- JS parabola motion packaging method
- Operation instructions of star boundary text automatic translator
- 01-nio basic ByteBuffer and filechannel
- 线程间控制之CountDownLatch和CyclicBarrier使用介绍
- 在电视屏幕上进行debug调试
- Get the thread return value. Introduction to the use of future interface and futuretask class
- 错误:无法远程查找到密钥 “428F7ECC7117F726“
- openstack理论知识
猜你喜欢
Nacos作为配置中心(四) 使用Demo
什么是云迁移?云迁移的四种模式分别是?
Some experience of using dialogfragment and anti stepping pit experience (getactivity and getdialog are empty, cancelable is invalid, etc.)
MySQL-InnoDB-事务
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
Recyclerview advanced use (I) - simple implementation of sideslip deletion
剑指offer刷题(1)--面向华为
Use the executors class to quickly create a thread pool
TLS/SSL 协议详解 (30) SSL中的RSA、DHE、ECDHE、ECDH流程与区别
Logback logger and root
随机推荐
Operation instructions of star boundary automatic text translator (advanced version)
Nacos作为配置中心(四) 使用Demo
KVM learning resources
線程組ThreadGroup使用介紹+自定義線程工廠類實現ThreadFactory接口
Notes on Visio drawing topology
js 格式化时间
MYSQL一种分表实现方案及InnoDB、MyISAM、MRG_MYISAM等各种引擎应用场景介绍
获取线程返回值Future接口与FutureTask类使用介绍
js 递归(1)
Redis cluster 原理
tcp_diag 内核相关实现 1 调用层次
Tongxin UOS php7 2.3 upgrade to php7.0 two point two four
js 键值判断
redis数据库讲解(三)redis数据类型
MySQL数据库讲解(十)
Introduction to loan market quotation interest rate (LPR) and loan benchmark interest rate
Installation and use of postman pit
剑指offer刷题(2)--面向华为
差分隐私(背景介绍)
Preview CSV file