SMTP authentications problem in c#

Last post 10-07-2008 4:52 AM by kdevendra. 3 replies.

Sort Posts:

  • SMTP authentications problem in c#

    10-02-2008, 12:54 PM
    • Member
      30 point Member
    • kirilminev
    • Member since 06-12-2007, 12:20 PM
    • Posts 212

     I am trying to send email from my .net console app and my app blows up with an error "rely denied"

     I know that my email provider uses smtp authentications and I am trying to authenticate to server, but for some reason I am not able to.

    I posted this issues multiple times unfortunately still haven't found resolution to it. I am able to send email withing my internal domain, but nothing externally.

     

    I looked at some post where people suggest I use the following syntax and add filed to my mail message, but I am not able to come up with the proper syntax e.g. mail.Fields.Add("dsdsdsds");

     

     

    Here is the code I am using to send an email: 

     

     System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage("xxx@localdomain.com", "xxxx@externaldomain.com", "test", "body");
    
                mail.IsBodyHtml = true;
    
                System.Net.Mail.SmtpClient exchange = new System.Net.Mail.SmtpClient("Smtp.host.net");
    
                exchange.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
    
                exchange.Credentials = new System.Net.NetworkCredential("xxxx", "xxxx");
    
    
    
                exchange.Send(mail);

      

     

    Any help will be greatly appreciated.

     

    Thanks

  • Re: SMTP authentications problem in c#

    10-02-2008, 7:22 PM
    • Member
      148 point Member
    • Jones0878
    • Member since 02-06-2007, 1:27 PM
    • Posts 24

    I normally also include the following line of code. .....

     exchange.UseDefaultCredentials = false;

     

    Hope this helps!

    Cheers,
    AJ
    MCTS,OCA
    ProgrammersEdge.com
  • Re: SMTP authentications problem in c#

    10-07-2008, 1:37 AM
    Answer

    Hi kirilminev,

    Possible Causes:
    1) The SMTP server does not allow relaying unless you are sending email from a trusted IP or use proper authentication. 
    2) Microsoft Exchange server: server is setup to block smtp relaying.
    3) Some servers allow you to send email internally but not to external email addresses.  
    4) Some SMTP servers require the Email User to login and check their POP3 email atleast once on the given IP before they let that user send SMTP mail from that IP.  Once the POP3 email is checked using a valid user/pwd, the computers IP is then automatically added to a safe list on the server. This is another way to prevent spamming.
    5) Relaying denied is a common safety measure used by SMTP servers when a certain email address & its mail server are put on a blacklist for email relay spam
    violations. Many honest mail servers get put on this list because someone broke through and used their server to relay spam. There are various blacklists available and SMTP servers may subscribe to none, a few or many of these lists. Hence you may be able to send email to a blacklisted address through one SMTP server but not through another.

    Possible solutions:
    1.  Make sure with your email administrator that your email server is setup to allow smtp relaying from trusted local internal IP addresses.
    2.  Make sure that your computers IP address is on the list of trusted clients on the email server.  
    3.  Some email servers may allow relaying only if you log in and check email atleast once from a given IP address.  In this case, you can check your POP3 email first before sending SMTP email from a new computer.
    4.  Some email servers allow smtp relaying only if you log in and check email within the last 1 hour/day etc... In this case, you should use the pop3 authentication method to send email.
    5.  Some servers require you to authenticate before sending email.  In this case, use the smtp or java authentication methods.
    6.  Microsoft Exchange: make sure that your exchange server is configured to allow smtp relaying either for trusted IP addreses, or for users that are authenticated.
    7.  Make sure that your email administrator has configured your ip address / account to send internal and external email.

    Here is the link, please check it.

    http://www.hiteksoftware.com/knowledge/articles/026.htm

    Sincerely,
    Hua Jun Li
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: SMTP authentications problem in c#

    10-07-2008, 4:52 AM
    Answer
    • Member
      368 point Member
    • kdevendra
    • Member since 09-24-2008, 2:51 AM
    • Pune
    • Posts 79

    use following code to send mail using system.net.mail;

    MailMessage mail= new MailMessage();

    mail.To.Add(receiver1@externaldomain.com);  //add receiver's email address here

    mail.To.Add(receiver2@externaldomain2.com);

    mail.From=new MailMessage(sender@domain.com);

    mail.Subject="Subject";

    mail.Body="hello, this is test mail";

    mail.IsBodyHtml=true;

    SmtpClient client= new SmtpClient("smtp.example.com");

    client.Credentials=new NetworkCredential(test@example.com, "password");  //give a user name and password for authentication on server

    client.Send(mail);

     

     

    K. Devendra

    It's hard enough to find an error in your code when you're looking for it;
    It's even harder when you've assumed your code is error-free.

Page 1 of 1 (4 items)