Click here to Skip to main content
15,881,413 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i am trying to sent email via my asp.net application
the code i use is as follows:
VB
Dim msg As New MailMessage()
msg.From = fromaddr ' is a gmail address
msg.To.Add(toaddr) 
msg.Subject = subject
msg.Body = msgbody
Dim cred As New System.Net.NetworkCredential("myusername@gmail.com","mypassword")
Dim client As New SmtpClient()
client.Host ="smtp.gmail.com"
client.Port = 465
client.EnableSsl = True
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.UseDefaultCredentials = False
client.Credentials = cred

client.Send(msg)

when i run it the application goes into an indefinite loop.
if i set the port no as 587 i get an operation timeout error.
also if change the code into the following one:
VB
Dim msg As New MailMessage()
        msg.From = fromaddr
        msg.To.Add(toaddr)
        msg.Subject = subject
        msg.IsBodyHtml = True
        msg.Body = msgbody

        Dim client As New SmtpClient("localhost")

        client.Send(msg)



the mail is present in the C:\Inetpub\mailroot\Queue folder but does not reach the destination.

Note: my outlook is also configured for the gmail sever and works fine.

please help me detect the problem.

thanks in advance.
Posted

I reckon you need to use port 587. And if it times out, then there's a network issue there - possibly a firewall.
 
Share this answer
 
Comments
aruna_0990 27-Apr-11 1:24am    
so do i need to do some changes in the firewall or in the code???
can u suggest something...
thanx...
Albin Abel 27-Apr-11 12:36pm    
Sharp answer. 5
As rightly pointed by Nish, AFAIR, GMail uses 587 as the port.

If that does not work, then you need to look at them one by one.
Is the port open? Firewall permissions in place?
Further make sure you have configured SMTP configuration in Web.Config (It's simpler and helps):
<system.net>
   <mailSettings>
     <smtp from="abc@somedomain.com">
       <network host="somesmtpserver" port="25" userName="name" password="pass" defaultCredentials="true" />
     </smtp>
   </mailSettings>
</system.net>

If needed, have a look at this Microsoft Video tutorial:
Use ASP.NET to send Email from Website[^]
Tutorials on sending Email in ASP.NET[^]
 
Share this answer
 
Comments
aruna_0990 27-Apr-11 10:02am    
thanx it worked when i used 587..i increased the timeout seconds
Sandeep Mewara 27-Apr-11 10:05am    
Good to know! :)

Albin Abel 27-Apr-11 12:36pm    
Looks like Op forgot to vote. My 5 and 5 on behalf of OP
Sandeep Mewara 27-Apr-11 12:48pm    
:)
Well, forgot? I guess we both know by now that most of them move on once they get an answer that works.

Thanks for the 5.
Albin Abel 27-Apr-11 14:22pm    
Looks like there is a good response. :)
Take a look at this article[^], could give you and idea on what you might be doing wrong. At a glance, I am not able to find any issue with your code.
 
Share this answer
 
Comments
aruna_0990 27-Apr-11 1:22am    
hey i read the article but it doesn't solve my problem... thanx anyways

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