Question

We have two versions of a web application running in IIS 7.5 on Windows Server 2008R2, with almost identical running conditions. Both versions are using the same set of email code to relay smtp messages to an internal Exchange2010 server. The first application completes the request, and sends the email without a hitch. However, the second, which uses a custom security model (being the only major difference between the apps), will not even initiate an smtp request at all. There is code to handle exceptions from SMTPClient.send, but none are occurring. Both versions send mail from development machine running with visual studio application server.

Wireshark shows complete request and verification of credentials for application one, but no traffic is detected at all from application two. The virtual running the IIS instance has it's IP address accepted by the Exchange server, and is on the same domain etc. The firewall is turned off for the domain.

UseDefaultCredentials is being implemented for authentication of smtp messages. Each application is running inside a separate app pool, but are using the same user (NetworkService) and have identical security permissions on the wwwroot etc.

Any insight is appreciated as to why the one application is seemingly doing nothing when using .Net Mail.SMTPClient on the IIS 7.5 server.

Code in a nutshell is as follows (ignore possible method signature mismatches, code is in different classes, default SMTP host is defined by Email.Host)

protected void taskHeaderGrid_OnItemCommand(object sender, GridCommandEventArgs e)
{
    try {
        Int32 recordsAcivated = workFlowController.activateWorkFlow((IDbConnection)myConn, null, System.Convert.ToInt32(taskHdrIdTxt));
    }
    catch (Exception) {
        AddMessage(Message.eMessageType.Error, "Warning: email message to alert users that next task has been activated was not sent.");
    }
}    

public Int32 activateWorkFlow(IDbConnection currConnection, IDbTransaction currTran, Int32 workFlowId)
{
        //Send out the mails......
        if (!string.IsNullOrWhiteSpace(primaryPersonEmail))
        {
            emailController.sendMessage(primaryPersonEmail, "Task has been activated.", string.Format("Hdr:({0}) Detail:({1})", taskHeaderDTO.Description, jobFunctionDTO.JobDescription));
        }
        if (!string.IsNullOrWhiteSpace(secondaryPersonEmail))
        {
            emailController.sendMessage(secondaryPersonEmail, "Task has been activated.", string.Format("Hdr:({0}) Detail:({1})", taskHeaderDTO.Description, jobFunctionDTO.JobDescription));
        }
}

public void sendMessage(string toEmailAddr, string txtmessage, string subject)
{  
    Email.Host = hostName;
    Email.Send(subject, txtmessage, senderEmailAddr, toEmailAddr);
}

public static void Send(string subject, string body, string from, IEnumerable<string> to, string smtphost)
{
    Send(CreateMessage(subject, body, from, to), smtphost);
}

public static void Send(System.Net.Mail.MailMessage msg, string smtphost)
{
    using (System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(smtphost))
    {
        if (InterceptEmails == true)
        {
            ApplyFailSafe(msg);
        }

        client.UseDefaultCredentials = true;
        client.Send(msg);
    }
}

public static System.Net.Mail.MailMessage CreateMessage(string subject, string body, string from, IEnumerable<string> to, IEnumerable<string> cc, IEnumerable<string> bcc)
{
    System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
    msg.Subject = subject;
    msg.Body = body;
    msg.From = new System.Net.Mail.MailAddress(from);
    msg.IsBodyHtml = true;

    if (to != null)
    {
        foreach (string email in to)
            msg.To.Add(email);
    }

    if (cc != null)
    {
        foreach (string email in cc)
            msg.CC.Add(email);
    }

    if (bcc != null)
    {
        foreach (string email in bcc)
            msg.Bcc.Add(email);
    }

    return msg;
}
Was it helpful?

Solution 2

Issue was resolved, a higher level configuration and error before the SMTP email was being generated caused the issue.

OTHER TIPS

I've created an example method for you, that will attempt to send an e-mail. Here are a couple of items to note about the implementation:

  • Settings Class - It holds all of our Client / Server data. Which helps keep the code decoupled for re-usability.

    public static void SendNotificationEmail(Settings setting) { // Loop through our generic list. foreach(string email in setting.To) { // Assign our email parameters to the message. MailMessage message = new MailMessage(setting.From, email, setting.Subject, setting.Body);

          //Build Our Smtp Client
          SmtpClient client = new SmtpClient();
          client.Host = setting.SmtpServer;
          client.Port = setting.Port;
          client.Timeout = setting.Timeout;
          client.DeliveryMethod = SmtpDeliveryMethod.Network;
          client.UseDefaultCredentials = false;
          client.Credentials = new NetworkCredential(setting.Username, setting.Password);
     }
    

    }

In order to truly help you we would need that Security Model and the precise code that is giving you trouble. Hopefully my implementation might contain a missing piece. This works on my server running the latest version of Internet Information System (IIS).

Also SMTP may fail silently without valid credentials.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top