Sending email to a local domain

I have an easy way to send emails from a 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 anything@domainname.comand the recipient bob@domainname.com, emails get stuck in the Dropdirectory folder inetpub/mailrootand are never sent to the recipient.

Question: how can I get around this to send emails to people in the same (local) domain?

+4
source share
3 answers

I think this is almost certainly a mail server configuration problem.

SmptClient.Send() (Host, Credentials, Port, SSL, Firewall, Anti -Virus ..) InvalidOperationException SmtpException.

, , , , , .

, .

, . , , ( - ) . , ( ).

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

, , , .

+5

:

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);
}

, , , !

+2

, 4 .

            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......
            }
+2

Source: https://habr.com/ru/post/1679459/


All Articles