繁体   English   中英

C# 发送 email

[英]Sending email in C#

我在一个页面上工作,我必须在 C# 中发送一个 email。我遵循了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());
}

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

答案更新 2023

谷歌在谷歌帐户中引入两步验证系统后,将 Gmail 用于个人用途并不容易,因此为了解决这个问题,我想出了一种将 Gmail 作为 email 媒介来使用 C# 发送电子邮件的方法。

此处包含 email 服务的 C# 代码: https://www.techaeblogs.live/2022/06/how-to-send-email-using-gmail.html

只需按照上述链接中的两步教程,您就可以立即解决问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM