Help with C# Send Mail

Status
Not open for further replies.
Level 22
Joined
Feb 3, 2009
Messages
3,292
Hello, I was coding a program to Send Mails, but for some reason it just skips the part to send the mail and goes to where it outputs "done", and the Try doesn't write any error either...

Any ideas?

Code:
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Ironside_Mail_Spammer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("                     ###########################");
            Console.WriteLine("                     ##                       ##");
            Console.WriteLine("                     ## Ironside Mail Spammer ##");
            Console.WriteLine("                     ##                       ##");
            Console.WriteLine("                     ########################### \n");

            string username, password, from, sender, to, subject, message, sep;
            int amount, i;
            SmtpClient client = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,

            };
            MailMessage msg = new MailMessage();
            Start:

            Console.Write(" Please Input your Username: ");
            username = Console.ReadLine();
            Console.Write(" Please Input your Password: ");
            password = Console.ReadLine();
            Console.Write(" Please Input a string for 'From': ");
            from = Console.ReadLine();
            if (from == "username") from = username;
            Console.Write(" Please Input a string for 'Sender': ");
            sender = Console.ReadLine();
            if (sender == "username") sender = username;
            Console.Write(" Please Input a string for 'To': ");
            to = Console.ReadLine();
            Console.Write(" Please Input the Subject: ");
            subject = Console.ReadLine();
            Console.Write(" Please Input the Message: ");
            message = Console.ReadLine();
            Console.Write(" Please Input the Seperator (default #): ");
            sep = Console.ReadLine();
            amount:
            try
            {
                Console.Write(" Please Input the amount of messages to send: ");
                amount = int.Parse(Console.ReadLine());
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n ## Bad Input ## \n");
                Console.ForegroundColor = ConsoleColor.Gray;
                goto amount;
            }

            try
            {
                msg.Priority = MailPriority.High;
                client.Credentials = new NetworkCredential(username, password);
                msg.From = new MailAddress(from);
                msg.Sender = new MailAddress(sender);
                msg.To.Add(new MailAddress(to));
                msg.Subject = subject;
                msg.Body = message;
            }
            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n ## Error, have you entered all data in the correct form? Please try again ## \n");
                Console.ForegroundColor = ConsoleColor.Gray;
                goto Start;
            }

            Send:
            try {

                for (i = 0; i >= amount; )
                {
                    client.Send(msg);
                    amount--;
                    i++;
                    Console.WriteLine("## Messages Send: " + i + " , Remaining: " + amount + " ##");
                }
            }

            catch
            {
                Console.ForegroundColor = ConsoleColor.Red;
                Console.WriteLine("\n ## Unable to Send message ## \n");
                Console.ForegroundColor = ConsoleColor.Gray;
                amount--;
                System.Threading.Thread.Sleep(1000);
                goto Send;
            }
            Console.WriteLine("done");
            Console.ReadLine();
        }
    }
}
 
Level 21
Joined
Dec 9, 2007
Messages
3,096
Code:
using System;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace Ironside_Mail_Spammer
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("                     ###########################");
            Console.WriteLine("                     ##                       ##");
            Console.WriteLine("                     ## Ironside Mail Spammer ##");
            Console.WriteLine("                     ##                       ##");
            Console.WriteLine("                     ########################### \n");

            string username, password, from, sender, to, subject, message, sep;
            int amount, i;

            SmtpClient client = new SmtpClient
            {
                Host = "smtp.gmail.com",
                Port = 587,
                EnableSsl = true,
                DeliveryMethod = SmtpDeliveryMethod.Network,
            };

            MailMessage msg = new MailMessage();

            while (true)
            {
                Console.Write(" Please Input your Username: ");
                username = Console.ReadLine();

                Console.Write(" Please Input your Password: ");
                password = Console.ReadLine();

                Console.Write(" Please Input a string for 'From': ");
                from = Console.ReadLine();
                if (from == "username") { from = username; }

                Console.Write(" Please Input a string for 'Sender': ");
                sender = Console.ReadLine();
                if (sender == "username") { sender = username; }

                Console.Write(" Please Input a string for 'To': ");
                to = Console.ReadLine();

                Console.Write(" Please Input the Subject: ");
                subject = Console.ReadLine();

                Console.Write(" Please Input the Message: ");
                message = Console.ReadLine();

                Console.Write(" Please Input the Seperator (default #): ");
                sep = Console.ReadLine();

                while (true)
                {
                    try
                    {
                        Console.Write(" Please Input the amount of messages to send: ");
                        amount = int.Parse(Console.ReadLine());

                        break;
                    }
                    catch
                    {
                        Console.ForegroundColor = ConsoleColor.Red;
                        Console.WriteLine("\n ## Bad Input ## \n");
                        Console.ForegroundColor = ConsoleColor.Gray;
                    }
                }

                try
                {
                    msg.Priority = MailPriority.High;
                    client.Credentials = new NetworkCredential(username, password);
                    msg.From = new MailAddress(from);
                    msg.Sender = new MailAddress(sender);
                    msg.To.Add(new MailAddress(to));
                    msg.Subject = subject;
                    msg.Body = message;

                    break;
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n ## Error, have you entered all data in the correct form? Please try again ## \n");
                    Console.ForegroundColor = ConsoleColor.Gray;
                }
            }

            while (true)
            {
                try
                {

                    for (i = 0; i < amount; i++)
                    {
                        client.Send(msg);
                        Console.WriteLine("## Messages Send: " + (i + 1) + " , Remaining: " + (amount - i) + " ##");
                    }

                    break;
                }
                catch
                {
                    Console.ForegroundColor = ConsoleColor.Red;
                    Console.WriteLine("\n ## Unable to Send message ## \n");
                    Console.ForegroundColor = ConsoleColor.Gray;
                    System.Threading.Thread.Sleep(1000);

                    Console.Clear();

                    Main(null);
                    return;
                }
            }

            Console.WriteLine("done");
            Console.ReadLine();
        }
    }
}

It works now.

Please, do not use labels! They are known for having many issues.

Edit: Do you understand my changes?
 
Status
Not open for further replies.
Top