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);
            }
        }
    }
}

附:

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

下滑

相关推荐
mrbone1120 分钟前
Git-git worktree的使用
开发语言·c++·git·cmake·worktree·gitab
浪裡遊31 分钟前
Sass详解:功能特性、常用方法与最佳实践
开发语言·前端·javascript·css·vue.js·rust·sass
真实的菜38 分钟前
JVM类加载系统详解:深入理解Java类的生命周期
java·开发语言·jvm
代码讲故事1 小时前
多种方法实现golang中实现对http的响应内容生成图片
开发语言·chrome·http·golang·图片·快照·截图
虾球xz2 小时前
CppCon 2018 学习:EFFECTIVE REPLACEMENT OF DYNAMIC POLYMORPHISM WITH std::variant
开发语言·c++·学习
Allen_LVyingbo2 小时前
Python常用医疗AI库以及案例解析(2025年版、上)
开发语言·人工智能·python·学习·健康医疗
小哈龙2 小时前
裸仓库 + Git Bash 搭建 本地 Git 服务端与客户端
开发语言·git·bash
唐青枫2 小时前
C#.NET log4net 详解
c#·.net
G探险者2 小时前
《如何在 Spring 中实现 MQ 消息的自动重连:监听与发送双通道策略》
java·开发语言·rpc
weixin_437398213 小时前
转Go学习笔记
linux·服务器·开发语言·后端·架构·golang