C# 用C语言发送电子邮件
我在一个页面上工作,我必须用C语言发送电子邮件。 我按上面的密码做 这有两个例外 System.dll中发生类型为“System.Net.Mail.SmtpException”的第一次意外异常。类型的第一次机会例外 mscorlib.dll中出现“System.Threading.ThreadAbortException” 以下是我实现的代码。我似乎不知道出了什么问题C# 用C语言发送电子邮件,c#,asp.net,.net,email,smtp,C#,Asp.net,.net,Email,Smtp,我在一个页面上工作,我必须用C语言发送电子邮件。 我按上面的密码做 这有两个例外 System.dll中发生类型为“System.Net.Mail.SmtpException”的第一次意外异常。类型的第一次机会例外 mscorlib.dll中出现“System.Threading.ThreadAbortException” 以下是我实现的代码。我似乎不知道出了什么问题 //Send email notification - removed actual email for this questi
//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中不太安全的应用程序的访问。 现在它就像一个符咒 要在Outlook中验证SMTP,下面的文章也非常有用。
异常消息是什么?参考@ArunprasanthKV我之前参考了该问题中的代码,但仍然出现相同的异常。检查这些,我认为此链接有一个工作示例异常消息是清楚的。您需要验证:
//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;
}
}