Reputation: 759
To send an email from my ASP.NET project using Visual Studio is very fast--it does in one second--but when published in IIS 7 on the same machine, it takes 50 seconds or more. Has anyone encountered this reduction in speed before? I've pasted my C# code and my settings in the web.config. Thank you very much.
public static bool EnviarMail(String eOrigen, String eDestino, String asunto, String cueMensaje)
{
Boolean EstadoEnvio;
MailMessage eMail = new MailMessage();
eMail.From = new MailAddress(eOrigen);
eMail.To.Add(new MailAddress(eDestino));
eMail.Subject = asunto;
eMail.IsBodyHtml = true;
cueMensaje = cueMensaje.Replace("\r\n", "<BR>");
eMail.Body = cueMensaje;
eMail.Priority = MailPriority.Normal;
SmtpClient clienteSMTP = new SmtpClient();
try
{
clienteSMTP.Send(eMail);
EstadoEnvio = true;
}
catch
{
EstadoEnvio = false;
}
return EstadoEnvio;
}
And in my web.config:
<mailSettings>
<smtp from="[email protected]">
<network host="174.120.190.6" port="25" userName="[email protected]" password="-----" defaultCredentials="true"/>
</smtp>
</mailSettings>
Upvotes: 4
Views: 2199
Reputation: 61
When sending an email in your ASP.NET application there are times when you do not want the user experience to slow just to wait for an email to be sent. The code sample below is how to send a System.Net.Mail.MailMessage asynchronously so that the current thread can continue while a secondary thread sends the email.
public static void SendEmail(System.Net.Mail.MailMessage m)
{
SendEmail(m, true);
}
public static void SendEmail(System.Net.Mail.MailMessage m, Boolean Async)
{
System.Net.Mail.SmtpClient smtpClient = null;
smtpClient = new System.Net.Mail.SmtpClient("localhost");
if (Async)
{
SendEmailDelegate sd = new SendEmailDelegate(smtpClient.Send);
AsyncCallback cb = new AsyncCallback(SendEmailResponse);
sd.BeginInvoke(m, cb, sd);
}
else
{
smtpClient.Send(m);
}
}
private delegate void SendEmailDelegate(System.Net.Mail.MailMessage m);
private static void SendEmailResponse(IAsyncResult ar)
{
SendEmailDelegate sd = (SendEmailDelegate)(ar.AsyncState);
sd.EndInvoke(ar);
}
To use this just call the SendEmail()
method with System.Net.Mail.MailMessage
object.
Upvotes: 1