天天看點

C# ASP.NET MVC SendMail 發送郵件

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Net.Mail;

using System.Data;

using CDO;

using ADODB;

namespace SendMailTest

{

    class Program

    {

        static void Main(string[] args)

        {

            SendMail();

        }

        public static string SendMsg()

        {

            DataTable dt = new DataTable();

            dt.Columns.Add("name");

            dt.Columns.Add("date");

            dt.Columns.Add("area");

            dt.Columns.Add("orgnizer");

            dt.Columns.Add("keyword");

            for (int i = 0; i < 10; i++)

            {

                DataRow dr = dt.NewRow();

                dr["name"] = "北文中心影視産權交易平台•影視項目路演季---路演項目征集" + i;

                dr["date"] = "2017-06-30";

                dr["area"] = "北京市 北京電影學院文創園(平房園區)" + i;

                dr["orgnizer"] = "北文中心影視産權交易" + i;

                dr["keyword"] = "影視" + i;

                dt.Rows.Add(dr);

            }

            string MailBody = "<p style=\"font-size: 10pt\">以下内容為系統自動發送,請勿直接回複,謝謝。</p><table cellspacing=\"1\" cellpadding=\"3\" 0\" bgcolor=\"000000\" style=\"font-size: 10pt;line-height: 15px;\">";

            MailBody += "<div align=\"center\">";

            MailBody += "<tr>";

            for (int hcol = 0; hcol < dt.Columns.Count; hcol++)

            {

                MailBody += "<td bgcolor=\"999999\">&nbsp;&nbsp;&nbsp;";

                MailBody += dt.Columns[hcol].ColumnName;

                MailBody += "&nbsp;&nbsp;&nbsp;</td>";

            }

            MailBody += "</tr>";

            for (int row = 0; row < dt.Rows.Count; row++)

            {

                MailBody += "<tr>";

                for (int col = 0; col < dt.Columns.Count; col++)

                {

                    MailBody += "<td bgcolor=\"dddddd\">&nbsp;&nbsp;&nbsp;";

                    MailBody += dt.Rows[row][col].ToString();

                    MailBody += "&nbsp;&nbsp;&nbsp;</td>";

                }

                MailBody += "</tr>";

            }

            MailBody += "</table>";

            MailBody += "</div>";

            return MailBody;

        }

        public static void SendMail()

        {

            MailMessage msg = new MailMessage();

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

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

            msg.From = new MailAddress("[email protected]", "ffff", System.Text.Encoding.UTF8);

            msg.Subject = "這是測試郵件";//郵件标題  

            msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件标題編碼  

            //msg.Body = "郵件内容";//郵件内容  

            msg.Body = SendMsg();

            msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件内容編碼  

            msg.IsBodyHtml = true;//是否是HTML郵件  

            msg.Priority = MailPriority.High;//郵件優先級

            SmtpClient client = new SmtpClient();

            //client.Host = "smtp.ctrchina.cn";

            client.Host = "smtp.qq.com";//發郵件SMTP伺服器位址

            client.Port = 587;//發郵件SMTP伺服器位址端口

            //client.EnableSsl = true;//經過ssl加密  

            client.Credentials = new System.Net.NetworkCredential("[email protected]", "password");//驗證發件人身份(發件人的郵箱,郵箱裡SMTP伺服器授權碼);

            object userState = msg;

            try

            {

                //client.SendAsync(msg, userState);

                client.Send(msg);

            }

            catch (System.Net.Mail.SmtpException ex)

            {

                return;

            }

        }

        public static void SendSinaMail()

        {

            MailMessage msg = new MailMessage();

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

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

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

            msg.From = new MailAddress("[email protected]", "shao_sks", System.Text.Encoding.UTF8);

            msg.Subject = "這是測試郵件";//郵件标題  

            msg.SubjectEncoding = System.Text.Encoding.UTF8;//郵件标題編碼  

            //msg.Body = "郵件内容";//郵件内容  

            msg.Body = SendMsg();

            msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件内容編碼  

            msg.IsBodyHtml = true;//是否是HTML郵件  

            msg.Priority = MailPriority.High;//郵件優先級

            SmtpClient client = new SmtpClient();

            client.Host = "smtp.sina.com";

            client.Port = 25;

            //client.EnableSsl = true;//經過ssl加密  

            client.Credentials = new System.Net.NetworkCredential("username", "password");

            object userState = msg;

            try

            {

                //client.SendAsync(msg, userState);

                client.Send(msg);

            }

            catch (System.Net.Mail.SmtpException ex)

            {

                return;

            }

        }

        public static void SenMail1()

        {

            try

            {

                CDO.Message oMsg = new CDO.Message();

                Configuration MyConfig = new ConfigurationClass();

                Fields MyFields = MyConfig.Fields;

                MyFields[@"http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2;

                MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserverport"].Value = 465;

                MyFields[@"http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = "210.77.136.200";

                MyFields.Update();

                oMsg.Configuration = MyConfig;

                oMsg.Subject = "Test SMTP2911111";

                oMsg.HTMLBody = SendMsg();

                oMsg.From = "[email protected]";

                oMsg.To = "[email protected]";

                oMsg.Send();

            }

            catch (Exception ex)

            {

                return;

            }

        }

    }

}

繼續閱讀