【问题标题】:Sending email in C#在 C# 中发送电子邮件
【发布时间】:2015-04-01 07:47:15
【问题描述】:

我正在处理一个必须用 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());
}

【问题讨论】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:
    //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;
                }
            }
    

    【讨论】:

      猜你喜欢
      • 2016-04-17
      • 2012-07-27
      • 2018-07-24
      • 2020-05-23
      • 2013-06-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多