c#中怎么实现winform根据邮箱地址和密码一键发送email

2023-05-29,

今天小编给大家分享一下c#中怎么实现winform根据邮箱地址和密码一键发送email的相关知识点,内容详细,逻辑清晰,相信大部分人都还太了解这方面的知识,所以分享这篇文章给大家参考一下,希望大家阅读完这篇文章后有所收获,下面我们一起来了解一下吧。

效果图:

核心要点及代码(这里以163为例)

1.发送代码:这是最核心的,注意引用。文本框:发送地址,发送密码,发送服务器,接收地址,发送主题,发送内容。

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 System.Text.RegularExpressions;
using System.IO;

  private void button1_Click(object sender, EventArgs e)
        {
            Regex r = new Regex("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$");
            if (!(r.IsMatch(tbSend.Text)))  //用正则表达式验证邮箱
            {
                MessageBox.Show("发送邮箱地址格式不正确!");
                return;
            }
           
            //生成SmtpClient实例,用它发送电子邮件
            MailMessage mail = new MailMessage();
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.From = new MailAddress(tbSend.Text);
            mail.To.Add(new MailAddress(tbAccep.Text));
            mail.Subject = tbAcceptS.Text;
            mail.Body = tbB.Text;
            //生成SmtpClient实例,用它发送电子邮件
            //指定SMTP服务器主机
            SmtpClient client = new SmtpClient(tbSendS.Text);
            client.UseDefaultCredentials = false;
            client.EnableSsl = true;
            client.Credentials = new System.Net.NetworkCredential(tbSend.Text.Substring(0, tbSend.Text.IndexOf('@')), tbSendP.Text);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            try
            {
                client.Send(mail);
                MessageBox.Show("发送成功");
            }
            catch (Exception ex)
            {
                MessageBox.Show("发送失败" + ex.Message.ToString());
            }


        }

2.配置了一些方便操作的功能,比如可以把默认发送地址密码保存在文件中,每次可以提取,还可以随时修改默认地址和密码。对winform的美观性做了强化。这里展示一些代码。有2个文本框是隐藏的,为了输入默认地址。一键可以现实。

这2个是修改默认地址的代码

  private void button3_Click(object sender, EventArgs e)
        {
            if (n%2 == 0)
            {
                textBox1.Visible = true;
                textBox2.Visible = true;
                string fileName = Environment.CurrentDirectory + "\\myText" + ".txt";
                if (System.IO.File.Exists(fileName))
                {
                    var lines = File.ReadAllLines(@fileName);
                    string str0 = lines[0];
                    string str1 = lines[1];
                    textBox1.Text = str0;
                    textBox2.Text = str1;
                
                }
           
                n++;
                button3.Text = "确认修改";

            }
            else
            {
                if (writefile(textBox1.Text, textBox2.Text))
                {
                    textBox1.Visible = false;
                    textBox2.Visible = false;
                    n++;
                    button3.Text = "修改默认";
                }

            }
        }

   private static bool writefile(string name, string password)
        {
            string fileName = Environment.CurrentDirectory + "\\myText" + ".txt";
            if (System.IO.File.Exists(fileName))
            {
               File.Delete(fileName);
            }
      
                StreamWriter sw = File.AppendText(fileName);
               sw.WriteLine(name);
               sw.WriteLine(password);
               sw.Flush();
               sw.Close();
          
            return true;
                 
        }

以上就是“c#中怎么实现winform根据邮箱地址和密码一键发送email”这篇文章的所有内容,感谢各位的阅读!相信大家阅读完这篇文章都有很大的收获,小编每天都会为大家更新不同的知识,如果还想学习更多的知识,请关注本站行业资讯频道。

《c#中怎么实现winform根据邮箱地址和密码一键发送email.doc》

下载本文的Word格式文档,以方便收藏与打印。