天天看點

c#實作gmial郵件發送

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("[email protected]");

        //msg.To.Add("[email protected]");

        /**/

        /* 

        * msg.To.Add("[email protected]"); 

        * msg.To.Add("[email protected]");可以發送給多人 

        */

        //   msg.CC.Add("[email protected]");

         * msg.CC.Add("[email protected]"); 

         * msg.CC.Add("[email protected]");可以抄送給多人 

         */

        msg.From = new MailAddress("[email protected]", "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("[email protected]", "*****");//上述寫你的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;

}

繼續閱讀