-1

My send mail Code below is not working correctly. It is not sending any email or returning any errors. I'm not getting an Exception thrown at all from the below code. How would I set up to catch common errors that may be happening in the below code?

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Net.Mail; 
using System.Net;

namespace DataAccess 
{ 
    public class DLSendMails 
    {
        public string SendCommentMails(String MemberEmail, int
        { 
            try 
            {
                using (SmtpClient client =
                         new SmtpClient("smtpout.secureserver.net"))
                { 
                    client.Credentials =
                     new NetworkCredential("webmaster@mysite.com", "pwd"); 

                    //client.Credentials
                    //         = CredentialCache.DefaultNetworkCredentials;  
                    //client.DeliveryMethod
                    //         = SmtpDeliveryMethod.Network;  

                    string to = MemberEmail; 

                    var mail = new MailMessage(); 
                    mail.From = new MailAddress("webmaster@mysite.com",
                                                "mysite.com"); 
                    mail.To.Add(to);

                    mail.Subject = "New Comment recieved at mysite.com"; 
                    mail.Body = "You can read your comment and access your "
                              + "profile page by using this link.\n"
                              + "http://www.mysite.com/Member.aspx?UserID="
                              + PicID; 
                    mail.IsBodyHtml = true; 

                    client.Send(mail); 
                    return "sent mail";
                }
            } 
            catch (Exception ex) 
            { 
                // exception handling  
                return ex.ToString(); 
            }
        }
    }
}
ahsteele
  • 26,243
  • 28
  • 134
  • 248
CsharpBeginner
  • 1,753
  • 8
  • 38
  • 68
  • 3
    have you checked that you didn't misspell (sp?) 'passowrd' :) if there is no exception the mail message is being forwarded to your smtp server. I recommend you try to use fiddler to see if the request is going out and what the response is. – Darko Feb 27 '12 at 02:34
  • I thought the same about the password typo but I also did some reading about possibly having to declare default credentials prior to setting new credentials. It seems silly and I'm certainly not saying it is a correct answer but it has me intrigued! – Stephen Tetreault Feb 27 '12 at 02:43
  • CsharpBeginner I realize that you are new here and to C#, but posting nicely formatted code makes it a lot easier to understand what you are asking. Further think about what's really necessary to answer a question. When I formatted your question I left everything, but you should ask yourself are the usings and namespace truly necessary? – ahsteele Feb 27 '12 at 02:51

1 Answers1

0

First off, I am not sure if it was intentional but in your client.Credentials code you have "passowrd" typed instead of "password". Was this a typo or not?

Also, in this related post c# SmtpClient class not able to send email using gmail a user says that you have to declare UseDefaultCredentials as false before you set new NetworkCredentials. Also, in the same article I've linked here it says maybe a possibility might be a firewall interfering?

I hope this might be of some assistance but if not I'll certainly continue reading about it as I would like to know more about this! Good luck!

Community
  • 1
  • 1
Stephen Tetreault
  • 769
  • 1
  • 8
  • 26