Link to home
Start Free TrialLog in
Avatar of Cyberzones
Cyberzones

asked on

Issue with Send Email Asynchronously with ASP.NET

Hello!

I am trying to add a page for sending email asynchronously to our web site.

My problem is the email does not send? You click the send button and the page just flickers and stays the same. No error, no nothing.

Any ideas?

Here is the code for the web.config:
<system.net>
    <mailSettings>
      <smtp>
        <network host="mailserver.com" port="587" userName="name@test.com" password="password" />
      </smtp>
    </mailSettings>
  </system.net>

New Page code:
<%@ Page Language="C#" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Net.Mail" %>
 
 <script language="C#" runat="server">
    
         protected void Page_Load(object sender, EventArgs e)
         {
 
         }
         protected void btnSend_Click(object sender, EventArgs e)
         {
             SendMail();
         }
 
         /// &lt;summary&gt;
         /// Sends email asynchronously.
         /// &lt;/summary&gt;
         private void SendMail()
         {
             System.Net.Mail.MailMessage mailMessage = new System.Net.Mail.MailMessage();
 
             mailMessage.From = new MailAddress("applicant@test.com");
             mailMessage.To.Add(new MailAddress("employer@test.com"));
 
             mailMessage.Subject = txtSubject.Text.Trim();
             mailMessage.Body = txtBody.Text.Trim();
 
             SmtpClient smtpClient = new SmtpClient();
             Object userState = mailMessage;
 
             //Attach event handler for async callback
             smtpClient.SendCompleted += new SendCompletedEventHandler(smtpClient_SendCompleted);
 
             try
             {
                 //Send the email asynchronously
                 smtpClient.SendAsync(mailMessage, userState);
             }
             catch (SmtpException smtpEx)
             {
                 //Error handling here
             }
             catch (Exception ex)
             {
                 //Error handling here
             }
         }
 
         /// &lt;summary&gt;
         /// Event handler for processing completion information after asynchronous email sent.
         /// &lt;/summary&gt;
         void smtpClient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
         {
 
             //Get UserState as MailMessage instance from SendMail()
             MailMessage mailMessage = e.UserState as MailMessage;
 
             if (e.Cancelled)
             {
                 labMessage.Text = "Sending of email message was cancelled. Address=" + mailMessage.To[0].Address;
             }
 
             if (e.Error != null)
             {
                 labMessage.Text = "Error occured, info=" + e.Error.Message;
             }
             else
             {
                 labMessage.Text = "Mail sent successfully";
             }
 
         }    
    
</script>
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
   <form id="form1" runat="server">
    <div>
     <p>
      <asp:TextBox ID="txtSubject" runat="server" Columns="50" />
    </p>
    <p>
      <asp:TextBox ID="txtBody" runat="server" TextMode="MultiLine" Rows="5" Columns="60" />
    </p>
    <p>
      <asp:Button ID="btnSend" runat="server" Text="Send Email" 
        onclick="btnSend_Click" /></p>
    <p>
      <asp:Label ID="labMessage" runat="server" /></p>
    </div>
    </form>
</body>
</html> 
Open in New Window Select All

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of brrrdog
brrrdog

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Have you tryed put some code in the catch blocks and setting breakpoints on those lines?
if you have an exception there its catched and ignored!

try
             {
                 //Send the email asynchronously
                 smtpClient.SendAsync(mailMessage, userState);
             }
             catch (SmtpException smtpEx)
             {
                 string s = smtpEx.ToString();
             }
             catch (Exception ex)
             {
                 string s = exToString();
             }
can you try after changing the user name to domainname\username in web config
<network host="mailserver.com" port="587" userName="domainName\UserName" password="password" />