/// <summary> /// 添加收信人 /// </summary> /// <param name="mailMessage">要添加收信人的<see cref="MailMessage"/>对象</param> /// <param name="receiveAddress">收信人地址</param> public static void AddReceiver(MailMessage mailMessage, params string[] receiveAddress) { if (mailMessage == null) { throw new NullReferenceException("MailMessage未初始化!"); } foreach (string address in receiveAddress) { if (string.IsNullOrEmpty(address)) { continue; } if (!RegularUtil.VerifyEmail(address)) { throw new ArgumentException("收信人地址错误!", nameof(receiveAddress)); } mailMessage.To.Add(address); } }
/// <summary> /// 创建一个<see cref="MailMessage"/>对象 /// </summary> /// <param name="senderAddress">发信人地址</param> /// <param name="title">邮件标题</param> /// <returns></returns> public static MailMessage CreateMailMessage(string senderAddress, string title = "") { if (StringUtil.IsNullOrEmpty(senderAddress)) { throw new ArgumentNullException(nameof(senderAddress), "发信人地址不允许为空!"); } if (!RegularUtil.VerifyEmail(senderAddress)) { throw new ArgumentException("发件人地址错误!", nameof(senderAddress)); } MailMessage result = new MailMessage(); result.From = new MailAddress(senderAddress); result.Subject = title; result.IsBodyHtml = true; result.BodyEncoding = Encoding.Default; result.Priority = MailPriority.Normal; return(result); }