C#发送电子邮件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace SendMail
{
    public partial class SendMail : Form
    {
        public SendMail()
        {
            InitializeComponent();
        }

        private void SendMail_Load(object sender, EventArgs e)
        {
            label6.Hide();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            label6.Show();
            Cursor.Current = Cursors.WaitCursor;
            label1.Hide();
            label2.Hide();
            label3.Hide();
            label4.Hide();
            label5.Hide();
            textBox1.Hide();
            textBox2.Hide();
            textBox3.Hide();
            textBox4.Hide();
            this.Text = "正在发送邮件...";
           // toolTip1.Show("正在发送邮件....",this);
            string from = textBox1.Text.ToString();
            string to = textBox2.Text.ToString();
            string subject=textBox3.Text.ToString ();
            string body=textBox4.Text.ToString ()+"<br/>----------------------<br/><td>来自idefav_2010开发的客户端</td>";
            //创建邮箱保存实体
            MailMessage mail = new MailMessage();

            SmtpClient client = new SmtpClient();
            client.UseDefaultCredentials = true;

            NetworkCredential nc = new NetworkCredential();//邮箱验证
            nc.UserName = from;
            nc.Password = "1qaz2wsx3edc";

            client.Credentials = nc;
            client.Host = "smtp.126.com";
            client.Port = 25;
            mail.From = new MailAddress(from);
            mail.To.Add(new MailAddress(to));
            mail.Subject = subject;
            mail.SubjectEncoding = Encoding.UTF8;
            client.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            mail.Body = body;
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = radioButton1.Checked;

            mail.Attachments.Add(new Attachment(textBox5.Text.ToString()));

            try
            {
                client.Send(mail);
                // Cursor cursor ;

                MessageBox.Show("发送成功!");
                label6.Hide();
            }
            catch (Exception ee) 
            {
                MessageBox.Show("错误:" + ee.Message);
            }
           
            int size = 0;//记录邮件大小

            label1.Show();
            label2.Show();
            label3.Show();
            label4.Show();
            label5.Show();
            textBox1.Show();
            textBox2.Show();
            textBox3.Show();
            textBox4.Show();
            this.Text = "";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK) 
            {
                textBox5.Text = openFileDialog1.FileName.ToString();
            }
        }
    }
}
posted @ 2011-03-29 13:23  idefav_2010  阅读(347)  评论(0)    收藏  举报