Skip to content

Instantly share code, notes, and snippets.

@miguelgonzalezLF
Created October 29, 2015 13:31
Show Gist options
  • Save miguelgonzalezLF/67cdade8f54ec8be4db6 to your computer and use it in GitHub Desktop.
Save miguelgonzalezLF/67cdade8f54ec8be4db6 to your computer and use it in GitHub Desktop.
using System;
using System.Net.Mail;
using Gurock.SmartInspect;
public class MailProtocol: Gurock.SmartInspect.Protocol
{
private string fHost;
private int fPort;
private string fTo;
private string fFrom;
private SmtpClient fClient;
protected override string Name
{
get { return "mail"; }
}
protected override bool IsValidOption(string name)
{
return
name.Equals("host") || /* SMTP server host */
name.Equals("port") || /* Port of SMTP server */
name.Equals("to") || /* Receiver address */
name.Equals("from") || /* Sender address */
base.IsValidOption(name); /* Handle common options */
}
protected override void LoadOptions()
{
base.LoadOptions(); /* Load common options */
this.fHost = GetStringOption("host", null);
this.fPort = GetIntegerOption("port", 25);
this.fTo = GetStringOption("to", null);
this.fFrom = GetStringOption("from", null);
}
protected override void InternalConnect()
{
this.fClient = new SmtpClient(this.fHost, this.fPort);
}
private MailMessage FormatLogEntry(LogEntry logEntry)
{
MailMessage message = new MailMessage(this.fFrom, this.fTo);
message.Subject = logEntry.Title;
message.Body += logEntry.Level;
message.Body += Environment.NewLine;
message.Body += logEntry.Title;
return message;
}
protected override void InternalWritePacket(Packet packet)
{
switch (packet.PacketType)
{
case PacketType.LogEntry:
{
MailMessage message = FormatLogEntry((LogEntry) packet);
this.fClient.Send(message);
break;
}
/* Other packet types can go here */
}
}
protected override void InternalDisconnect()
{
this.fClient = null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment