在 C# 中发送电子邮件

Sending email in C#

我正在处理一个必须使用 C# 发送电子邮件的页面。 我遵循了代码 http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx 并遇到了这两个例外

'System.Net.Mail.SmtpException' 类型的第一次机会异常发生在 System.dll 中。类型的第一次机会异常 'System.Threading.ThreadAbortException' 发生在 mscorlib.dll

这是我实现的代码。我似乎无法弄清楚哪里出了问题。

//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());
}

出于共享目的,我设法通过在 Gmail 中启用对安全性较低的应用程序的访问来解决我的问题。 它现在就像一个魅力! https://www.google.com/settings/security/lesssecureapps

要在 Outlook 中验证 SMTP,下面的文章也很有用。 http://www.tradebooster.com/web-hosting-articles/how-to-enable-smtp-authentication-in-outlook-2010/

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

//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;
            }
        }