当前位置:网站首页>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