SMTP发送邮件代码

    class Email
    {
        //private string subject = String.Empty;
        //private StringBuilder body = new StringBuilder();

        public static SmtpClient InitEmail(string Host, int Port, string Username, string Password, string enableSSL)
        {
            SmtpClient SMTPServer = new SmtpClient(Host, Port);
            SMTPServer.Credentials = new System.Net.NetworkCredential(Username, Password);
            SMTPServer.EnableSsl = bool.Parse(enableSSL);
            SMTPServer.DeliveryMethod = SmtpDeliveryMethod.Network;
            SMTPServer.Timeout = 180000;
            return SMTPServer;
        }
        private static void SendEmail(SmtpClient SMTPServer, string From, ArrayList To, string Subject, string Body)
        {
            MailMessage Message = new MailMessage();
            Message.From = new MailAddress(From);
            Message.Subject = Subject;
            Message.Body = HttpUtility.HtmlDecode(Body);
            Message.IsBodyHtml = false;
            foreach (string e in To)
                Message.To.Add(new MailAddress(e));
            EmailObject obj = new EmailObject();
            obj.Message = Message;
            obj.Smtp = SMTPServer;
            //SMTPServer.Send(Message);
            System.Threading.Thread thread = new System.Threading.Thread(
                delegate(Object eobj)
                {
                    try
                    {
                        EmailObject emailobject = (EmailObject)eobj;

                        emailobject.Smtp.Send(emailobject.Message);
                    }
                    catch
                    {
                    }
                }
            );
            thread.Start(obj);
            //thread.Join();
        }

     }

 

邮件内容需要换行时,可以用\r\n,但是IsBodyHtml属性必须是false;或者用<br/>,此时IsBodyHtml必须是true。

posted on 2008-07-28 17:00  美丽心情11  阅读(340)  评论(0编辑  收藏  举报