当前位置:网站首页>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
边栏推荐
- UML learning_ Day2
- Swift:Entry of program、Swift调用OC、@_silgen_name 、 OC 调用Swift、dynamic、String、Substring
- like和regexp差别
- 如何打开Win10启动文件夹?
- 冰冰学习笔记:一步一步带你实现顺序表
- Set onedrive or Google drive as a drawing bed in upic for free
- nuxt项目:全局获取process.env信息
- The art of automation
- Nuxt project: Global get process Env information
- MySQL error packet out of order
猜你喜欢
Detailed comparison between asemi three-phase rectifier bridge and single-phase rectifier bridge
Sword finger offer II 019 Delete at most one character to get palindrome (simple)
Nuxt project: Global get process Env information
Redis master-slave synchronization
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
1 - first knowledge of go language
UML project example -- UML diagram description of tiktok
你还不知道责任链模式的使用场景吗?
nuxt项目:全局获取process.env信息
PCIe X1 插槽的主要用途是什么?
随机推荐
OC to swift conditional compilation, marking, macro, log, version detection, expiration prompt
Frame synchronization implementation
Svn detailed use tutorial
[untitled]
3、 Gradient descent solution θ
raised exception class EAccexxViolation with ‘Access violation at address 45EFD5 in module 出错
三、梯度下降求解最小θ
【JZ46 把数字翻译成字符串】
Leetcode162 - find peak - dichotomy - array
冰冰学习笔记:一步一步带你实现顺序表
剑指 Offer II 019. 最多删除一个字符得到回文(简单)
牛客网数据库SQL实战详细剖析(26-30)
async void 导致程序崩溃
nuxt项目:全局获取process.env信息
LeetCode165-比较版本号-双指针-字符串
OPPO数据湖统一存储技术实践
Using MATLAB programming to realize the steepest descent method to solve unconstrained optimization problems
Thinkphp5 + data large screen display effect
科技的成就(二十一)
LeetCode149-直线上最多的点数-数学-哈希表