SendEmail C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Text.RegularExpressions;

namespace TESTEmail
{
	public class Email
	{
		#region Properties
		/// <summary>
		/// Gets or sets the SMTP server password
		/// </summary>
		public string Password { get; set; }

		/// <summary>
		/// Gets or sets the SMTP server port
		/// </summary>
		public string Port { get; set; }

		/// <summary>
		/// Gets or sets the SMTP server
		/// </summary>
		public string Server { get; set; }

		/// <summary>
		/// Gets or sets the SMTP server user id (email Address)
		/// </summary>
		public string UserId { get; set; }

		#endregion

		#region Constructor
		/// <summary>
		/// Create a new Email object
		/// </summary>
		public Email()
		{ 
		}

		/// <summary>
		/// Create a new Email object with the provided SMTP settings string
		/// </summary>
		/// <param name="pConnectionString">The SMTP Settings string</param>
		public Email(string pConnectionString)
		{
			this.Server = this.GetSetting("Server", pConnectionString);
			this.Port = this.GetSetting("Port", pConnectionString);
			this.UserId = this.GetSetting("User Id", pConnectionString);
			this.Password = this.GetSetting("Password", pConnectionString);
		}

		/// <summary>
		/// Create a new Email object with the provided settings
		/// </summary>
		/// <param name="pServer">The SMTP Server</param>
		/// <param name="pPort">The SMTP Port</param>
		/// <param name="pUserId">The SMTP User name/Email Address</param>
		/// <param name="pPassword">The SMTP Password</param>
		public Email(string pServer, string pPort, string pUserId, string pPassword)
		{
			this.Server = pServer;
			this.Port = pPort;
			this.UserId = pUserId;
			this.Password = pPassword;
		}

		#endregion

		#region Methods
		#region Private Methods
		/// <summary>
		/// Returns the setting value from the source string
		/// </summary>
		/// <param name="name">The name of the setting to find</param>
		/// <param name="source">The complete source string</param>
		private string GetSetting(string name, string source)
		{
			string
				retVal = string.Empty;

			Regex
				regex = new Regex(name + "=([^;]+)");

			return retVal = regex.Match(source).Groups[1].Value;
		}

		#endregion

		#region Public Methods
		/// <summary>
		/// Send an email message to the desired recipient(s), returning an error message if an issue is found
		/// </summary>
		/// <param name="subject">The subject of the email</param>
		/// <param name="msg">The content of the email</param>
		/// <param name="asHtml">A value indicating whether the email content is Html formatted text</param>
		/// <param name="from">The from email address</param>
		/// <param name="recipients">The semi-colan seperated string of recipient email address(es)</param>
		public string Send(string subject, string msg, bool asHtml, string from, string recipients)
		{
			return this.Send(subject, msg, asHtml, from, recipients, string.Empty, string.Empty);
		}

		/// <summary>
		/// Send an email message to the desired recipient(s), returning an error message if an issue is found
		/// </summary>
		/// <param name="subject">The subject of the email</param>
		/// <param name="msg">The content of the email</param>
		/// <param name="asHtml">A value indicating whether the email content is Html formatted text</param>
		/// <param name="from">The from email address</param>
		/// <param name="recipients">The semi-colan seperated string of recipient email address(es)</param>
		/// <param name="cc">The semi-colan seperated string of carbon copy email address(es)</param>
		public string Send(string subject, string msg, bool asHtml, string from, string recipients, string cc)
		{
			return this.Send(subject, msg, asHtml, from, recipients, cc, string.Empty);
		}

		/// <summary>
		/// Send an email message to the desired recipient(s), returning an error message if an issue is found
		/// </summary>
		/// <param name="subject">The subject of the email</param>
		/// <param name="msg">The content of the email</param>
		/// <param name="asHtml">A value indicating whether the email content is Html formatted text</param>
		/// <param name="from">The from email address</param>
		/// <param name="recipients">The semi-colan seperated string of recipient email address(es)</param>
		/// <param name="cc">The semi-colan seperated string of carbon copy email address(es)</param>
		/// <param name="bcc">The semi-colan seperated string of blind carbon copy email address(es)</param>
		public string Send(string subject, string msg, bool asHtml, string from, string recipients, string cc, string bcc)
		{
			string
				err = string.Empty;
			MailMessage
				message = null;
			SmtpClient
				client = new SmtpClient(this.Server, Convert.ToInt16(this.Port));

			if (!string.IsNullOrEmpty(this.UserId) || !string.IsNullOrEmpty(this.Password))
			{
				NetworkCredential
					credential = new NetworkCredential(this.UserId, this.Password);

				client.Credentials = credential;
			}

			try
			{
				message = new MailMessage();

				MailAddress
					fromAddr = new MailAddress(from);

				message.From = fromAddr;
				message.IsBodyHtml = asHtml;
				message.Subject = subject;
				message.Body = msg;

				foreach (string toAddr in recipients.Split(';'))
				{
					if (!string.IsNullOrEmpty(toAddr))
						message.To.Add(toAddr);
				}

				if (!string.IsNullOrEmpty(cc))
					message.CC.Add(cc);

				if (!string.IsNullOrEmpty(bcc))
					message.Bcc.Add(bcc);
				
				client.Send(message);
			}

			// Catch the exception
			catch (Exception ex)
			{
				// Set the error message to the exception's message
				err = ex.Message;
			}

			finally
			{
				// If the message object is null, dispose of it
				if (message != null)
				{
					message.Dispose();
				}
			}

			// Return the error message string
			return err;
		}
				
		#endregion

		#endregion

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值