Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello everyone......

i have created a simple windows application in c# in which i want to send an email.
i have a piece of code that i am going to post here.
i got an error "Failure Sending Mail".

Can any one please correct my code .

public void myEmail(string sendTo, string sendFrom, string subjct,string bdy, string pass)
        {   
            SmtpClient smtp = new SmtpClient("127.0.0.1",25);
            MailAddress to = new MailAddress(sendTo);
            MailAddress from = new MailAddress(sendFrom);
            MailMessage message = new MailMessage(sendFrom, sendTo);
            message.Subject = subjct;
            message.Body = bdy;
            smtp.Timeout = 50000;
            smtp.EnableSsl = true;
            smtp.Credentials = new System.Net.NetworkCredential(sendFrom, pass);
          
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
            smtp.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
            
            smtp.Send(message);
        }



i will be highly thankful to you if you correct this code
Posted
Updated 12-Aug-10 15:18pm
v2
Comments
[no name] 12-Aug-10 21:19pm    
Formatted code block. Please remember to do so in the future

There is nothing wrong with your code. I doubt your local machine (127.0.0.1) is an email server or relay.
 
Share this answer
 
Comments
haseebsvirgo 12-Aug-10 21:33pm    
its a relay
haseebsvirgo 12-Aug-10 21:33pm    
can you please fix my problem
#realJSOP 15-Aug-10 15:01pm    
Reason for my vote of 5
The answer
No one is going to fix your issues, but you can try

1. put your code in try / catch block and see the exception you are getting
2. Check that your machine has smtp configured right
3. Make sure you can 'pass-through' to your mail server.
 
Share this answer
 
Comments
haseebsvirgo 13-Aug-10 0:56am    
sir i have tried every thing but still the problem continuous
ok you tell me what type of smtp configuration is required.
is host = 127.0.0.1 ?
port = 25 ?
what rely and access settings would be required?
plz help me sir
SmtpClient smtp = new SmtpClient("127.0.0.1",25);

Is your local machine is the smtp host? How can you make sure that?
 
Share this answer
 
I think your localhost(might be IIS) is not configured properly to send mails.

Check this
http://service1.symantec.com/support/ent-gate.nsf/docid/2007241920754398[^]
Why dont you try to send mails from your google account, or any external email server which you use.

You can see
How to Send Mails from your GMAIL Account through VB.NET or C#. Windows Programming, with a Bit of Customization[^]

:rose:
 
Share this answer
 
Put a try/catch block around the code in the method, and look at the actual exception being thrown.

As mark said, your local machine probably doesn't have an email server running on it, and most ISPs don't allow relays. Use a mail server that you actually have an account on.
 
Share this answer
 
Your code is correct. I can't find any problems in that. You just need to enable SMTP port (default 25) of your computer or use any other computer on network with SMTP ports open. Usually you can open access to SMTP port through Firewall settings or you can google for the same.

BTW, you can even use your exchange server as SMTP for sending e-mails (if you have Outlook configured with your corporate e-mail account).
 
Share this answer
 
Comments
haseebsvirgo 16-Aug-10 3:46am    
From the last 3 days i had goolge on this problem . Try to configure the smtp but in vein.
How to properly configur it please tell me.....
Also tell me whats the fully qualified domain , masquarede domain and smart host in smtp? what to enter in these fields

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