您的位置:首页 > 编程语言 > C#

c# 邮件发送

2010-05-12 12:52 211 查看
  public enum MailFormat

        {

            /// <summary>

            /// None

            /// </summary>

            None = 0,

            /// <summary>

            /// Text

            /// </summary>

            Text = 1,

            /// <summary>

            /// Html

            /// </summary>

            Html = 2

        }

        public static void SendMail(ArrayList to, string from, string subject, string body)

        {

            if (to.Count <= 0)

                throw new ArgumentException("The adressee of mail is not specified", "to");

            if (string.IsNullOrEmpty(subject))

                throw new ArgumentException("The subject of mail is not specified", "subject");

            if (string.IsNullOrEmpty(body))

                throw new ArgumentException("The body of mail is not specified", "body");

            string From = from.Length > 0 ? from : "hkwong1003@163.com";

            string SMTPServer = "smtp.163.com";

            int SMTPPort = 25;

            bool SMTPAuthentictation = true;

            bool SMTPUseSSL = false;

            string SMTPUsername = "ddddd@163.com";

            string SMTPPassword = "************";

            SendMail(to, From, subject, body, System.Text.Encoding.UTF8,

                MailPriority.Normal, MailFormat.Html, SMTPServer, SMTPPort,

                SMTPUseSSL, SMTPAuthentictation, SMTPUsername, SMTPPassword);

        }

        /// <summary>

        /// Sends mail using the standard .net 2.0 System.Net.Mail classes

        /// </summary>

        /// <param name="to">To.</param>

        /// <param name="from">From.</param>

        /// <param name="subject">The subject.</param>

        /// <param name="body">The body.</param>

        /// <param name="bodyEncoding">The body encoding.</param>

        /// <param name="priority">The priority.</param>

        /// <param name="bodyFormat">The body format.</param>

        /// <param name="smtpServer">The SMTP server.</param>

        /// <param name="smtpPort">The SMTP port.</param>

        /// <param name="smtpUseSSL">if set to <c>true</c> [SMTP use SSL].</param>

        /// <param name="smtpAuthentication">if set to <c>true</c> [SMTP authentication].</param>

        /// <param name="smtpUsername">The SMTP username.</param>

        /// <param name="smtpPassword">The SMTP password.</param>

        public static void SendMail(ArrayList to, string from, string subject, string body, System.Text.Encoding bodyEncoding, MailPriority priority, MailFormat bodyFormat,

            string smtpServer, int smtpPort, bool smtpUseSSL,

            bool smtpAuthentication, string smtpUsername, string smtpPassword)

        {

            if (to.Count <= 0)

                throw new ArgumentException("The receiver of the mail message is not specified", "to");

            if (string.IsNullOrEmpty(from))

                throw new ArgumentException("The sender of the mail message is not specified", "from");

            if (string.IsNullOrEmpty(subject))

                throw new ArgumentException("The subject of the mail message is not specified", "subject");

            if (string.IsNullOrEmpty(body))

                throw new ArgumentException("The body of the mail message is not specified", "body");

            if (string.IsNullOrEmpty(smtpServer))

                throw new ArgumentException("The smtp server is not specified", "smtpServer");

            try

            {

                MailMessage msg = new MailMessage();

                foreach (string tos in to)

                {

                    msg.To.Add(new MailAddress(tos.ToString()));

                }

                msg.From = new MailAddress(from);

                msg.Priority = priority;

                msg.IsBodyHtml = bodyFormat == MailFormat.Html ? true : false;

                msg.BodyEncoding = System.Text.Encoding.UTF8;

                msg.Body = body;

                msg.Subject = subject;

                SmtpClient smtp = new SmtpClient();

                smtp.Host = smtpServer;

                smtp.Port = smtpPort;

                smtp.EnableSsl = smtpUseSSL;

                if (smtpAuthentication)

                    smtp.Credentials = new NetworkCredential(smtpUsername, smtpPassword);

                smtp.Send(msg);

            }

            catch (Exception ex)

            {

            }

        }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息