Skip to content

Commit

Permalink
Added SSL Validation Bypass for SMTP #170
Browse files Browse the repository at this point in the history
  • Loading branch information
saucepleez committed Dec 30, 2019
1 parent f1f0ae1 commit 077011d
Showing 1 changed file with 58 additions and 22 deletions.
80 changes: 58 additions & 22 deletions taskt/Core/Automation/Commands/SMTPSendEmailCommand.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
Expand Down Expand Up @@ -82,43 +83,78 @@ public class SMTPSendEmailCommand : ScriptCommand
[Attributes.PropertyAttributes.SampleUsage("**c:\\temp\\file.txt**")]
[Attributes.PropertyAttributes.Remarks("")]
public string v_SMTPAttachment { get; set; }

[XmlElement]
[Attributes.PropertyAttributes.PropertyDescription("SSL Validation")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Validate SSL")]
[Attributes.PropertyAttributes.PropertyUISelectionOption("Bypass SSL Validation")]

[Attributes.PropertyAttributes.InputSpecification("Select the appropriate option")]
[Attributes.PropertyAttributes.SampleUsage("Select from **Validate SSL**, **Bypass SSL Validation**")]
[Attributes.PropertyAttributes.Remarks("This field manages whether taskt will attempt to validate the SSL connection")]
public string v_SSLValidation { get; set; }
public SMTPSendEmailCommand()
{
this.CommandName = "SMTPCommand";
this.SelectionName = "Send SMTP Email";
this.CommandEnabled = true;
this.CustomRendering = true;
this.v_SSLValidation = "Validate SSL";
}

public override void RunCommand(object sender)
{
string varSMTPHost = v_SMTPHost.ConvertToUserVariable(sender);
string varSMTPPort = v_SMTPPort.ToString().ConvertToUserVariable(sender);
string varSMTPUserName = v_SMTPUserName.ConvertToUserVariable(sender);
string varSMTPPassword = v_SMTPPassword.ConvertToUserVariable(sender);

string varSMTPFromEmail = v_SMTPFromEmail.ConvertToUserVariable(sender);
string varSMTPToEmail = v_SMTPToEmail.ConvertToUserVariable(sender);
string varSMTPSubject = v_SMTPSubject.ConvertToUserVariable(sender);
string varSMTPBody = v_SMTPBody.ConvertToUserVariable(sender);
string varSMTPFilePath = v_SMTPAttachment.ConvertToUserVariable(sender);
//bypass ssl validation if requested
if (v_SSLValidation.ConvertToUserVariable(sender) == "Bypass SSL Validation")
{
ServicePointManager.ServerCertificateValidationCallback =
(sndr, certificate, chain, sslPolicyErrors) => true;
}

var client = new SmtpClient(varSMTPHost, int.Parse(varSMTPPort))
try
{
Credentials = new System.Net.NetworkCredential(varSMTPUserName, varSMTPPassword),
EnableSsl = true
};
string varSMTPHost = v_SMTPHost.ConvertToUserVariable(sender);
string varSMTPPort = v_SMTPPort.ToString().ConvertToUserVariable(sender);
string varSMTPUserName = v_SMTPUserName.ConvertToUserVariable(sender);
string varSMTPPassword = v_SMTPPassword.ConvertToUserVariable(sender);

string varSMTPFromEmail = v_SMTPFromEmail.ConvertToUserVariable(sender);
string varSMTPToEmail = v_SMTPToEmail.ConvertToUserVariable(sender);
string varSMTPSubject = v_SMTPSubject.ConvertToUserVariable(sender);
string varSMTPBody = v_SMTPBody.ConvertToUserVariable(sender);
string varSMTPFilePath = v_SMTPAttachment.ConvertToUserVariable(sender);

var client = new SmtpClient(varSMTPHost, int.Parse(varSMTPPort))
{
Credentials = new System.Net.NetworkCredential(varSMTPUserName, varSMTPPassword),
EnableSsl = true
};


var message = new MailMessage(varSMTPFromEmail, varSMTPToEmail);
message.Subject = varSMTPSubject;
message.Body = varSMTPBody;
var message = new MailMessage(varSMTPFromEmail, varSMTPToEmail);
message.Subject = varSMTPSubject;
message.Body = varSMTPBody;

if (!string.IsNullOrEmpty(varSMTPFilePath))
if (!string.IsNullOrEmpty(varSMTPFilePath))
{
message.Attachments.Add(new Attachment(varSMTPFilePath));
}

client.Send(message);
}
catch (Exception ex)
{
message.Attachments.Add(new Attachment(varSMTPFilePath));
throw ex;
}

client.Send(message);
finally
{
//restore default validation
if (v_SSLValidation.ConvertToUserVariable(sender) == "Bypass SSL Validation")
{
ServicePointManager.ServerCertificateValidationCallback = null;
}
}

}
public override List<Control> Render(frmCommandEditor editor)
{
Expand All @@ -134,7 +170,7 @@ public override List<Control> Render(frmCommandEditor editor)
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_SMTPSubject", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_SMTPBody", this, editor));
RenderedControls.AddRange(CommandControls.CreateDefaultInputGroupFor("v_SMTPAttachment", this, editor));

RenderedControls.AddRange(CommandControls.CreateDefaultDropdownGroupFor("v_SSLValidation", this, editor));
return RenderedControls;

}
Expand Down

0 comments on commit 077011d

Please sign in to comment.