在C#中发送电子邮件

时间:2015-02-02 11:13:27

标签: c# asp.net .net email smtp

我正在开发一个页面,我必须在C#中发送电子邮件。 我按照代码进行操作 http://blogs.msdn.com/b/mariya/archive/2006/06/15/633007.aspx 并且遇到了这两个例外

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

以下是我实施的代码。我似乎无法弄清楚出了什么问题。

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

2 个答案:

答案 0 :(得分:0)

出于共享目的,我设法通过启用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

答案 1 :(得分: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;
            }
        }