I am making a console app in visual studio 2022, where I after sending message to my gmail account, want to have criterias of whether sending the message to Inbox or to the spam folder, like spam-filtering.
Right now, I have mixed sending and the spam filtering functionality without them corresponding with each other.
I hope the question makes sense.
static bool IsSpam(string text, IEnumerable<string> wordBlacklist)
{
string pattern = @"\b[\p{L}]+\b";
return
Regex.Matches(text, pattern)
.Cast<Match>()
.Select(match => match.Value.ToLower())
.Where(word => wordBlacklist.Contains(word))
.Any();
}
static void Main(string[] args)
{
var message = new MimeMessage();
message.To.Add(MailboxAddress.Parse("blah"));
message.Subject = "testing spam";
message.From.Add(MailboxAddress.Parse("blah"));
string text = "bla, bla, bla, hey";
string[] blacklist = { "bla", "hey"};
var builder = new BodyBuilder();
builder.TextBody = text;
message.Body = builder.ToMessageBody();
using (var smtpclient = new SmtpClient())
{
smtpclient.Connect("smtp.gmail.com", 587, SecureSocketOptions.StartTls);
smtpclient.Authenticate("blah, sheesh");
smtpclient.Send(message);
smtpclient.Disconnect(true);
}
try
{
Console.WriteLine("message sent");
}
catch(Exception ex)
{
Console.WriteLine(ex.ToString());
}
if (IsSpam(text, blacklist))
Console.WriteLine("The message is spam");
else
Console.WriteLine("message is not spam");
}
What I have tried:
I can send message successfully to my Inbox, and it prints correctly whether or not the message is a spam, but it needs it to filter it as spam in gmail also. I am aware, there are different techniques, when it comes to spam filtering. The one I am currently choosing is the blacklist-functionality.