读书人

那位大哥会用C#把当地文件发到邮箱

发布时间: 2012-09-19 13:43:54 作者: rapoo

那位大哥会用C#把本地文件发到邮箱
RT 我用的winform程序,把本地的图片发送到邮箱!163 qq邮箱都可以!坐等高手

[解决办法]

C# code
 private void button1_Click(object sender, EventArgs e)        {            string To = this.textBox1.Text;//发送到            string From = this.textBox2.Text;//发送邮箱            string password = this.MaskedtextBox1.Text;//发送邮箱密码            string name = this.textBox3.Text;            if (To != "" & From != "")            {                SmtpClient smtp = new SmtpClient();                 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;                 smtp.EnableSsl = false;                smtp.Host = "smtp.163.com";//格式:smtp.163.com                smtp.Port = 25;                            smtp.UseDefaultCredentials = true;                                smtp.Credentials = new NetworkCredential(From, password);                MailMessage Message = new MailMessage();                // Message.BodyEncoding = System.Text.Encoding.UTF8;                Message.Priority = MailPriority.High;                Message.From = new MailAddress(From, name, Encoding.GetEncoding(936));                               Message.ReplyTo = new MailAddress(From, "我的接收邮箱", Encoding.GetEncoding(936)); Message.To.Add(ToAd);                Message.Subject = textBox4.Text; //邮件标题                Message.SubjectEncoding = Encoding.GetEncoding(936);                          Message.IsBodyHtml = true;                Message.BodyEncoding = Encoding.GetEncoding(936);                Message.Body = textBox5.Text;//内容                if (textBox6.Text != "")                {                    Attachment att = new Attachment(@textBox6.Text);                    Message.Attachments.Add(att);//加附件                }                try                {                    if (textBox6.Text != "")                    {                        EmailWithProgress();                    }                    smtp.Send(Message); //发送邮件,如果不返回异常, 则大功告成了。                    MessageBox.Show("Email 已发送完成!", "系统V2.01");                }                catch (Exception ex)                {                    MessageBox.Show("发送失败!" + ex.ToString(), "系统V2.01");                }            }        }  public static int filelen = 0;        private void EmailWithProgress()        {            string filepath = textBox6.Text;            if (!File.Exists(filepath))            {                MessageBox.Show(filepath + "文件不存在", "系统V2.01");                return;            }            else            {                FileInfo fi1elens = new FileInfo(filepath);                int filelen = Convert.ToInt32(fi1elens.Length);                progressBar1.Visible = true;//进度条                progressBar1.Minimum = 1;                progressBar1.Maximum = filelen;                progressBar1.Value = 1;                progressBar1.Step = 1;                for (int x = 1; x <= filelen; x++)                {                    if (filelen > 0)                    {                        progressBar1.PerformStep();                    }                }            }        }        private void button2_Click(object sender, EventArgs e)        {            Stream myStream = null;            OpenFileDialog openFileDialog1 = new OpenFileDialog();            openFileDialog1.InitialDirectory = "c:\\";            openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";            openFileDialog1.FilterIndex = 2;            openFileDialog1.RestoreDirectory = true;            if (openFileDialog1.ShowDialog() == DialogResult.OK)            {                try                {                    if ((myStream = openFileDialog1.OpenFile()) != null)                    {                        using (myStream)                        {                            textBox6.Text = openFileDialog1.FileName;                        }                    }                }                catch (Exception ex)                {                    MessageBox.Show(ex.Message);                }            }        } 

读书人网 >C#

热点推荐