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
}
}
SendEmail C#
最新推荐文章于 2022-10-21 10:33:53 发布