Click here to Skip to main content
15,905,068 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
When I send an email from c# I get a "relay access denied" error.
Using the same credentials from Thunderbird everything works fine.
I run my own mail server with Postfix and Dovecote.

Below is my code:
C#
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
    client.Host = "mail.mydomainname.nl";
    client.Port = 2525;
    client.Credentials = new System.Net.NetworkCredential("username@mydomainname.nl", "correctpassword");
    client.UseDefaultCredentials = false;

string Van = "username@mydomainname.nl";


System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage("username@mydomainname.nl", "somebody@hisdomainname.nl", "Subject of this mail", "Contents of this mail");

try
{
    client.Send(msg);
}
catch(Exception e)
{
    System.Windows.Forms.MessageBox.Show("Error sending email: \r\n" + e.Message );
    return false;
}

return true;


Thanks in advance,

Rob

Edit 2012-12-04:
I currently use the myNetworks entry in /etc/postfix/main.cf to add the IP from which I'm sending. This ain't ideal for several reasons so I'm still looking for a better solution.

Rob
Posted
Updated 3-Dec-12 23:23pm
v2

"Relay access denied" usually means the server you're using to send the email doesn't like the domain in the From field of the email. Are you trying to send an email as a user from a different domain than the SMTP server you're using to send it?? For example, using a Google SMTP server to send an email with a From address like someuser@yahoo.com.
 
Share this answer
 
v2
Comments
Sandeep Mewara 5-Aug-12 2:55am    
Comment from OP:
No, it's the same domain.
I use exactly the same data from thunderbird, and it works OK.
I feel it has something to do with authentication.

Rob
Hi dear,

Check this solution.

private void MailSend()
       {
           try
           {
               MailMessage mail = new MailMessage();
               SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
               SmtpServer.Host = "smtp.gmail.com";

               mail.From = new MailAddress("FromMailName@gamil.com");
               mail.To.Add("ToMailName@gmail.com");
               mail.Subject = "Test Mail";
               mail.Body = "This is for testing SMTP mail from GMAIL";

               SmtpServer.Port = 587;

               SmtpServer.Credentials = new System.Net.NetworkCredential("ToMailName@gmail.com", "123456"); // User Mail Name And Mail Password
               SmtpServer.EnableSsl = true;

               SmtpServer.Send(mail);
           }
           catch (Exception ex)
           {
               MessageBox.Show(ex.ToString());
           }
       }
 
Share this answer
 
Comments
RobScripta 4-Dec-12 7:59am    
I have my own mailserver, using Postfix as mail transfer agent. As my mobile internet provider blocks port 25 I have port 2525 enabled as well. Both in my mail client and in my C# programs I use port 2525. In my mail client (Thunderbird) everything works fine, but using C# the sender IP has to be configured in "myNetworks".

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900