C# Winform 入门(2)之发送邮件

主要使用控件: label button textBox

1.添加引用

cs 复制代码
using System.Net.Mail;

2.实例化邮件(封装)

  • From:邮件的发送地址
  • To.Add:邮件的接收地址
  • Subject:邮件的主题
  • Body:消息的主文(内容)
cs 复制代码
 void Send_mail()
 {
     //实例化一个邮件
     MailMessage mailMessage = new MailMessage();
     mailMessage.From = new MailAddress(Address_sender.Text);
     mailMessage.To.Add(Address_received.Text);
     mailMessage.Subject = txt_subject.Text;
     mailMessage.Body = txt_content.Text;

     //实例化邮件的传输协议
     SmtpClient smtpClient = new SmtpClient();
     // 传输协议里面的账号密码
     smtpClient.Credentials = new System.Net.NetworkCredential(Address_sender.Text, txt_pwd.Text);
     smtpClient.Host = "smtp.163.com";
     smtpClient.Send(mailMessage);
 }

3.清除按钮的实现

cs 复制代码
 private void Cleartxt_Click(object sender, EventArgs e)
 {
     if(MessageBox.Show("Do you want to erase all the information ?","Information hint",MessageBoxButtons.YesNo)==DialogResult.Yes)
     {
         Address_sender.Clear();
         Address_received.Clear();
         txt_subject.Clear();
         txt_content.Clear();
         txt_pwd.Clear();
     }
 }

4.发送邮件按钮功能

cs 复制代码
   private void Send_email_Click(object sender, EventArgs e)
   {
       try
       {
           Send_mail();
           MessageBox.Show("发送成功");
       }catch(Exception ex)
       {
           throw ex;
       }
   }

5.回车按钮实现

cs 复制代码
 private void Address_sender_KeyDown(object sender, KeyEventArgs e)
 {
     if(this.Address_sender.Text.Trim().Length!=0 && e.KeyValue==13)
     {
         this.Address_received.Focus();
         this.Address_received.SelectAll();
     }
 }

 private void Address_received_KeyDown(object sender, KeyEventArgs e)
 {
     if(this.Address_received.Text.Trim().Length!=0 && e.KeyValue==13)
     {
         this.txt_subject.Focus();
         this.txt_subject.SelectAll();
     }
 }

 private void txt_subject_KeyDown(object sender, KeyEventArgs e)
 {
     if(this.txt_subject.Text.Trim().Length!= 0 && e.KeyValue==13)
     {
         this.txt_content.Focus();
         this.txt_content.SelectAll();
     }
 }

 private void txt_content_KeyDown(object sender, KeyEventArgs e)
 {
     if(this.txt_content.Text.Trim().Length!=0 && e.KeyValue ==13)
     {
         this.txt_pwd.Focus();
         this.txt_pwd.SelectAll();
     }
 }

 private void txt_pwd_KeyDown(object sender, KeyEventArgs e)
 {
     if(this.txt_pwd.Text.Trim().Length!=0 && e.KeyValue ==13)
     {
         this.Send_email_Click(null, null);
     }
 }

cs 复制代码
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
using Sunisoft.IrisSkin;


namespace _02.Send_mail
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            SkinEngine sk=new SkinEngine();
            sk.SkinFile = "C:\\Users\\86195\\Desktop\\C#-lesson\\02.Send-mail\\bin\\Debug\\Skins\\DiamondBlue.ssk";
        }

        private void Send_email_Click(object sender, EventArgs e)
        {
            try
            {
                Send_mail();
                MessageBox.Show("发送成功");
            }catch(Exception ex)
            {
                throw ex;
            }
        }
        void Send_mail()
        {
            //实例化一个邮件
            MailMessage mailMessage = new MailMessage();
            mailMessage.From = new MailAddress(Address_sender.Text);
            mailMessage.To.Add(Address_received.Text);
            mailMessage.Subject = txt_subject.Text;
            mailMessage.Body = txt_content.Text;

            //实例化邮件的传输协议
            SmtpClient smtpClient = new SmtpClient();
            // 传输协议里面的账号密码
            smtpClient.Credentials = new System.Net.NetworkCredential(Address_sender.Text, txt_pwd.Text);
            smtpClient.Host = "smtp.163.com";
            smtpClient.Send(mailMessage);
        }

        private void Cleartxt_Click(object sender, EventArgs e)
        {
            if(MessageBox.Show("Do you want to erase all the information ?","Information hint",MessageBoxButtons.YesNo)==DialogResult.Yes)
            {
                Address_sender.Clear();
                Address_received.Clear();
                txt_subject.Clear();
                txt_content.Clear();
                txt_pwd.Clear();
            }
        }

        private void Address_sender_KeyDown(object sender, KeyEventArgs e)
        {
            if(this.Address_sender.Text.Trim().Length!=0 && e.KeyValue==13)
            {
                this.Address_received.Focus();
                this.Address_received.SelectAll();
            }
        }

        private void Address_received_KeyDown(object sender, KeyEventArgs e)
        {
            if(this.Address_received.Text.Trim().Length!=0 && e.KeyValue==13)
            {
                this.txt_subject.Focus();
                this.txt_subject.SelectAll();
            }
        }

        private void txt_subject_KeyDown(object sender, KeyEventArgs e)
        {
            if(this.txt_subject.Text.Trim().Length!= 0 && e.KeyValue==13)
            {
                this.txt_content.Focus();
                this.txt_content.SelectAll();
            }
        }

        private void txt_content_KeyDown(object sender, KeyEventArgs e)
        {
            if(this.txt_content.Text.Trim().Length!=0 && e.KeyValue ==13)
            {
                this.txt_pwd.Focus();
                this.txt_pwd.SelectAll();
            }
        }

        private void txt_pwd_KeyDown(object sender, KeyEventArgs e)
        {
            if(this.txt_pwd.Text.Trim().Length!=0 && e.KeyValue ==13)
            {
                this.Send_email_Click(null, null);
            }
        }
    }
}

附:

网易邮箱 => 打开微软浏览器 => 网易邮箱网页版 =>

下滑

相关推荐
草莓熊Lotso24 分钟前
【数据结构初阶】--算法复杂度的深度解析
c语言·开发语言·数据结构·经验分享·笔记·其他·算法
海的诗篇_40 分钟前
前端开发面试题总结-JavaScript篇(二)
开发语言·前端·javascript·typescript
cccc来财1 小时前
Go中的协程并发和并发panic处理
开发语言·后端·golang
狐凄1 小时前
Python实例题:Python计算线性代数
开发语言·python·线性代数
惊鸿一博2 小时前
java_网络服务相关_gateway_nacos_feign区别联系
java·开发语言·gateway
Bruce_Liuxiaowei2 小时前
深入理解PHP安全漏洞:文件包含与SSRF攻击全解析
开发语言·网络安全·php
成工小白2 小时前
【C++ 】智能指针:内存管理的 “自动导航仪”
开发语言·c++·智能指针
sc写算法2 小时前
基于nlohmann/json 实现 从C++对象转换成JSON数据格式
开发语言·c++·json
Andrew_Xzw2 小时前
数据结构与算法(快速基础C++版)
开发语言·数据结构·c++·python·深度学习·算法
库库的里昂2 小时前
【C++从练气到飞升】03---构造函数和析构函数
开发语言·c++