首页 > 代码库 > 发送邮件

发送邮件

//判断QQ是否在线 

private void simpleButton1_Click(object sender, EventArgs e)
        {
            string ssnum = "1000000";

            string eenum = "999999999";


            Int32 startnum = Int32.Parse(ssnum);

            Int32 endnum = Int32.Parse(eenum);
            Random rd = new Random();

            Int32 i = rd.Next(startnum, endnum);

            WebClient wc = new WebClient();

            wc.Encoding = Encoding.GetEncoding("GB2312");

            byte[] temp = wc.DownloadData("http://wpa.qq.com/pa?p=2:306476937:41");
            if (temp.Length == 3327)
            {

                //这里是QQ在线执行的代码

            }


        }

//发送邮件

        private void simpleButton2_Click(object sender, EventArgs e)
        {
            try
            {
                MailAddress Messagefrom = new MailAddress(this.txtSend.Text, "C#仰望着");  //发件人邮箱地址
                string MessageTo = this.txtTo.Text;  //收件人邮箱地址
                string MessageSubject = this.txtSubject.Text;        //邮件主题
                string MessageBody = this.txtBody.Text;    //邮件内容
                Send(MessageTo, MessageSubject, MessageBody, Messagefrom);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }

        }

        public void Send(string MessageTo, string MessageSubject, string MessageBody, MailAddress Messagefrom)
        {

            try
            {
                txtTo.Text = txtTo.Text.Replace(" ", "");//去除空格
                txtTo.Text = txtTo.Text.Trim();
                txtTo.Text = txtTo.Text.Replace((char)13, (char)0);
                MailMessage email = new MailMessage();
                email.From = Messagefrom;
                email.To.Add(MessageTo);//收件人邮箱地址可以是多个以实现群发
                email.Subject = MessageSubject;
                email.Body = MessageBody;
                email.IsBodyHtml = false; //是否为html格式 
                email.Priority = MailPriority.Normal;  //发送邮件的优先等级
                SmtpClient sc = new SmtpClient();
                sc.Host = cbHost.Text;              //指定发送邮件的服务器地址或IP
                sc.Port = 25;//指定发送邮件端口
                sc.DeliveryMethod = SmtpDeliveryMethod.Network;//指定如何发送电子邮件
                sc.UseDefaultCredentials = false;//是否随请求一起发送
                sc.EnableSsl = false;//安全连接设置
                sc.Credentials = new System.Net.NetworkCredential(this.txtSend.Text, this.txtSenderPwd.Text); //指定登录服务器的用户名和密码
                sc.Send(email);
                MessageBox.Show("邮件发送成功!", "系统提示");
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }

        }

发送邮件