.net 用C#发送电子邮件

zpgglvta  于 2022-12-05  发布在  .NET
关注(0)|答案(3)|浏览(221)

我正在一个页面上工作,我必须用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());
}
kuarbcqp

kuarbcqp1#

出于共享的目的,我设法解决了我的问题,通过启用访问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

u0sqgete

u0sqgete2#

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

hxzsmxv23#

2023年答案更新

在Google在Google帐户中引入了两步验证系统后,要将Gmail用于个人用途并不容易,因此为了解决这个问题,我想出了一种方法,使用Gmail作为电子邮件媒介,使用C#发送电子邮件。
电子邮件服务的C#程式码包含在这里:******
只需按照上述链接中的2步教程,您就可以立即解决问题。

相关问题