Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sending an email to the local domain

Tags:

c#

asp.net

smtp

I've got a simple method to send emails from the website:

... // local vars
using (var mail = new MailMessage(from, sendTo))
{
    using (var smtp = new SmtpClient())
    {
        mail.CC.Add(cc);
        mail.Bcc.Add(bcc.Replace(";", ","));
        mail.Subject = subject;
        mail.Body = body;
        mail.IsBodyHtml = html == -1;
        mail.Priority = priority;
        mail.BodyEncoding = mail.SubjectEncoding = mail.HeadersEncoding = Encoding.UTF8;                    

        smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;

        try
        {
            if (mail.Body.IsNotEmpty())
            {               
                smtp.Send(mail);                
            }
        }
        catch
        {
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;

            try
            {
                if (mail.Body.IsNotEmpty())
                {                   
                    smtp.Send(mail);                    
                }
            }
            catch(Exception e)
            {               
                // I log the error here
            }
        }
    }
}

Which works great; however, when the sender is [email protected] and the recipient is [email protected], the emails are getting stuck in the Drop folder of the inetpub/mailroot directory and never sent to the recipient.

The question is - how I can get around this to be able to send emails to people on the same (local) domain?

like image 553
Morpheus Avatar asked Jan 03 '23 20:01

Morpheus


1 Answers

I think it is almost certainly a mail server configuration issue.

According to the SmptClient.Send() Documentation any incorrect configuration on your end (Host, Credentials, Port, SSL, Firewall, Anti-Virus etc) should throw an InvalidOperationException or a SmtpException.

The fact that you can send external mail using the same code and config - which means that you have connectivity to the mail server also very strongly suggests that the problem lies downstream.

I have worked at companies in the past that had different mail servers for internal and external mail delivery.

It could be worth considering what Credentials are being used for the message. Perhaps you have a rule which only allows members of a group (All Company or something like that) to send mail to internals addresses. You could check this by using your real domain credentials (and removing them after the test)

smtp.Credentials = new NetworkCredential("your.username", "your.password");

Either way if no exception is generated the message should have been received on the mail server, and it is up to that server to determine delivery.

like image 175
ste-fu Avatar answered Jan 14 '23 06:01

ste-fu