发送邮件

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;
using System.Net.Mail;

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

        private void button1_Click(object sender, EventArgs e)
        {
            //设置要调用的发送邮件的服务器
            SmtpClient smtp = new SmtpClient("smtp.sina.cn");

            //创建发信人对象
            MailAddress from = new MailAddress(textBox1.Text);

            //创建收信人对象
            MailAddress to = new MailAddress(textBox2.Text);

            //要发送的邮件对象,包含4个内容需要填充
            MailMessage mail = new MailMessage(from, to);

            //设置邮件的标题
            mail.Subject = textBox3.Text;

            //设置邮件的主体正文内容
            mail.Body = textBox4.Text;

            //创建发件人身份验证凭证
            NetworkCredential cred = new NetworkCredential(textBox1.Text, textBox5.Text);

            //将凭证证书绑定到服务端对象上,一并发送出去
            smtp.Credentials = cred;

            //此服务器对象执行发送邮件功能
            smtp.Send(mail);
        }
    }
}

posted @ 2016-12-14 14:55  游称  阅读(128)  评论(0编辑  收藏  举报