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 anything@domainname.com
and the recipient is bob@domainname.com
, 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?
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.
Try this, it works 4 me.
MailMessage mail = new MailMessage();
mail.From = new MailAddress("mymail@mail.com");
mail.To.Add("anymail@mail.com");
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("mymail@mail.com", "anymail@mail.com");
client.EnableSsl = true;
client.Send(mail);
}
catch (Exception ex)
{
//If not than......
}
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!