首页 > 代码库 > 【C#】#102 发送邮件

【C#】#102 发送邮件

项目需求:定时的发送邮件,于是学习了如何发送邮件

下面有一个简单的例子。能够实现简单的发送邮件,加上附件可以添加一个属性【Attachment】,然后配置上附件的路径

 

Demo下载

代码总共只有一下这一些,就可以发送邮件。

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Net.Mail;namespace SendMail{    public class EmailHelper    {        /// <summary>        /// 发送邮件        /// </summary>        /// <param name="from">发件人</param>        /// <param name="to">收件人</param>        /// <param name="title">标题</param>        /// <param name="body">邮件内容</param>        /// <returns></returns>        public static bool Send(MailAddress from, string to, string subject, string body)        {            MailMessage mailMessage = new MailMessage();            mailMessage.From = from;            mailMessage.To.Add(to);            mailMessage.To.Add("443347237@qq.com");            mailMessage.To.Add("zhongxia245@sina.com");            mailMessage.Subject = subject;            mailMessage.Body = body;            mailMessage.IsBodyHtml = false;            mailMessage.Priority = MailPriority.High;            SmtpClient sc = new SmtpClient();            sc.Host = "smtp.qq.com";   //如果使用非QQ的邮箱,需要更换            sc.Port = 25;                       sc.UseDefaultCredentials = true;            sc.EnableSsl = false;            sc.Credentials = new System.Net.NetworkCredential("账号", "密码");            try            {                sc.Send(mailMessage);            }            catch (Exception e1)            {                //抛出异常,不处理,让调用者处理                throw e1;            }            return true;        }    }}

【C#】#102 发送邮件