从C#异步发送电子邮件 [英] send email asynchronously from C#
本文介绍了从C#异步发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试从c#异步发送邮件电子邮件,但是没有给出错误,也没有向给定的邮件ID发送电子邮件。这里我使用的是代码: -
在这段代码中,当我发送简单的邮件时,它工作正常。
I am trying to send mail email asynchronously from c#, but there is not giving error and also not sending email to the given mail id. Here what I am using code :-
In this code when I send simple mail it works fine.
public void SendMailAccro(int SlNo, string EmailId, string Body)
{
MailMessage mail = new MailMessage();
mail.From = new MailAddress("_____________", "___________", System.Text.Encoding.UTF8);
mail.To.Add(EmailId);
mail.Subject = "VOTING DETAILS";
mail.Body = Body;
mail.Priority = MailPriority.Normal;
mail.ReplyTo = new MailAddress("___________", "___________", System.Text.Encoding.UTF8);
slno = SlNo;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "__________________";
//smtp.EnableSsl = true;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "_______";
NetworkCred.Password = "_______";
//smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.Port = "___"
Object state = mail;
smtp.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
try
{
smtp.SendAsync(mail, "Name");
}
catch (SmtpException ex)
{
Console.WriteLine(ex.Message);
}
finally
{
mail.Dispose();
}
}
void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
MailMessage mail = e.UserState as MailMessage;
if (!e.Cancelled && e.Error != null)
{
//message.Text = "Mail sent successfully";
}
}
推荐答案
我为我的公司开发了一个电子邮件发件人,可以发送async和我说的fire and forget方法。我们通过交换服务器发送它。检查你的代码行,唯一的区别是我可以找到:
I developed a email sender for my company that can send both async and as i say fire and forget method. We send it through the exchange server. checking the lines of your code, the only difference i can find is that i define:
Properties.Settings settings = new DBPROG.Properties.Settings();
//to get our server settings
SmtpClient client = new SmtpClient(settings.smtp_server,settings.smtp_port);
client.DeliveryMethod = SmtpDeliveryMethod.Network;
.....
if (securesent)
{
client.SendCompleted += new SendCompletedEventHandler(MailDeliveryComplete);
client.SendAsync(mail, mail.To[0].ToString());
}
else
{
client.Send(mail);
CreateLogFiles SendLog = new CreateLogFiles();
SendLog.ErrorLog("C:\\","\t|Mail sended (Subject): "+mail.Subject.ToString()
+ " \t|TO: " + mail.To.ToString()
+ " \t|FROM: " + mail.From.ToString()
+ " \t|REPLYTO: " + mail.ReplyToList[0].ToString()
, "UNSECURE_SENDLOG", true);
}
此代码未给出错误。但是,电子邮件ID没有收到电子邮件。
This code is not giving error. But email not received given email id.
这篇关于从C#异步发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文