|
C# 2005 For discussion of Visual C# 2005. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the C# 2005 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
November 20th, 2009, 05:52 AM
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Problem sending mail after publishing site
Hi all,
This code works perfect at local but after publishing the site mail is not coming to the user. plzzz help
Code:
try
{
string getattach;
string mailServer = ConfigurationManager.AppSettings["MailServer"].ToString();
// Instantiate a new instance of MailMessage
MailMessage mMailMessage = new MailMessage();
// Set the sender address of the mail message
mMailMessage.From = new MailAddress(from);
// Set the recepient address of the mail message
mMailMessage.To.Add(new MailAddress(to));
// Check if the bcc value is null or an empty string
if ((bcc != ""))
{
// Set the Bcc address of the mail message
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
// Check if the cc value is null or an empty value
if (cc!="")
{
// Set the CC address of the mail message
mMailMessage.CC.Add(new MailAddress(cc));
}
// Set the subject of the mail message
mMailMessage.Subject = subject;
// Set the body of the mail message
mMailMessage.Body = body;
// Set the format of the mail message body as HTML
mMailMessage.IsBodyHtml = true;
for (int i = 0; i < attach.Length; i++)
{
string attach21 = attach[i].ToString();
if ((attach21.ToString() != null) && (attach21.ToString() != string.Empty))
{
getattach = attach[i];
mMailMessage.Attachments.Add(new Attachment(getattach));
getattach = "";
}
}
// Set the priority of the mail message to normal
mMailMessage.Priority = MailPriority.High;
// Instantiate a new instance of SmtpClient
SmtpClient mSmtpClient = new SmtpClient();
// mSmtpClient.EnableSsl = true;
mSmtpClient.Host = mailServer;
// mSmtpClient.Credentials = System.Net.NetworkCredential("clinim", "qnool+pu");
// mSmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
// Send the mail message
mSmtpClient.Send(mMailMessage);
Label1.Text = "Mail Successfully Send.";
Trace.Write(mMailMessage.ToString());
}
catch (Exception ex)
{
//TraceContext tr = new TraceContext();
//tr.TraceMode = TraceMode.SortByCategory;
Trace.Write(ex.Message);
}
Last edited by raj_yash22; November 20th, 2009 at 06:01 AM..
|
November 20th, 2009, 06:14 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Obvious suggestion: look in the Trace you're writing to in the catch block and see what errors it is generating. Maybe authentication with the mail server fails, or the server cannot be reached?
BTW: when posting code examples like this, it may be a good idea to clean up a little first by removing dead code. There are a lot of commented out code lines which may distract from the real code when reading it in a web browser.
Imar
|
November 20th, 2009, 07:15 AM
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Clean Code
Thanks for ur reply Imar and I apologize for using comment tags(//) I post with my code. but i only getting error "Message send Failed" after i click send mail. Mail server works perfectly fine in my other sites and also this code works fine at local. Is there any way through i check mail server address status in asp.net. Thanks
Code:
try
{
string getattach;
string mailServer = ConfigurationManager.AppSettings["MailServer"].ToString();
MailMessage mMailMessage = new MailMessage();
mMailMessage.From = new MailAddress(from);
mMailMessage.To.Add(new MailAddress(to));
if ((bcc != ""))
{
mMailMessage.Bcc.Add(new MailAddress(bcc));
}
if (cc!="")
{
mMailMessage.CC.Add(new MailAddress(cc));
}
mMailMessage.Subject = subject;
mMailMessage.Body = body;
mMailMessage.IsBodyHtml = true;
for (int i = 0; i < attach.Length; i++)
{
string attach21 = attach[i].ToString();
if ((attach21.ToString() != null) && (attach21.ToString() != string.Empty))
{
getattach = attach[i];
mMailMessage.Attachments.Add(new Attachment(getattach));
getattach = "";
}
}
mMailMessage.Priority = MailPriority.High;
SmtpClient mSmtpClient = new SmtpClient();
mSmtpClient.Host = mailServer;
mSmtpClient.Send(mMailMessage);
Label1.Text = "Mail Successfully Send.";
Trace.Write(mMailMessage.ToString());
}
catch (Exception ex)
{
Trace.Write(ex.Message);
}
|
November 20th, 2009, 07:21 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Try outputting ex.ToString() rather than ex.Message - that contains the entire stack flow and may provide you with more information on the cause of the error.
From the information you have provided it is virtually impossible to work out what the problem might be. Have you checked that the MailServer setting is being read in correctly?
|
November 20th, 2009, 07:38 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Quote:
I apologize for using comment tags(//)
|
No problem. Comments by itself aren't bad. althoough maybe not always adding value. But I was merely referring to dead code; commented out code that makes things confusing.
Anywa, looks like Sam is already asking the right questions....
Imar
|
November 20th, 2009, 07:50 AM
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Exception Information
After doing ex.tostring(), I got this error.
Code:
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 202.138.96.11:25 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) 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(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) 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 communicationPanel.WebForm3.SendMailMessage(String from, String to, String bcc, String cc, String subject, String body, String[] attach) in C:\Inetpub\wwwroot\communicationPanel\Default.aspx.cs:line 420
It seems to be that this error is a mail server error but why it is working fine on another sites. And also at local it rocks. What could be the possibility for this.
Thanks,
|
November 20th, 2009, 07:56 AM
|
|
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Code:
What could be the possibility for this.
There could be a number of reasons, including:
1. The remote server cannot make a connection to the mail server on the port due to configuration / firewall settings
2. You need to authenticate. You may not need to do that locally if you're on a trusted line from your ISP
You probably need to talk to the owner of the mail server and ask them about how to use the server from your web app.
You could also try making a telnet connection to the mail server from your web server and see if that works.
Cheers,
Imar
|
November 20th, 2009, 07:56 AM
|
|
Friend of Wrox
|
|
Join Date: Aug 2007
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
|
|
Well plain and simple it is failing to connect to the mail server. So it is a networking issue. The cause could be numerous - wrong IP address, incorrectly configured DNS, firewalls, who knows.
|
November 20th, 2009, 08:04 AM
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
Thank you
I will look what exactly will be the reason for this connection and I will let u know. It seems relax that this is not my coding fault.
Thank you very much,
Rajat Bhalla
|
November 23rd, 2009, 01:45 AM
|
Authorized User
|
|
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
|
|
got the solution but still seems to be bug something
hey ,
When i removed host name from online in send mail function it works great but when I put host name online, then i dont know y it does not work. It is working but still does not making any logic. Any suggestions.
|
|
|