程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 更多關於編程 >> C#.NET發送郵件的實例代碼

C#.NET發送郵件的實例代碼

編輯:更多關於編程
    這篇文章介紹了C#.NET發送郵件的實例代碼,有需要的朋友可以參考一下,希望對你有所幫助   復制代碼 代碼如下:


    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Net.Mail;
    using System.Net;
    namespace MyQuery.Utils
    {
        /// <summary>
        /// 封裝郵件處理
        /// by 賈世義 2011-6-3
        /// </summary>
        public static class MailHelper
        {
            private static string smtpHost = null;
            private static int smptPort = 25;
            private static bool smtpIsUserCredentials = false;
            private static string smtpCredentialAccount = null;
            private static string smtpCredentialPassword = null;
            /// <summary>
            /// 設置發送郵件參數
            /// </summary>
            /// <param name="host">smtp服務器地址或名稱</param>
            /// <param name="port">smtp服務端口 一般為25</param>
            /// <param name="isUserCredentials">是否需要認證</param>
            /// <param name="account">需要認證時的用戶</param>
            /// <param name="password">需要認證時的用戶的密碼</param>
            public static void SetParameters(string host, int port, bool isUserCredentials, string account, string password)
            {
                smtpHost = host;
                smptPort = port;
                smtpIsUserCredentials = isUserCredentials;
                smtpCredentialAccount = account;
                smtpCredentialPassword = password;
            }
            /// <summary>
            /// 設置發送郵件參數 取配置
            /// </summary>
            private static void setParameters()
            {
                if (String.IsNullOrEmpty(smtpHost))
                {
                    smtpHost = WebHelper.GetAppConfig("SmtpHost");
                    smptPort = DataHelper.GetIntValue(WebHelper.GetAppConfig("SmptPort"), 25);
                    smtpIsUserCredentials = Constants.TRUE_ID.Equals(WebHelper.GetAppConfig("SmtpIsUserCredentials"));
                    smtpCredentialAccount = WebHelper.GetAppConfig("SmtpCredentialAccount");
                    smtpCredentialPassword = WebHelper.GetAppConfig("SmtpCredentialPassword");
                }
            }
            /// <summary>
            /// 發送郵件 發送郵件錯誤不會拋出異常
            /// </summary>
            /// <param name="receivers">收件人</param>
            /// <param name="title">標題/主題</param>
            /// <param name="content">信件內容</param>
            /// <param name="sender">發件人 空則取系統配置</param>
            public static void SendMail(string receivers, string title, string content, string sender)
            {
                if (!String.IsNullOrEmpty(receivers))
                {
                    //初始化參數
                    setParameters();
                    if (!String.IsNullOrEmpty(smtpHost))
                    {
                        try
                        {
                            SmtpClient smtp = new SmtpClient(smtpHost, smptPort);
                            if (smtpIsUserCredentials)
                            {
                                smtp.UseDefaultCredentials = true;
                                smtp.Credentials = new NetworkCredential(smtpCredentialAccount, smtpCredentialPassword); ;
                            }
                            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
                            if (String.IsNullOrEmpty(sender))
                            {
                                sender = smtpCredentialAccount;
                            }
                            foreach (string receiver in DataHelper.GetStrings(receivers))
                            {
                                MailMessage msg = new MailMessage(sender, receiver, title, content);
                                msg.BodyEncoding = Encoding.UTF8;
                                msg.SubjectEncoding = Encoding.UTF8;
                                msg.IsBodyHtml = true;
                                smtp.Send(msg);
                                msg.Dispose();
                            }
                        }
                        catch { }
                    }
                }
            }
        }
    }

    1. 上一頁:
    2. 下一頁:
    Copyright © 程式師世界 All Rights Reserved