locked
C#.NET console application -sending mail.. when i am trying to send mail, i am getting error "Failed Sending mail" , can some one help me, below is my code... Thanks RRS feed

  • Question

  • mM.From = new MailAddress(mFrom, "MCheck");
                    mM.To.Add(new MailAddress(mTo));
                    //mM.CC.Add(new MailAddress(mCc));
                    //mM.Bcc.Add(new MailAddress(mBCC));
                    mM.Subject = "Health of PL2011Check status- " + mSubject;
                    mM.IsBodyHtml = true;

                    mM.Body =    "Database Server Name=" + dataBaseServername +
                                 "Database Name=" + dataBaseName +
                                 "Health Check Begin Date and Time=" + databasecheckStartDateTime +
                                 "Health Check End Date and Time=" + databasecheckEndDateTime +
                                 "Health check generic status=" + mSubject;
                    mM.BodyEncoding = System.Text.Encoding.UTF8;
                    // text or html
                    mM.IsBodyHtml = true;
                    //FileStream fs = new FileStream(@"test.pdf",
                    //                   FileMode.Open, FileAccess.Read);
                    //Attachment a = new Attachment(fs, "test.pdf",
                    //                   MediaTypeNames.Application.Octet);
                    //mM.Attachments.Add(a);

                    string str = "<html><body><h1>Picture</h1><br/><imgsrc=\"cid:image1\"></body></html>";
                    AlternateView av =
                                 AlternateView.CreateAlternateViewFromString(str,
                                 null,MediaTypeNames.Text.Html);                
                    mM.AlternateViews.Add(av);

                    sC.Host = "smtp.gmail.com";
                    //sC.Host = host_Name;
                    sC.Port =  587;
                    //sC.Port = Convert.ToInt32(port_Number);
                    //sC.Credentials = new
                    //System.Net.NetworkCredential("from@gmail.com","Password");
                    sC.Credentials = new NetworkCredential(mFrom, "1234567");
                    //sC.UseDefaultCredentials = false;
                    //sC.Credentials = nc;
                    //System.Net.NetworkCredential(mFrom,"1234567");
                    sC.EnableSsl = true;                
                    sC.Send(mM);
    Tuesday, December 4, 2012 6:13 AM

All replies

  • It looks like you have the FROM property commented out.  You need both the Credentials and the FROM address set in your code, and they both must be from the same email acount.

    The google website (owns gmail.com) recommends the following

    If you tried configuring your SMTP server on port 465 (with SSL) and port 587 (with TLS), but are still having trouble sending mail, try configuring your SMTP to use port 25 (with SSL).

    You must have either SSL or TLS enabled.  It still may not work if you intranet has ports 465, 587, or 25 blocked.  You would need to check with your MIS people to see if any of the ports are blocked.


    jdweng

    Tuesday, December 4, 2012 10:50 AM
  • Thank you Joel,

    I am providing FROM and all required things... But still it is not working.

    Yes, when i ping to gmail in comand prompt i.e. "telnet gmail.com 25".

    I am getting error like "Connecting gmail.com could not open connecting to the host,on port 25 connection failed".

    I think port 25 port is blocked, right.

    Please advice me...

    Thanks again...... :)


    • Edited by SusantaKumar Wednesday, December 5, 2012 7:03 AM
    Wednesday, December 5, 2012 7:01 AM
  • Many servers don't respond to a ping message becasue haskers would bring down the server by constantly sending pings.  Just because a server doesn't respond to a ping doesn't mean the port is blocked.  but I agree it is probably blocked.  I would enable SSL and try port 465.  Default credentials need to be set.  this is the settings I would use.

                MailMessage mC = new MailMessage();
                mC.From = new MailAddress(mFrom, "MCheck");
                mC.To.Add(new MailAddress(mTo));
                SmtpClient sC = new SmtpClient();
                sC.DeliveryMethod = SmtpDeliveryMethod.Network;  //<--------Add Line
                sC.UseDefaultCredentials = false;
                sC.Credentials = new NetworkCredential(mFrom, "1234567");
                sC.Host = "smtp.gmail.com";
                sC.Port = 465;
                sC.EnableSsl = true;   


    jdweng

    Wednesday, December 5, 2012 10:30 AM
  • sC.DeliveryMethod = SmtpDeliveryMethod.Network;
    I have added the line but still it fails. still i am getting same error "Fails sending mail".
    Wednesday, December 5, 2012 12:36 PM
  • If you are getting an exception copy the full exception to clipboard.  Then paste to Notepad and post results.  I'm not familar with the error message "Fails sending mail".  Thsi error message sound like it from your application and not the exception message.

     

     


    jdweng

    Wednesday, December 5, 2012 1:28 PM
  • Thanks Joel,Thank you for your valuable advice....

    Joel ,my code works in public network  but it is not working in my office network.

    Thursday, December 6, 2012 5:38 AM
  • Your company has the port blocked.  If you are writting software for your company you have to talk to the MIS people to see if they have a policy to allow you to work with outside SMTP mail servers.  there is either a virus protection program or firewall that is blocoking the port number.  You may just need to change the settings on the virus checker or firewall to unblock the port number.  Another method is to run a service that will use port forwarding.  Port forwarding will allow you to send to a different port number to get through the firewall and then change the port number back to the port number that gmail.com requires.


    jdweng

    Thursday, December 6, 2012 6:36 AM
  • Thanks a lot Joel,

    Thank you for your suggestion. ok i will ask to MIS team.

    Can you please explain more about how i can use port forwarding method and how it works and how to change firewall settings.

    Thank you again for your reply.

    Thursday, December 6, 2012 8:21 AM
  • I don't know how to change the firewall settings.  That is usually handled by my MIS depeartment and I don't have a forwall on my home PC.  I use my internet provider firewall.

    Port forwarding is used in a multi hop IP network where you are going through a number of servers and routers to get between computers.  I server will sometimes talk a number connections (say 100) and combine them on a single connection.  Each connection is a sepeerate application originally on a different port number.  So the new combined port number will be a new port number lets say 1234.  This is called port forwarding.  Then the firewall will block all port numbers except 1234.  The combined port 1234 will be able to get through the firewall but other ports like 25 will not be able to get through the firewall.  A second computer will later split the combined port 1234 back into the original port number and route the messages to their final destination.  Port forwarding is used in firewall application.  It is also used routing connections like over a satellite connection.  Instead of routing 100 seperate connections over a satellite channel the connections will be forwarded onto a single channel.


    jdweng

    Thursday, December 6, 2012 10:39 AM
  • Thanks a lot Joel,

    So , can i use port 1234 to send mails from my application or not.

    Friday, December 7, 2012 9:49 AM
  • You could, but GMAIL will not recogize the port number and the emails would not be sent.  An application can use any port number that is not blocked or in use by another application.  I was just using a hypothetical port number to explain the process.

    jdweng

    Friday, December 7, 2012 10:04 AM