Click here to Skip to main content
15,664,272 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
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"; // Regular expressions

                Regex.Matches(text, pattern)
                    .Cast<Match>()                                // Extract matches
                    .Select(match => match.Value.ToLower())       // Convert to lower case
                    .Where(word => wordBlacklist.Contains(word))  // Find in blacklist
                    .Any();                                       // Stop when first match found


        static void Main(string[] args)

            var message = new MimeMessage();


            message.Subject = "testing spam";


            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("", 587, SecureSocketOptions.StartTls);

                smtpclient.Authenticate("blah, sheesh");



                Console.WriteLine("message sent");

            catch(Exception ex)

            if (IsSpam(text, blacklist))

                Console.WriteLine("The message is spam");


                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.
Dave Kreskowiak 12-Dec-22 13:27pm    
Why are you creating an SmtpClient to send an email? Are you filtering out emails before they are being sent to their destination?

If that's the case, just don't send the email.

Spam filters usually filter incoming emails, not outgoing.
Member 15862460 12-Dec-22 13:36pm    
The idea was to send to my own account to test it, but it obviously doesn't work as intended since the message sent is always in my inbox, even though the console prints the message is spam.

If there are another way, please
Dave Kreskowiak 12-Dec-22 14:04pm    
First, hit the Reply button on the message you want to reply to, otherwise nobody gets a notification you replied to anything.

If you're trying to filter inbound email, you would need to write your own email client software to grab the emails from your box on the server, then process each email and sort them into the folders you want.

Using an SmtpClient to send the emails does you no good. You would have to use the GMail API[^] to get the messages and move them to folders, and even create the folders you need.
PIEBALDconsult 12-Dec-22 14:43pm    
Are you in the same class as
Gerry Schmitz 13-Dec-22 12:35pm

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900