Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > C# and C > C# 2005 > C# 2005
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
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 tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
 
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 20th, 2009, 05:52 AM
Authorized User
 
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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..
  #2 (permalink)  
Old November 20th, 2009, 06:14 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #3 (permalink)  
Old November 20th, 2009, 07:15 AM
Authorized User
 
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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);
            
            }
  #4 (permalink)  
Old November 20th, 2009, 07:21 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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?
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #5 (permalink)  
Old November 20th, 2009, 07:38 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #6 (permalink)  
Old November 20th, 2009, 07:50 AM
Authorized User
 
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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,
  #7 (permalink)  
Old November 20th, 2009, 07:56 AM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

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
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
  #8 (permalink)  
Old November 20th, 2009, 07:56 AM
samjudson's Avatar
Friend of Wrox
Points: 8,687, Level: 40
Points: 8,687, Level: 40 Points: 8,687, Level: 40 Points: 8,687, Level: 40
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Aug 2007
Location: Newcastle, , United Kingdom.
Posts: 2,128
Thanks: 1
Thanked 189 Times in 188 Posts
Default

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.
__________________
/- Sam Judson : Wrox Technical Editor -/

Think before you post: What have you tried?
  #9 (permalink)  
Old November 20th, 2009, 08:04 AM
Authorized User
 
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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
  #10 (permalink)  
Old November 23rd, 2009, 01:45 AM
Authorized User
 
Join Date: Nov 2009
Posts: 14
Thanks: 1
Thanked 0 Times in 0 Posts
Default 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.
 


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem with sending e-mail Paula222 Pro PHP 1 July 27th, 2006 06:57 AM
Problem with sending mail in php Paula222 Beginning PHP 4 July 16th, 2006 03:38 PM
Problem sending SMTP mail Dsypher ASP.NET 2.0 Basics 3 February 16th, 2006 01:58 PM
Sending mail problem!!!! NitinJoshi General .NET 2 January 13th, 2005 08:31 AM
Problem with sending mail nikunjn VS.NET 2002/2003 1 January 29th, 2004 08:29 AM



All times are GMT -4. The time now is 10:13 AM.


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