当前位置:网站首页>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", "XXXXX@163.com");// 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
边栏推荐
- How to write the keywords in the cover and title? As we media, why is there no video playback
- PCIe X1 插槽的主要用途是什么?
- Explanation and example application of the principle of logistic regression in machine learning
- Provided by Chengdu control panel design_ It's detailed_ Introduction to the definition, compilation and quotation of single chip microcomputer program header file
- QT Detailed explanation of pro file
- January 1, 1990 is Monday. Define the function date_ to_ Week (year, month, day), which realizes the function of returning the day of the week after inputting the year, month and day, such as date_ to
- What is the effect of Zhongfu Jinshi wealth class 29800? Walk with professional investors to make investment easier
- When splicing HQL, the new field does not appear in the construction method
- 22年了你还不知道文件包含漏洞?
- Detailed analysis of SQL combat of Niuke database (26-30)
猜你喜欢
For 22 years, you didn't know the file contained vulnerabilities?
LeetCode165-比较版本号-双指针-字符串
22年了你还不知道文件包含漏洞?
LeetCode153-寻找旋转排序数组中的最小值-数组-二分查找
Redis主从同步
LeetCode151-颠倒字符串中的单词-字符串-模拟
Don't you know the usage scenario of the responsibility chain model?
LeetCode149-直线上最多的点数-数学-哈希表
Interviewer: let's talk about the process of class loading and the mechanism of class loading (parental delegation mechanism)
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
随机推荐
买卖股票的最佳时机系列问题
Share 3 tools, edit 5 works at home and earn more than 400
UML learning_ Day2
编程哲学——自动加载、依赖注入与控制反转
The difference between having and where in SQL
js——实现点击复制功能
3、 Gradient descent solution θ
go基础 反射
Unity_ Code mode add binding button click event
js——實現點擊複制功能
Leetcode167 - sum of two numbers II - double pointer - bisection - array - Search
1-初识Go语言
博睿数据携手F5共同构建金融科技从代码到用户的全数据链DNA
Set onedrive or Google drive as a drawing bed in upic for free
PCIe X1 插槽的主要用途是什么?
Brute force of DVWA low -- > High
For 22 years, you didn't know the file contained vulnerabilities?
22年了你还不知道文件包含漏洞?
解决computed属性与input的blur事件冲突问题
OPPO数据湖统一存储技术实践