asp.net 郵件

//添加引用
using System.Net.Mail;
using System.Text;

/// <summary>
        /// 發送郵件
        /// </summary>
        /// <param name="fromemail">發件人郵箱</param>
        /// <param name="pwd">發件人密碼</param>
        /// <param name="toemail">收件人郵箱</param>
        /// <param name="subject">主題</param>
        /// <param name="body">內容</param>
        /// <returns></returns>
        public static bool send(string fromemail, string pwd, string toemail, string subject, string body)
        {
            SmtpClient client = new SmtpClient();
            client.Host = "smtp." + fromemail.Remove(0, fromemail.IndexOf("@") + 1);
            client.UseDefaultCredentials = false;
            client.Credentials = new System.Net.NetworkCredential(fromemail, pwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(fromemail, toemail);
            message.Subject = "標題:" + subject;
            message.Body = "內容:" + body;
            message.BodyEncoding = Encoding.UTF8;
            message.IsBodyHtml = true;
            try
            {
                client.Send(message);
                return true;
            }
            catch
            {
                throw;
            }
        }
//發送

  protected void Button1_Click(object sender, EventArgs e)
        {
            string str = "目標郵箱1,目標郵箱2,目標郵箱3,目標郵箱4";
            string[] arrystr = str.Split(',');
            foreach (string i in arrystr)
            {
                send("郵箱", "密碼", i.ToString(), "測試標題", "內容");
            }
            //if (send("源郵箱", "密碼", "目標郵箱", "測試標題", "內容") != true)
            //{
            //    Response.Write("失敗");
            //}
            //else
            //{
            //    Response.Write("成功");
            //}
        }

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章