温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2022-07-15 09:24:06 来源:亿速云 阅读:163 作者:iii 栏目:开发技术

这篇文章主要讲解了“c#中winform怎么根据邮箱地址和密码一键发送email”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“c#中winform怎么根据邮箱地址和密码一键发送email”吧!

企业信息化进程中,根据自己的Email地址一键发送邮件,了解发送原理可以批量发送多人邮箱。原来曾经用VB做过群发工资条,效果比较理想,现在使用c#做开发,原理基本一样。

应用的技术:访问邮件服务器发送邮件、文件操作保存默认信息、winform按钮的逻辑操作

效果图:

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这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是亿速云,小编将为大家推送更多相关知识点的文章,欢迎关注!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI