c#编写邮件发送系统

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/daguniang123/article/details/78775437

初学c#,书上有简单邮件的发送系统,顿时兴趣大增,感觉自己也能写个邮箱实在太帅,写的时候反正什么也看不懂,还有很多没见过的命名空间,看着代码在差不多知道代码什么意思的基础上把程序写完了,运行的时候发现根本什么也发不出去,看着代码想了半天突然发现连个邮箱的号码密码也没有怎么可能发的出去,要是这样的话,谁都能帮我发邮件了,但是又不知道怎么写,想了想头痛,就没写了;反正后来又想写了,就自己百度了一个,打算真的要实现它,接下来就是我实现它的过程;

 

首先先看下我的界面:

 

 

 

 

一共二个界面:一个是发送的主页面,还有一个是设置界面,通过picturebox点击到设置页面;

我写的和我参考的不同,我增加了设置页面,然后里面用了注册表实现保存之前的设置;

接下来说一下发送页面的代码:

 

using System;
usingSystem.Collections.Generic;
usingSystem.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
using System.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingSystem.Net.Mail;
usingSystem.Net.Mime;
usingSystem.Net;
usingSystem.IO;
usingMicrosoft;
usingMicrosoft.Win32;
 
namespaceWindowsFormsApplication1
{
   public partial class Form1 : Form
   {
       public Form1()
       {
            InitializeComponent();
       }
    
       private void Form1_Load(object sender, EventArgs e)
       {
            comboBox1.Items.Add("smtp.gmail.com");
            comboBox1.Items.Add("smtp.163.com");
            comboBox1.Items.Add("smtp.qq.com");
            comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
            comboBox2.Items.Add("@qq.com");
            comboBox2.Items.Add("@gmail.com");
            comboBox2.Items.Add("@live.com");
            comboBox2.Items.Add("@163.com");
            comboBox2.Items.Add("@outlook.com");
            comboBox2.DropDownStyle =ComboBoxStyle.DropDownList;
            comboBox3.Items.Add("@qq.com");
            comboBox3.Items.Add("@gmail.com");
            comboBox3.Items.Add("@163.com");
            comboBox3.DropDownStyle =ComboBoxStyle.DropDownList;
 
 
            RegistryKey root = Registry.LocalMachine;
            RegistryKey software = root.OpenSubKey("SOFTWARE", true);
            if (software.OpenSubKey("MYRESIGTRY", true) == null)
            {
                RegistryKey myresigtry = software.CreateSubKey("MYRESIGTRY");
                myresigtry.SetValue("fristserver", "0");
                myresigtry.SetValue("ffristname", "");
                myresigtry.SetValue("ffristid", "");
                myresigtry.SetValue("ffristpassword", "");
                myresigtry.SetValue("tfristname", "");
                myresigtry.SetValue("tfristemail", "");
                myresigtry.SetValue("tfristserver", "0");
                root.Close();
            }
            else
           {
                RegistryKey myresigtry = software.OpenSubKey("MYRESIGTRY", true);
                comboBox1.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("fristserver"));
                comboBox3.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("fristserver"));
                comboBox2.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("tfristserver"));
                comboBox3.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("fristserver"));
                textBox2.Text = Convert.ToString(myresigtry.GetValue("ffristname"));
                textBox6.Text = Convert.ToString(myresigtry.GetValue("tfristname"));
                textBox4.Text = Convert.ToString(myresigtry.GetValue("tfristemail"));
 
            }
 
       }
 
       private void button1_Click(object sender, EventArgs e)
       {
            OpenFileDialog open = new OpenFileDialog();
            open.FileName = "";
            open.Title = "添加";
            open.InitialDirectory =Application.StartupPath;
            open.RestoreDirectory = false;
            open.Multiselect = true;//允许选择多个文件;
            open.Filter = "All files(*.*)|*.*|java(*.java;*.class)|*.java;*class|Office(*.docx;*.pptx;*.xlsx;*.txt)|*.docx;*.doc;*.pptx;*ppt;*.xlsx;*.xls;*.txt|code(*.cpp;*.c)|*.cpp;*.c|ImageFiles(*.BMP;*.JPG;*.GIF)|*.BMP;*.JPG;*.GIF";
            open.FilterIndex = 2;
            open.SupportMultiDottedExtensions =true;
           open.FileName = "";
            if (open.ShowDialog() == DialogResult.OK)
            {
                foreach (string s in open.SafeFileNames)
                {
                    treeView1.Nodes.Add(s);
                }
            }
       }
 
       private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
       {
 
       }
 
       private void button2_Click(object sender, EventArgs e)
       {
            if (treeView1.SelectedNode != null)//如果选中的不为空
            {
               treeView1.Nodes.Remove(treeView1.SelectedNode);
            }
 
       }
 
       private void button3_Click(object sender, EventArgs e)
       {
            try
            {
                SmtpClient m = new SmtpClient(comboBox1.Text);
                string t = textBox1.Text;
                MailAddress from = new MailAddress(t + comboBox3.Text, textBox2.Text, Encoding.UTF8);
                MailAddress to = new MailAddress(textBox4.Text + comboBox2.Text, textBox6.Text, Encoding.UTF8);
                MailMessage me = new MailMessage(from, to);
                foreach (TreeNode node in treeView1.Nodes)
                {
                    string s;
                    s = node.Text;
                    if (File.Exists(s))
                    {
                        Attachment attach = new Attachment(s);
                        ContentDisposition l =attach.ContentDisposition;//配置;
                        l.CreationDate = File.GetCreationTime(s);
                        l.ModificationDate = File.GetLastWriteTime(s);
                        l.ReadDate = File.GetLastAccessTime(s);
                       me.Attachments.Add(attach);
 
                    }
                    else
                    {
                        MessageBox.Show(s + "  没找到! ");
                    }
 
                }
 
                me.Subject = textBox5.Text;
                me.SubjectEncoding = Encoding.UTF8;
                me.Body = textBox7.Text;
                m.DeliveryMethod = SmtpDeliveryMethod.Network;//指定待发送的邮件通过网络发到smtp上
                me.BodyEncoding = Encoding.UTF8;
                me.IsBodyHtml = false;
                if (comboBox1.Text == "smtp.163.com")
                    m.EnableSsl = false;
                else
                    m.EnableSsl = true;
                m.UseDefaultCredentials = false;
                string username = textBox1.Text;
                string passwd = textBox3.Text;
                NetworkCredential p = new NetworkCredential(username, passwd);
                m.Credentials = p;
                m.Send(me);
                MessageBox.Show("发送成功!");
 
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
 
 
 
 
       }
 
       private void pictureBox1_Click(object sender, EventArgs e)
       {
            Form2 niahao = new Form2();
            niahao.ShowDialog();
       }
   }
}
 


 

 

接着是设置的代码:


using System;
usingSystem.Collections.Generic;
using System.ComponentModel;
usingSystem.Data;
usingSystem.Drawing;
usingSystem.Linq;
usingSystem.Text;
usingSystem.Threading.Tasks;
usingSystem.Windows.Forms;
usingMicrosoft.Win32;
 
namespaceWindowsFormsApplication1
{
   public partial class Form2 : Form
   {
       public Form2()
       {
            InitializeComponent();
       }
      
       private void Form2_Load(object sender, EventArgs e)
       {
            comboBox1.Items.Add("smtp.qq.com");
            comboBox1.Items.Add("smtp.gmail.com");
            comboBox1.Items.Add("smtp.163.com");
          
            comboBox1.DropDownStyle =ComboBoxStyle.DropDownList;
            comboBox2.Items.Add("@qq.com");
            comboBox2.Items.Add("@gmail.com");
            comboBox2.Items.Add("@163.com");
           
            comboBox2.DropDownStyle =ComboBoxStyle.DropDownList;
            comboBox3.Items.Add("@qq.com");
            comboBox3.Items.Add("@gmail.com");
            comboBox3.Items.Add("@live.com");
            comboBox3.Items.Add("@163.com");
            comboBox3.Items.Add("@outlook.com");
            comboBox3.DropDownStyle =ComboBoxStyle.DropDownList;
 
            RegistryKey root = Registry.LocalMachine;
            RegistryKey software = root.OpenSubKey("SOFTWARE", true);
            RegistryKey myresigtry = software.OpenSubKey("MYRESIGTRY", true);
            comboBox1.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("fristserver"));
            comboBox2.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("fristserver"));
            comboBox3.SelectedIndex = Convert.ToInt32(myresigtry.GetValue("tfristserver"));
            textBox1.Text = Convert.ToString(myresigtry.GetValue("ffristname"));
            textBox4.Text = Convert.ToString(myresigtry.GetValue("tfristname"));
            textBox5.Text = Convert.ToString(myresigtry.GetValue("tfristemail"));
            textBox2.Text = Convert.ToString(myresigtry.GetValue("ffristid"));
            textBox3.Text = Convert.ToString(myresigtry.GetValue("ffristpassword"));
            root.Close();
 
       }
 
       private void button1_Click(object sender, EventArgs e)
       {
 
            RegistryKey root = Registry.LocalMachine;
            RegistryKey software = root.OpenSubKey("SOFTWARE", true);
            RegistryKey myresigtry = software.OpenSubKey("MYRESIGTRY", true);
          //MessageBox.Show(comboBox1.SelectedIndex.ToString());
           
            myresigtry.SetValue("fristserver",comboBox1.SelectedIndex.ToString());
            myresigtry.SetValue("ffristname",textBox1.Text);
            myresigtry.SetValue("ffristid",textBox2.Text);
            myresigtry.SetValue("ffristpassword",textBox3.Text);
            myresigtry.SetValue("fristserver",comboBox1.SelectedIndex.ToString());
            myresigtry.SetValue("tfristname",textBox4.Text);
            myresigtry.SetValue("tfristemail",textBox5.Text);
            myresigtry.SetValue("tfristserver", comboBox3.SelectedIndex.ToString());
            root.Close();
            MessageBox.Show("设置成功!重启生效");
     
       }
 
       private void button2_Click(object sender, EventArgs e)
       {
            this.Hide();
       }
 
       private void button3_Click(object sender, EventArgs e)
       {
 
            RegistryKey root = Registry.LocalMachine;
            RegistryKey software = root.OpenSubKey("SOFTWARE", true);
 
            RegistryKey myresigtry = software.CreateSubKey("MYRESIGTRY");
            myresigtry.SetValue("fristserver", "0");
            myresigtry.SetValue("ffristname", "");
            myresigtry.SetValue("ffristid", "");
            myresigtry.SetValue("ffristpassword", "");
            myresigtry.SetValue("tfristname", "");
            myresigtry.SetValue("tfristemail", "");
            myresigtry.SetValue("tfristserver", "0");
            MessageBox.Show("设置成功!重启生效");
            root.Close();
       }
   }
}

参考博客:

https://www.cnblogs.com/youring2/archive/2008/11/29/1343911.html

https://www.cnblogs.com/babycool/p/3569183.html


展开阅读全文

没有更多推荐了,返回首页