Morpheus
Morpheus

Reputation: 9055

Sending an email to the local domain

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?

Upvotes: 4

Views: 3773

Answers (3)

ste-fu
ste-fu

Reputation: 7434

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.

Upvotes: 5

Simon
Simon

Reputation: 88

I made a tool for sending email`s with this configurations:

MailMessage mail = new MailMessage("senderaddress", "recipientaddress");
SmtpClient client = new SmtpClient()
{
    Host = "smtpserver",
    Port = 587,//standard port for the most of the server`s
    DeliveryMethod = SmtpDeliveryMethod.Network,
    EnableSsl = true,
    UseDefaultCredentials = false//I got the address and the password so I don`t need the default-credentials
};
NetworkCredential na = new NetworkCredential("yoursender", "senderpassword");//I let the user input his email-address and password to get auntenthificated 
client.Credentials = na;
mail.Subject = "subject";

mail.Body = "body";
mail.IsBodyHtml = true;//I use to send html formated text

try
{
    client.Send(mail);
}
catch (Exception)//for debugging purposes I used the default exception
{
    MessageBox.Show("Sending failed", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

It worked fine for me, when I wanted to send from one domain to the same host, so I hope it works also fine for you!

Upvotes: 2

Kevin
Kevin

Reputation: 71

Try this, it works 4 me.

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress("[email protected]"); 

                mail.To.Add("[email protected]"); 

                mail.To.Add(empfaenger.Text);

                mail.Subject = "Betreff Text";

                mail.Body = "body Text";

            SmtpClient client = new SmtpClient("smtp", "port");

            client.UseDefaultCredentials = false;

            try
            {
                client.Credentials = new System.Net.NetworkCredential("[email protected]", "[email protected]");

                client.EnableSsl = true;

                client.Send(mail);
            }
            catch (Exception ex)
            {
                //If not than......
            }

Upvotes: 2

Related Questions