可以完成发送邮件功能的C#代码

实例如下:

public void SendMail(string from, string to,
                     string subject, string body)
        {
            string mailServerName = "smtp.test.com";
            try
            {
                //MailMessage represents the e-mail being sent
                using (MailMessage message = new MailMessage(from,
                       to, subject, body))
                {
                    message.IsBodyHtml = true;
                    SmtpClient mailClient = new SmtpClient();
                    mailClient.Host = mailServerName;
                    mailClient.UseDefaultCredentials = true;
                    mailClient.DeliveryMethod =
                       SmtpDeliveryMethod.PickupDirectoryFromIis;
                    //Send delivers the message to the mail server
                    mailClient.Send(message);
                }
            }
            catch (SmtpException ex)
            {
                throw new ApplicationException
                   ("SmtpException has oCCured: " + ex.Message);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

 

来源:

Send Mails from within a .NET 2.0 Application

http://www.codeguru.com/csharp/csharp/cs_misc/e-mail/article.php/c9957

posted on 2010-02-25 14:00  中道学友  阅读(341)  评论(0编辑  收藏  举报

导航

技术追求准确,态度积极向上