p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning ASP.NET 4 : in C# and VB (http://p2p.wrox.com/forumdisplay.php?f=560)
-   -   SMTP EMAIL NOT WORKING (http://p2p.wrox.com/showthread.php?t=87760)

Rajasekar1988 June 14th, 2012 08:23 AM

SMTP EMAIL NOT WORKING
 
Hi Imar,

I'm currently in the excercise how to send email from ur web page.
The Specifiedpickupdirectory is working fine bt sending from mail to mail not working as it says timeout expired.

As i've already searched the forums and couldn't able to get a solution. Another guy is also having the same problem as i in which he also needs solution. But he's having a different error. so please help me to get a solution.

Here's my code

Code:


Email.aspx.cs
-------------

protected void Page_Load(object sender, EventArgs e)
    {
        MailMessage myMessage = new MailMessage();
        myMessage.Subject = "Text Message";
        myMessage.Body = "Hello World, Welcome To Planet Wrox";
        myMessage.From = new MailAddress("my@gmail.com", "Developer");
        myMessage.To.Add(new MailAddress("my@gmail.com", "Developer"));

        SmtpClient mySmtpClient = new SmtpClient();
        mySmtpClient.Send(myMessage);
    }


Web.config
-----------

<?xml version="1.0"?>

<configuration>

  <system.web>
        <compilation debug="true" targetFramework="4.0" />
      <pages theme ="Monochrome">
        <controls>
          <add tagName="Banner" tagPrefix="Wrox" src ="~/Controls/Banner.ascx"/>
        </controls>
      </pages>
    </system.web>
  <system.net>
  <mailSettings>
    <!--<smtp deliveryMethod ="SpecifiedPickupDirectory">
      <specifiedPickupDirectory pickupDirectoryLocation ="c:\TempMail" />-->
    <smtp deliveryMethod="Network" from="my@gmail.com">
      <network enableSsl ="true" userName="myusername" password="mypassword" host="smtp.gmail.com" port="465"/>
    </smtp>
   
    <!--<smtp deliveryMethod="Network" from="&lt;myemail&gt;">
      <network enableSsl="true" userName="username" password="pwd" host="smtp.gmail.com" port="587"/>
    </smtp>-->
  </mailSettings>
  </system.net>
</configuration>


Ian Ryder June 14th, 2012 08:54 AM

Hi Rajasekar,

I have had this problem before and from what I can see from your web.config file you have actually commented out the correct solution.

With gmail you must connect to port 587 and not 465 to send mail via smtp.

Hope this helps.

Kind regards,

Ian Ryder.

Rajasekar1988 June 15th, 2012 12:27 AM

I commented those lines because it's not worked properly. that's y i commented it

Imar June 15th, 2012 02:36 AM

I agree with Ian: the code you commented out should work. A few things to check:

1. Use your full Gmail user name. That is, not userName, but userName@gmail.com

2. Make sure your network is allowed to reach smtp.gmail.com over port 587. Maybe your network administrator doesn't allow traffic to go over that port?

3. Take a look at this:

from="&lt;myemail&gt;"

The from attribute should contain at least a valid e-mail address; not just your name. This means you need to remove the < and > symbols.

Imar

Rajasekar1988 June 15th, 2012 03:27 AM

Hi Imar,
I did exactly what Ian told. I used port 587. but now i'm getting this error.

Server Error in '/' Application.
--------------------------------------------------------------------------------

The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Net.Mail.SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at

Source Error:


Line 30:
Line 31: SmtpClient mySmtpClient = new SmtpClient();
Line 32: mySmtpClient.Send(myMessage);
Line 33: }
Line 34: }


Source File: c:\Users\Developer\Documents\Visual Studio 2010\WebSites\WebSite1\Demos\Email.aspx.cs Line: 32

Stack Trace:


[SmtpException: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at]
System.Net.Mail.MailCommand.CheckResponse(SmtpStat usCode statusCode, String response) +1209995
System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, String from) +41
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) +97
System.Net.Mail.SmtpClient.Send(MailMessage message) +1772
Demos_Email.Page_Load(Object sender, EventArgs e) in c:\Users\Developer\Documents\Visual Studio 2010\WebSites\WebSite1\Demos\Email.aspx.cs:32
System.Web.Util.CalliHelper.EventArgFunctionCaller (IntPtr fp, Object o, Object t, EventArgs e) +14
System.Web.Util.CalliEventHandlerDelegateProxy.Cal lback(Object sender, EventArgs e) +35
System.Web.UI.Control.OnLoad(EventArgs e) +91
System.Web.UI.Control.LoadRecursive() +74
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2207

And this is my new Web.config file settings
-----------------------------------------
<system.net>
<mailSettings>
<smtp deliveryMethod="Network" from="my@gmail.com">
<network enableSsl ="true" userName="my@gmail.com" password="pwd" host="smtp.gmail.com" port="587"/>
</smtp>
</mailSettings>
</system.net>

Please help me to resolve this issue

Ian Ryder June 15th, 2012 04:08 AM

Hi Rajasekar,

Your web.config file looks fine.

Since I use gmail myself, to test your snippet, I copied your web.config code into my own web.config file and changed the "from", "username" and "password" to my own credentials. The email sent fine?

Therefore, this would imply that your code is correct but there is something wrong with the username and/or password. To test your credentials go to www.gmail.com and log in to your account. If you can login then these are the credentials you need to use in the web.config file.

Hope that helps, but if not, I will need to leave it to the master (Imar) to guide you further.

Good Luck and Kind regards,

Ian

Imar June 15th, 2012 05:23 AM

You may also want to look at the suggestions made here: http://stackoverflow.com/questions/7...r-with-c-sharp

Some relate to anti-virus software and others to changing your password. One of them might have the answer for you....

Imar

Rajasekar1988 June 15th, 2012 05:26 AM

Hi Ian,
Yes my web.config setting is correct. I checked it with my hotmail account. its working fine and i could able to send email.

I verified my account details in gmail it works fine.

but when i use my gmail account details in web.config only the error which i posted last is popping up.

so its not working for gmail only.

Please help me to get a soultion..


All times are GMT -4. The time now is 08:45 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.