c#实现gmail邮件发送

 

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
using System.Net;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        SendMailUseGmail();
    }

    public void SendMailUseGmail()
    {
        System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
        msg.To.Add("123456@qq.com");
        //msg.To.Add("b@b.com");
        /**/
        /*
        * msg.To.Add("b@b.com");
        * msg.To.Add("b@b.com");
        * msg.To.Add("b@b.com");可以发送给多人
        */
        //   msg.CC.Add("c@c.com");
        /**/
        /*
         * msg.CC.Add("c@c.com");
         * msg.CC.Add("c@c.com");可以抄送给多人
         */

        msg.From = new MailAddress("xxxxx@gmail.cn", "tao", System.Text.Encoding.UTF8);
        msg.Subject = "这是测试邮件";//邮件标题
        msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码
        msg.Body = "邮件内容";//邮件内容
        msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码
        msg.IsBodyHtml = false;//是否是HTML邮件
        msg.Priority = MailPriority.High;//邮件优先级

        SmtpClient client = new SmtpClient();
        client.Credentials = new System.Net.NetworkCredential("xxxxx@gmail.com", "*****");//上述写你的GMail邮箱和密码
        client.Port = 587;//Gmail使用的端口
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;//经过ssl加密
        object userState = msg;
        try
        {
            client.Send(msg);
            // client.SendAsync(msg, userState);
            //简单一点儿可以client.Send(msg);
            Label1.Text = "邮件已成功发送!";
        }
        catch (System.Net.Mail.SmtpException ex)
        {
            Label1.Text = ex.Message;
        }
    }


}

 

posted on 2009-09-22 13:35  晴天1848  阅读(853)  评论(3编辑  收藏  举报