C#-WinForm-send mail

Enter the mailbox → open the settings → become active

Preparation before sending: sender, sender password, recipient, title, content

Set the event in the <Send> button

One, refer to System.Net; and System.Net.Mail; namespace

2. Set the <Send> button

Copy code
//<Send> button 
        private void button1_Click(object sender, EventArgs e) 
        { 
            //Set the mail server to be called 
            SmtpClient smtp = new SmtpClient("smtp.qq.com"); 
            //Create the sender object 
            MailAddress from = new MailAddress(textBox1.Text); 
            //Create recipient object 
            MailAddress to = new MailAddress(textBox2.Text); 
            //The mail object to be sent contains four contents to be filled 
            MailMessage mail = new MailMessage(from, to); 
            //Set the subject of the email 
            mail.Subject = textBox3.Text; 
            //Set the subject body format of the email 
            mail.Body = textBox4.Text; 
            //Create the sender authentication credentials
            NetworkCredential cred = new NetworkCredential(textBox1.Text,textBox5.Text);
            // Bind the credential certificate to the server object and send it out 
            smtp.Credentials = cred; 
            //This server object executes the function of sending mail 
            smtp.Send(mail); 
        }
Copy code

Click the button to send

 

Guess you like

Origin blog.csdn.net/s_156/article/details/112977384