-
-
Save miguelangelgonzalez/70c56778da066f9ad60e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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