简单的发送邮件代码

  #region 邮件操作
  /// <summary>
  /// 获取MailMessage,邮件对象
  /// </summary>
  /// <param name="from">发件人</param>
  /// <param name="to">收件人</param>
  /// <param name="subject">邮件主题</param>
  /// <param name="body">邮件正文</param>
  /// <param name="bodyFormat">邮件发送方式:html:以html方式发送;默认以text方式发送</param>
  /// <param name="priority">邮件优先级("0":高优先级;"1":普通优先级;"2":低优先级;默认:普通优先级</param>
  /// <returns></returns>
  private MailMessage GetMailMessage(string from,string password,string to, string subject,string body,string bodyFormat,string priority)
  {
   MailMessage mail = new MailMessage();
   mail.From = from;
   mail.To = to;
   mail.Subject = subject;
   mail.Body = body;
   mail.BodyFormat = (bodyFormat == "html" ? MailFormat.Html : MailFormat.Text);
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", from); //set your username here
   mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); //set your password here
   switch(priority)
   {
    case "0" : mail.Priority = MailPriority.High;break;
    case "1" : mail.Priority = MailPriority.Normal;break;
    case "2" : mail.Priority = MailPriority.Low;break;
    default:mail.Priority = MailPriority.Normal;break;
   }

   return mail;
  }

  private MailMessage GetMailMessage(string from,string to,string password,string subject,string body)
  {
   MailMessage mail = GetMailMessage(from,to,password,subject,body,"text","1");

   return mail;
  }
  

  private void Send(string smtpServer, MailMessage mail)
  {
   SmtpMail.SmtpServer = smtpServer;
   SmtpMail.Send(mail);
  }

  #endregion 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值