关键词:C#发送邮件  发邮件   发送邮件


因程序的需要,从网上参考了下别人的代码,我封装了一个类来发送邮件,

这类很简单,不过已经满足我的需要了:


class SendMail
    {
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="info">要发送的信息</param>
        /// <returns></returns>
        public static bool SendEmail(string info)
        {
            try
            {
                System.Net.Mail.SmtpClient client = new SmtpClient("smtp.qq.com");

                //发送邮箱的账户和密码
                client.UseDefaultCredentials = false;
                client.Credentials = new System.Net.NetworkCredential("3721649@qq.com", "123456");
                client.DeliveryMethod = SmtpDeliveryMethod.Network;

                //设置发件人信息
                MailAddress addressFrom = new MailAddress("3721649@qq.com", "FromICQ");

                //设置收件人信息,这里我们给自己发信
                MailAddress addressTo = new MailAddress("3721649@qq.com", "ToICQ");

                System.Net.Mail.MailMessage message = new MailMessage(addressFrom, addressTo);
                message.Sender = new MailAddress("3721649@qq.com"); //这里写发件人的邮箱
                message.BodyEncoding = System.Text.Encoding.UTF8;
                message.IsBodyHtml =false;
                message.Body = info;//邮件正文
                message.Subject = "Hello World^_^!...";//邮件主题

                client.Send(message);
                return true;
            }
            catch (System.Exception ex)
            {
                return false;
            }
        }
    }