Click here to Skip to main content
15,884,937 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi Friends,
I have got small problem in sending email from my asp.net application. I have used the below code.

I have used for forgot password recovery. I have generated a random alphanumeric code and that code is sent to user who asked for forgot password and at the same time I am updating it in the data base so the next time after the user login with the user name and randomly generated password that I have sent will be checkd and if they are true user logins.

So I hosted my application in my server. but didnt make any mails from the server where I hosted. is it mandatory to know my server smtp details to send mail from my asp.ner applicaton.

and when I test my application I am getting an exception but the database is updating with the new password.

VB
cmd = New SqlCommand("Update Users Set Password='" + NewPassword.ToString + "' where UserId=" + result.ToString + "", con)
              cmd.ExecuteNonQuery()

              Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
              mailMessage.From = New System.Net.Mail.MailAddress("ganesh.cdm.47@gmail.com", "AVIT WebSite")

              mailMessage.To.Add(New System.Net.Mail.MailAddress(txtFPEmail.Text.Trim()))

              mailMessage.Priority = Net.Mail.MailPriority.High

              mailMessage.IsBodyHtml = False

              mailMessage.Subject = "Password Request from AVIT"
              mailMessage.Body = "You have requested for Password Reset. UserName" & txtFPUName.Text.Trim & " and your New Password is " & NewPassword.ToString & " Please use the above details for your LogIn."

I used the gmail because I didnot create any emnail for my site yet.
here s the code please let me know how to fix it.
              Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("smtp.gmailcom", 587)
              smtpClient.UseDefaultCredentials = False
              Dim networkCredientials As System.Net.NetworkCredential = New System.Net.NetworkCredential("ganesh.cdm.47@gmail.com";"mygmalpassword";)
              smtpClient.Credentials = networkCredientials
              smtpClient.Host = "smtp.gmail.com"
              smtpClient.Port = 587
              smtpClient.EnableSsl = False
              lblSuccess.Text = Session("ValidDetails")
              smtpClient.Send(mailMessage)



Thanks in Advance
Ganesh.
Posted
Comments
[no name] 27-Apr-12 14:01pm    
What is the exception? Are we supposed to guess?

You also need to learn about why you should NOT be using string concatenation for your SqlCommand and what using block is for.
ZurdoDev 27-Apr-12 20:23pm    
What is the error?
Ganesh KP 1-May-12 1:08am    
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 173.194.77.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at AVIT_WebSite.ForgotPassword.btnResetPassword_Click(Object sender, EventArgs e) in D:\AVIT_WebSite_25thApril\AVIT_WebSite\AVIT_WebSite\ForgotPassword.aspx.vb:line 43


This is my Error I got when I hosted the application in Server and Click the Forgot Password Button.
ZurdoDev 1-May-12 7:50am    
That means it can't connect to the server. Are you sure smtp.gmailcom should not be smtp.gmail.com?
Ganesh KP 1-May-12 9:20am    
Noah I have written smtp.gmail.com not smtp.gmailcom

//Code for sending a welcome mail for the registered user

C#
// add name space using System.Net;

var smtp = new System.Net.Mail.SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
smtp.Credentials = new NetworkCredential("fromaddress@gmail.com","password");
smtp.Timeout = 20000;
smtp.Send("fromaddress@gmail.com", "toaddress@gmail.com", "Test Mail", "Hi, This is Test Mail");
 
Share this answer
 
v3
Comments
Ganesh KP 30-Apr-12 10:38am    
Hi amraj, I have seen your code but my site is hosted in another server but I am sending mail from my gmail account does it supports this type of service that hosted in some other server and mail from my gmail account.
i am using this code for sending verification code to email and this works perfectly.....

MailAddress from = new MailAddress("xxxx@gmail.com", "abcd");
MailAddress to = new MailAddress(MailAddresstoSend); // enter the receiver mail address

MailMessage message = new MailMessage(from, to);
message.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
message.IsBodyHtml = true;
message.Priority = MailPriority.High;
message.Subject = "abcd";
message.Body = "abcdefgh ";

message.From = from;


SmtpClient mailClient = new SmtpClient();



mailClient.Host = "smtp.gmail.com";
mailClient.Port = 587;
mailClient.UseDefaultCredentials = false;
mailClient.EnableSsl = true;
NetworkCredential account = new NetworkCredential("xxxx@gmail.com", "password");

mailClient.Credentials = account;


mailClient.Send(message);
 
Share this answer
 
Comments
Ganesh KP 1-May-12 1:14am    
Hi Ahmed I have used the same code but I am still unable to send mail from my application, and I got the error which I comment above. Please check and if you know Please review and make a necessary corrections in my code.
MailMessage msg = new MailMessage();
msg.From = new MailAddress("arul44.ece@gmail.com");

msg.To.Add("ass@fgjf.com");//To recipt id

msg.Subject = "Record inserted";

msg.Body = "The record has been added";

System.Net.Mail.SmtpClient smtp = new SmtpClient("");//give smtp value

smtp.Send(msg);

//SmtpClient smt = new SmtpClient("");

//SmtpClient client = new SmtpClient();
//client.Host = "";
//client.Port = 25// port no

//client.Send(msg);
Response.Write("Mail Successfully sent");
 
Share this answer
 

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