Using gmail smtp server for emails in asp.net web site in IIS 6.0
I've uploaded my asp.net web site to IIS 6.0, and it's currently working, except for sending e-mails. In my asp.net test server, I was able to use gmail as an smtp server for outgoing e-mails from my gmail account.
In the IIS live server version, when I click the button control that is supposed to send the message, the loading bar of the browser only reaches 25% and then stops, no error or message.
The following is my code for the button click event that sends the e-mail.
private void sendUsername()
{
/**
* Sends user's username to
* their listed e-mail address.
*
*/
string username = Session["retrievedUsername"].ToString();
string usernameSent = "usernameSent";
string from = "mandizi84@gmail.com";
string to = enterEmailTextBoxTwo.Text;
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from, "The All-Star R.E.C. Center", System.Text.Encoding.UTF8);
mail.Subject = "ASRC password retrieval.";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
string htmlBody = "Hi " + username + "," + "<br /><br />";
htmlBody += "Your username is " + "'" + username + "'" + "." + "<br /><br />";
htmlBody += "If you don't remember your password, return to the Credential Recovery section to get a new password." + "<br /><br />";
htmlBody += "Your membership is much appreciated." + "<br /><br />";
htmlBody += "Thank you," + "<br /><br/>";
htmlBody += "The All-Star R.E.C. Center";
mail.Body = htmlBody;
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.Normal;
SmtpClient client = new SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from, "password");
c开发者_如何转开发lient.Port = 587;
client.Host = "smtp.gmail.com";
client.EnableSsl = true;
try
{
client.Send(mail);
Session["usernameSent"] = usernameSent;
Response.Redirect("~/Email Confirmation.aspx", false);
}
catch (Exception ex)
{
Exception ex2 = ex;
while (ex2 != null)
{
errorLabel.Text = "Mail could not be sent. ";
errorLabel.Text += ex2.ToString();
}
}
}
I did some research and read in a few places that configuring the mail server role is not necessary if I use gmail account, but in other places I did.
So my question is in IIS, do I need to configure the mail server role and then add the gmail smtp to use that smtp service in my live website on IIS 6.0?
Thanks
Update: I just removed the while loop from my catch block and received the following error:
Mail could not be sent. System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 74.125.95.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 Credential_Recovery.sendUsername() in e:\Senior Design\Second Version\ASRC\Credential Recovery.aspx.cs:line 619
line 619 in my code is "client.Send(mail)"
Dont you think the While loop in your Catch block is going into an infinite loop, thus not showing you the actual error?if therez an Exception, ex2 will never be null. its like a while(1)
statement!!
精彩评论