![](/img/trans.png)
[英]ClosedXML Read excel file Error: The type initializer for 'MS.Utility.EventTrace' threw an exception. (C#)
[英]How do I fix the error “The type initializer for '?' threw an exception.” when sending e-mails through C# with EASendMail?
我一直在從事這個項目以向用戶發送電子郵件,並且一直出現錯誤。 (我正在使用 EASendMail NuGet 包)它是“'的類型初始化程序'? 拋出異常。” (這是在 package 具有的錯誤系統的控制台中)我真的不知道這意味着什么,我的代碼是這樣的:
using EASendMail;
namespace Email
{
class Program
{
static void Main(string[] args)
{
try
{
SmtpMail oMail = new SmtpMail("TryIt");
// Set sender email address, please change it to yours
oMail.From = "Splat2ooner@gmail.com";
// Set recipient email address, please change it to yours
oMail.To = "Splat2ooner@gmail.com";
// Set email subject
oMail.Subject = "test email from c# project";
// Set email body
oMail.TextBody = "this is a test email sent from c# project, do not reply";
// SMTP server address
SmtpServer oServer = new SmtpServer("smtp.gmail.com");
// User and password for ESMTP authentication
oServer.User = "Splat2ooner@gmail.com";
oServer.Password = "password (not my passoword)";
// Most mordern SMTP servers require SSL/TLS connection now.
// ConnectTryTLS means if server supports SSL/TLS, SSL/TLS will be used automatically.
oServer.ConnectType = SmtpConnectType.ConnectTryTLS;
// If your SMTP server uses 587 port
//oServer.Port = 587;
// If your SMTP server requires SSL/TLS connection on 25/587/465 port
//oServer.Port = 25; // 25 or 587 or 465
oServer.ConnectType = SmtpConnectType.ConnectSSLAuto;
Console.WriteLine("start to send email ...");
SmtpClient oSmtp = new SmtpClient();
oSmtp.SendMail(oServer, oMail);
Console.WriteLine("email was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send email with the following error:");
Console.WriteLine(ep.Message);
}
}
}
}
謝謝。
看起來較新版本的“ EASendMail ”(截至目前最新版本是7.5.0.3 )package 有這個問題。
我沒有太多時間研究它,只是安裝了舊版本( 7.3.1.2 )並解決了這個問題。
希望能幫助到你。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.