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

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

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

核心要点及代码(这里以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;
                 
        }
 

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT技术与企业应用结合的爱好者

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值