当前位置:网站首页>asp. Net method of sending mail using mailmessage
asp. Net method of sending mail using mailmessage
2022-04-23 15:03:00 【Rv1989】
1. newly build web forms Email.aspx, Add... To the design page table Control , Set up table There are six rows and two columns , And add corresponding controls , The overall design is shown in the figure :( Software :Vs2017)
Control name and ID as follows :
Before writing background code, you need to know MailMessage Class :
From: Sender's email address .
To: The recipient's email address .
CC: E-mail address of CC .
Subject: Email title .
Body: Email content .
Attachments: Email attachment
Besides MailMessage You need to use SmtpClient Class to send mail .SmtpClient Class means : Allow applications to use the Simple Mail Transfer Protocol (SMTP) To send e-mail .SmtpClient Some properties and methods commonly used by class :
(SmtpClient class (System.Net.Mail) | Microsoft Docs)
1、Host: Gets or sets the SMTP The name of the host of the transaction or IP Address .
2、EnableSsl: Whether to use secure socket layer to encrypt the connection .
3、UseDefaultCredentials: Whether to send with the request .
4、Credentials: Gets or sets the credentials used to authenticate the sender .
5、Send(): send out .
Need additional knowledge MailMessage.Fields attribute :
effect : Get a map to Microsoft Collaborative data objects (CDO) A collection of objects for fields .
(MailMessage.Fields attribute (System.Web.Mail) | Microsoft Docs)
In a nutshell MailMessage.Fields Property to send the user name and password to the simple mail transfer protocol , (SMTP) The server authenticates . We can use this method to send email :
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "( Your username )");
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "( password )");
2. below Email.aspx.cs The code takes Netease email as an example , Send email :
1. Namespace... Needs to be introduced :
using System.IO;// Upload attachments
using System.Web.Mail; // Send E-mail
2. by Button1 add to Button1_Click event :
protected void Button1_Click(object sender, EventArgs e)
{
MailMessage objMail = new MailMessage();// Instantiate a mail class 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]");// Personal email
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "( Authorization code )");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// First upload the files from the client to the server
string filename, filepath;
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
filepath = Server.MapPath("./") + filename;
FileUpload1.PostedFile.SaveAs(filepath);
// establish MailAttachment object , Add attachments
MailAttachment objAttach = new MailAttachment(filepath);
objMail.Attachments.Add(objAttach); // Add attachments
SmtpMail.SmtpServer = "smtp.163.com";// The address of the server corresponding to the email
SmtpMail.Send(objMail);// Execute the send operation
}
3. Acquisition of authorization code :
The function of authorization code here is equivalent to password , After opening, you can use foxmail、Outlook Wait for a third-party client to send and receive mail .
Log in to Netease mailbox --> Find settings -->POP3/SMTP/IMAP--> Follow the prompts to turn on IMAP/SMTP service --> Get authorization code
The overall code is as follows :
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();// Instantiate a mail class objMail
objMail.From = mailFrom.Text;//objMail.From Indicates the original email address That is, the sender's email
objMail.To = mailTo.Text;//objMail.To Indicates the recipient address E-mail address of the recipient
objMail.Subject = Subject.Text;//objMail.Subject Indicates the subject of the message That is, the title of the message
objMail.Body = Body.Text;//objMail.Body That is, the content of the email
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "( Email account )");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "( Authorization code )");
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);
objMail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");
// First upload the files from the client to the server
string filename, filepath;
filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
filepath = Server.MapPath("./") + filename;
FileUpload1.PostedFile.SaveAs(filepath);
// establish MailAttachment object , Add attachments
MailAttachment objAttach = new MailAttachment(filepath);
objMail.Attachments.Add(objAttach); // Add attachments
SmtpMail.SmtpServer = "smtp.163.com";// The address of the server corresponding to the email
SmtpMail.Send(objMail);// Execute the send operation
}
}
}
Run a screenshot :
The above is ASP.NET web The page realizes sending mail , I hope it helped you !!
版权声明
本文为[Rv1989]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204231411010865.html
边栏推荐
- Leetcode149 - maximum number of points on a line - Math - hash table
- Redis master-slave synchronization
- Tencent has written a few words, Ali has written them all for a month
- 如何设计一个良好的API接口?
- The art of automation
- Alexnet model
- Async keyword
- 22年了你还不知道文件包含漏洞?
- 买卖股票的最佳时机系列问题
- [NLP] HMM hidden Markov + Viterbi word segmentation
猜你喜欢
The art of automation
Vous ne connaissez pas encore les scénarios d'utilisation du modèle de chaîne de responsabilité?
How to upload large files quickly?
1 - first knowledge of go language
冰冰学习笔记:一步一步带你实现顺序表
1n5408-asemi rectifier diode
What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
What is the role of the full connection layer?
Borui data and F5 jointly build the full data chain DNA of financial technology from code to user
Mds55-16-asemi rectifier module mds55-16
随机推荐
Resolve the conflict between computed attribute and input blur event
Thread synchronization, life cycle
OC 转 Swift 条件编译、标记、宏、 Log、 版本检测、过期提示
分享3个使用工具,在家剪辑5个作品挣了400多
I/O复用的高级应用之一:非阻塞 connect———使用 select 实现(也可以用 poll 实现)
免费在upic中设置OneDrive或Google Drive作为图床
Set up an AI team in the game world and start the super parametric multi-agent "chaos fight"
Leetcode exercise - 396 Rotation function
Unity_ Code mode add binding button click event
如何打开Win10启动文件夹?
科技的成就(二十一)
8.3 language model and data set
编程哲学——自动加载、依赖注入与控制反转
Alexnet model
OPPO数据湖统一存储技术实践
Achievements in science and Technology (21)
Go basic reflection
Introduction to Arduino for esp8266 serial port function
Nuxt project: Global get process Env information
2-Go变量操作