using System;
using System.Net;
using System.Net.Mail;
using QC.QTMDotNetKernelInterface;

namespace QC.QTMToolKit.Tools;

[QTMTestClass]
public class SendMailTool
{
	private string fromAddress;

	private string toAddress;

	private string message;

	private string subject;

	private string smtpServer;

	private string errMsg;

	[QTMParameter(ParameterIOType.Input, TestList = "SendEmail", DefaultValue = "", Description = "")]
	public string FromAddress
	{
		get
		{
			return fromAddress;
		}
		set
		{
			fromAddress = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "SendEmail", DefaultValue = "", Description = "")]
	public string ToAddress
	{
		get
		{
			return toAddress;
		}
		set
		{
			toAddress = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "SendEmail", DefaultValue = "", Description = "Email message")]
	public string MessageBody
	{
		get
		{
			return message;
		}
		set
		{
			message = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "SendEmail", DefaultValue = "From QTM")]
	public string Subject
	{
		get
		{
			return subject;
		}
		set
		{
			subject = value;
		}
	}

	[QTMParameter(ParameterIOType.Input, TestList = "SendEmail", DefaultValue = "smtphost.qualcomm.com", Description = "The SMTP mail server")]
	public string SMTPServerName
	{
		get
		{
			return smtpServer;
		}
		set
		{
			smtpServer = value;
		}
	}

	[QTMParameter(ParameterIOType.Output, TestList = "SendEmail")]
	public string ErrorString
	{
		get
		{
			return errMsg;
		}
		set
		{
			errMsg = value;
		}
	}

	public bool SendEmail()
	{
		try
		{
			MailAddress from = new MailAddress(FromAddress);
			MailAddress to = new MailAddress(ToAddress);
			MailMessage mailMessage = new MailMessage(from, to);
			mailMessage.Subject = Subject;
			mailMessage.Body = MessageBody;
			try
			{
				SmtpClient smtpClient = new SmtpClient(SMTPServerName);
				smtpClient.Send(mailMessage);
				errMsg = "";
				return true;
			}
			catch (HttpListenerException ex)
			{
				errMsg = ex.Message;
				return false;
			}
		}
		catch (Exception ex2)
		{
			errMsg = ex2.Message;
			return false;
		}
	}
}
