zoukankan      html  css  js  c++  java
  • c#语言邮件发送参考代码

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.IO;
    using System.Net.Mail;
    using System.Text;
    using System.Windows.Forms;
    using System.Drawing.Imaging;
    using System.Collections;
    using System.Threading;
    using Microsoft.Win32;
    
    namespace QQ邮件发送
    {
        public partial class Form1 : Form
        {
            public Thread thread;//定义一个线程
            public Form1()
            {
                InitializeComponent();
                
                this.im = Image.FromFile("1.jpg");
                this.BackgroundImage = im;
            }
            string file;
            //string aa;
            ArrayList Lujing=new ArrayList();
         
            private void button6_Click(object sender, EventArgs e) //添加附件
            {
                openFileDialog1.Filter = "Word文件.doc|*.doc|表格文件.xls|*.xls|所有文件|*.*"; //筛选文件类型
                DialogResult dk = openFileDialog1.ShowDialog();  //创建一个枚举类型的变量dk来接收打开这个对话框
                if (dk == DialogResult.OK) //如果点的是确定,才会执行下面的代码
                {
                 
                    string[] files = openFileDialog1.FileNames;//将获取到的所有路径都存到一个string类型的数组里面
                    foreach (string file in files)//遍历所有路径,一条条的加到Lujing这个ArrayList里面去,然后用的时候再从集合里面一条条遍历出来
                    {
                        Lujing.Add(file);
                        txtfujianname.AppendText(file + "
    ");//AppendText追加文本
                    }
                    MessageBox.Show("添加附件成功");
                    //获取附件的名字添加到文本框当中
                }
    
    
            }
         
            //下面开始写SendEmail函数
            MailMessage msg;
          
            public void SendEmail(string Emailshoujian, string Emailbiaoti, string Emailzhengwen, MailAddress EmailFrom)
            {
                try
                {
                    //创建发送邮箱,并获取发件人地址,收件人地址,以及邮件标题与正文
                    msg = new MailMessage();  //创建一个MailMessage的类,用来发送邮件
                    msg.To.Add(Emailshoujian); //将收件人的邮箱地址添加进来  
                    msg.Subject = Emailbiaoti; //获取一下发送邮件的标题
                    msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题改成国际编码方式
                    msg.From = EmailFrom;//获取一下发件人的邮箱地址
                    msg.Body = Emailzhengwen;//邮件的正文内容
                    msg.BodyEncoding = System.Text.Encoding.UTF8; //将邮件的正文内容改一下编码方式
                    msg.IsBodyHtml = false; //确认正文内容是不是以网页格式发送的
                    msg.Priority = MailPriority.High;//邮件发送的优先等级为最高
    
                  
                    //添加附件
                    if(txtfujianname.Text!="")
                    {
                            foreach (string path in Lujing) //路径都存到Lujing里面去了,全部遍历出来
                            {
                                Attachment data = new Attachment(path); //Attachment:附件
                                msg.Attachments.Add(data);
                            }       
                    }
    
                    // //设置用于验证发件人身份的凭据
                    SmtpClient client = new SmtpClient(); //允许应用程序使用简单邮件传输协议 (SMTP) 来发送电子邮件。 
                    client.Host = "smtp.qq.com"; //设置一下应用程序的服务器名称
    
    
    
                    //请在这里输入您邮箱和密码!!!!!!!!!!!!!!!!!!!!!!!!!!!!
                    client.Credentials = new System.Net.NetworkCredential("527384244@qq.com", "lk19920619"); //输入发件人邮箱的用户名密码来发送邮件
                    //注意!!必须在发送的时候将发件人的邮箱账户POP3/IMAP协议开启,然后输入的密码是QQ邮箱独立密码,而不是QQ密码!!!!
                    client.Send(msg);//发送
                    //mail from address must be same as authorization user 若出现这个错误,证明没有将发件人邮箱的POP3/IMAP协议打开,并且密码是QQ邮箱独立密码,而不是QQ密码
                    //打开方式在最有一张截图
                    MessageBox.Show("发送成功");
                }
                catch (Exception ex  )
                {
    
                    throw ex;
                }
               
            }
    
            //当点击发送按钮的时候启动新的线程,从而不会因为卡住而影响到别的按钮的功能
            private void button1_Click(object sender, EventArgs e) //发送按钮
            {
                //thread = new Thread(new ThreadStart(fasong)); //为button1按钮造一个新的线程,这样点button1的话,就会运行新造的这个线程,而不会影响到窗体的主线程,button1执行的发送,点了button1如果卡住,但不会影响到别的按钮的作用
                //thread.IsBackground = true;//确认新造的线程后台运行
                //thread.Start();
                fasong();
            }
            public void fasong()
            {
                // MailAddress 表示电子邮件发件人或收件人的地址。
                MailAddress EmailFrom = new MailAddress("527384244@qq.com");  //发件人邮箱地址 //创建一个MailAddress的类来写发件人的地址
                string Emailshoujian = txtshoujian.Text;  //收件人邮箱地址
                string Emailbiaoti = txtbiaoti.Text; //邮件标题
                string Emailzhengwen = textBox1.Text; //邮件内容
                SendEmail(Emailshoujian, Emailbiaoti, Emailzhengwen, EmailFrom);  //调用发送邮件函数
            }
            private bool isok;
            private int dianxiaqux;
            private int dianxiaquy;
            private int chushix;
            private int chushiy;
            private int movex;
            private int movey;
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                isok = true;
                dianxiaqux = Cursor.Position.X;
                dianxiaquy = Cursor.Position.Y;
                chushix = this.Location.X;
                chushiy = this.Location.Y;
            }
    
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isok)
                {
                    movex = Cursor.Position.X;
                    movey = Cursor.Position.Y;
                    this.Location = new Point(chushix + movex - dianxiaqux, chushiy + movey - dianxiaquy);
                }
            }
    
            private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                isok = false;
            }
    
            private void button5_Click(object sender, EventArgs e)
            {
                this.Close();
            }
    
            private void button7_Click(object sender, EventArgs e)
            {
               
            }
    
            private void button7_Click_1(object sender, EventArgs e)
            {
                this.WindowState = FormWindowState.Minimized;
            }
            Image im;
            private int i = 1;
            private void button8_Click(object sender, EventArgs e)
            {
                if (i == 0)
                {
                    this.im = Image.FromFile("5.jpg");
                    this.BackgroundImage = im;
                }
                else if(i == 1)
                {
                    this.im = Image.FromFile("2.jpg");
                    this.BackgroundImage = im;
                }
                 else if(i == 2)
                {
                    this.im = Image.FromFile("3.jpg");
                    this.BackgroundImage = im;
                   
                }
                else if (i == 3)
                {
                    this.im = Image.FromFile("4.jpg");
                    this.BackgroundImage = im;
                }
                else if (i == 4)
                {
                    this.im = Image.FromFile("5.jpg");
                    this.BackgroundImage = im;
                }
                else 
                {
                    this.im = Image.FromFile("1.jpg");
                    this.BackgroundImage = im;
                    i = 0;
                 }
               
                  i++;
            }
    
            private void button9_Click(object sender, EventArgs e)
            {
               
            }
            string pic;
            private void button9_Click_1(object sender, EventArgs e)
            {
                
               
            }
            SoftReg softReg = new SoftReg();
            private void Form1_Load(object sender, EventArgs e) //在页面加载的时候判断软件是否注册
            {
                //判断软件是否注册
                RegistryKey retkey = Registry.CurrentUser.OpenSubKey("SOFTWARE", true).CreateSubKey("mySoftWare").CreateSubKey("Register.INI");
                foreach (string strRNum in retkey.GetSubKeyNames())
                {
                    if (strRNum == softReg.GetRNum())
                    {
                        this.label1.Text = "此软件已注册!";
                        this.btnReg.Enabled = false;
                        return;
                    }
                }
                this.label1.Text = "此软件尚未注册!";
                this.btnReg.Enabled = true;
                MessageBox.Show("您现在使用的是试用版,可以免费试用30次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Int32 tLong;    //已使用次数
                try
                {
                    tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\mySoftWare", "UseTimes", 0);
                    MessageBox.Show("您已经使用了" + tLong + "次!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                catch (Exception)
                {
                    MessageBox.Show("欢迎使用本软件!", "信息", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\mySoftWare", "UseTimes", 0, RegistryValueKind.DWord);
                }
                //判断是否可以继续试用
                tLong = (Int32)Registry.GetValue("HKEY_LOCAL_MACHINE\SOFTWARE\mySoftWare", "UseTimes", 0);
                if (tLong < 30)
                {
                    int tTimes = tLong + 1;
                    Registry.SetValue("HKEY_LOCAL_MACHINE\SOFTWARE\mySoftWare", "UseTimes", tTimes);
    
                }
                else
                {
                    DialogResult result = MessageBox.Show("试用次数已到!您是否需要注册?", "信息", MessageBoxButtons.YesNo, MessageBoxIcon.Information);
                    if (result == DialogResult.Yes)
                    {
                        //FormRegister.state = false; //设置软件状态为不可用
                        btnReg_Click(sender, e);    //打开注册窗口
                    }
                    else
                    {
                        Application.Exit();
                    }
                }
            }
    
            private void btnReg_Click(object sender, EventArgs e)
            {
                FormRegister frmRegister = new FormRegister();
                frmRegister.ShowDialog();
            }
    
            //这是为了打开添加附件的窗口,从而获得选择的附件的路径
         
        }
    }
  • 相关阅读:
    《企业虚拟化应用实战》笔记
    s3c2440笔记1(启动)
    reactor & proactor 笔记
    交换机选型笔记
    Intel VT-x 基本概念
    note of introduction of Algorithms(Lecture 3
    STC12C5A60S2笔记8(串口)
    STC12C5A60S2笔记7(定时器)
    bzoj 3242: [Noi2013]快餐店 章鱼图
    高精度模板
  • 原文地址:https://www.cnblogs.com/275147378abc/p/4621244.html
Copyright © 2011-2022 走看看