Connecting Tech Pros Worldwide Help | Site Map

Problem sending mails

ThatThatGuy's Avatar
Member
 
Join Date: Jul 2009
Location: Mumbai--India
Posts: 39
#1: Aug 28 '09
Hello reader,,
This might be a very simple question, but what to just say about it, i'm facing it

Actually i'm developing a web App and i want email feature in that....
I'm sending mail from my development machine i.e localhost

This is the setting in web.config
Expand|Select|Wrap|Line Numbers
  1. <system.net>
  2.       <mailSettings>
  3.  
  4.         <smtp>
  5.           <network host="localhost" port="25" userName="reverso@gmail.com" password="somepassword" />
  6.         </smtp>
  7.       </mailSettings>
  8.     </system.net>
  9.  
and this is the code to send the email
Expand|Select|Wrap|Line Numbers
  1.         try
  2.         {
  3.             MailMessage mail = new MailMessage("from", "to","Some subject", "Mail Body");
  4.             SmtpClient sm = new SmtpClient("localhost", 25);
  5.             sm.Send(mail);
  6.             Page.Controls.Add(new LiteralControl("<script language='javascript'> alert('Mail Sent Successfully'); </script>"));
  7.  
  8.         }
  9.         catch (Exception rr)
  10.         {
  11.             Page.Controls.Add(new LiteralControl("<script language='javascript'> alert('" + rr.Message + "'); </script>"));
  12.         }
  13.  
Actually im facing problem sending mail plz someone verify this code
whether is it proper or not...
I'm getting failure sending mail error every time....
Plz help me with this it's urgent....
ssnaik84's Avatar
Member
 
Join Date: Aug 2009
Location: Bengaluru, India
Posts: 119
#2: Aug 31 '09

re: Problem sending mails


- is mail server configured on your localhost?
- whats the exception?
- new MailMessage("from", "to" ......... "from", "to" should be email address not normal string
Frinavale's Avatar
Site Moderator
 
Join Date: Oct 2006
Location: The Great White North
Posts: 5,066
#3: Aug 31 '09

re: Problem sending mails


Quote:

Originally Posted by ThatThatGuy View Post

Hello reader,,
This might be a very simple question, but what to just say about it, i'm facing it

Actually i'm developing a web App and i want email feature in that....
I'm sending mail from my development machine i.e localhost

This is the setting in web.config

Expand|Select|Wrap|Line Numbers
  1. <system.net>
  2.       <mailSettings>
  3.  
  4.         <smtp>
  5.           <network host="localhost" port="25" userName="reverso@gmail.com" password="somepassword" />
  6.         </smtp>
  7.       </mailSettings>
  8.     </system.net>
  9.  
and this is the code to send the email
Expand|Select|Wrap|Line Numbers
  1.         try
  2.         {
  3.             MailMessage mail = new MailMessage("from", "to","Some subject", "Mail Body");
  4.             SmtpClient sm = new SmtpClient("localhost", 25);
  5.             sm.Send(mail);
  6.             Page.Controls.Add(new LiteralControl("<script language='javascript'> alert('Mail Sent Successfully'); </script>"));
  7.  
  8.         }
  9.         catch (Exception rr)
  10.         {
  11.             Page.Controls.Add(new LiteralControl("<script language='javascript'> alert('" + rr.Message + "'); </script>"));
  12.         }
  13.  
Actually im facing problem sending mail plz someone verify this code
whether is it proper or not...
I'm getting failure sending mail error every time....
Plz help me with this it's urgent....


In line 3 of your C# code you should be passing it the email address (maybe the one stored in the web.config) as the From parameter.

You also need to pass a valid email address as the To parameter.

If you're using the gmail account to send email messages then you're going to have to use a NetworkCredential Object to provide the credentials for your gmail account.

Check out this Quick Reference on how to send an email using .NET for more information.
Reply