0

I am working on a page where I have to send an email in C#. I followed the codes on http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx and came upon this two exceptions

A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll. A first chance exception of type 'System.Threading.ThreadAbortException' occurred in mscorlib.dll

Here are the codes I implemented. I can't seem to figure what went wrong.

//Send email notification - removed actual email for this question

SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
client.EnableSsl = true;

MailAddress from = new MailAddress("myemail@gmail.com", "My name is here");
MailAddress to = new MailAddress("anotherpersonsemail@gmail.com", "Subject here");

MailMessage message = new MailMessage(from, to);
message.Body = "Thank you";
message.Subject = "Successful submission";

NetworkCredential myCreds = new NetworkCredential("myemail@gmail.com",         
"mypassword", "");

client.Credentials = myCreds;
try
{
  client.Send(message);
  Console.Write(ex.Message.ToString());

}

catch (Exception ex)
{
  Console.Write(ex.Message.ToString());
}
Enovyne
  • 195
  • 1
  • 3
  • 20

2 Answers2

0

For sharing purposes, I managed to resolve my problem by enabling access for less secure apps in Gmail. It works like a charm now! https://www.google.com/settings/security/lesssecureapps

To authenticate SMTP in Outlook, the articles below are very useful too. http://www.tradebooster.com/web-hosting-articles/how-to-enable-smtp-authentication-in-outlook-2010/

https://www.authsmtp.com/outlook-2010/default-port.html

Enovyne
  • 195
  • 1
  • 3
  • 20
0
//bulk Emails using mailkit you have to import it by nuget manager 

//set in gmail https://myaccount.google.com/lesssecureapps?pli=1 to be on


// Read Text File

        public void ReadFileAndSend()
        {
            using (StreamReader reader = new StreamReader(@"d:\Email.txt"))
            {
                while (!(reader.ReadLine() == null))
                {
                    String line = reader.ReadLine();
                    if (line != "")
                    {
                        try
                        {
                            Send("", line.Trim());
                            Thread.Sleep(500);
                        }
                        catch
                        {

                        }
                    }

                }
                Console.ReadLine();
            }
        }



        public void Send(String FromAddress,String ToAddress)
        {
            try
            {

                string FromAdressTitle = "";

                string ToAdressTitle = "";
                string Subject = "";
                string BodyContent = "";
                string SmtpServer = "smtp.gmail.com";
                int SmtpPortNumber = 587;

                var mimeMessage = new MimeMessage();
                mimeMessage.From.Add(new MailboxAddress(FromAdressTitle, FromAddress));
                mimeMessage.To.Add(new MailboxAddress(ToAdressTitle, ToAddress));
                mimeMessage.Subject = Subject;
                mimeMessage.Body = new TextPart("html")
                {
                    Text = BodyContent

                };

                using (var client = new MailKit.Net.Smtp.SmtpClient())
                {

                    client.Connect(SmtpServer, SmtpPortNumber, false);
                    client.Authenticate("your email", "pass");
                    client.Send(mimeMessage);
                    Console.WriteLine("The mail has been sent successfully !!");
                    client.Disconnect(true);

                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }