0

Possible Duplicate:
Sending email in .NET through Gmail

This mail code is working in localhost i.e on my computer but when i uopload it on server it is not working. The error is : Failure sending mail. please tell me where is the problem.

 if (Session["userinfo"] != null)
     {

         lblTest.Text = Session["userinfo"].ToString();
         MailMessage msg = new MailMessage();
         msg.From = new MailAddress("shop.bcharya@gmail.com");
         msg.To.Add(new MailAddress("bcc@dr.com"));
         msg.To.Add(new MailAddress("info@yzentech.com"));
         msg.Subject = "Mail from BcharyaCorporation.online shopping site";
         msg.Body = ""+lblTest.Text+"  wants to buy some products. please contact with him/her";
         SmtpClient sc = new SmtpClient();
         sc.Host = "smtp.gmail.com";
         // sc.Port = 25;
         sc.Credentials = new NetworkCredential("shop.bcharya@gmail.com", "mypassword");
         sc.EnableSsl = true;
         try
         {
             sc.Send(msg);
             lblPayment.Text = "Sorry. Currently we are out of online payment service. We will contact you for payment process. Thank you for buying this product.";

         }
         catch (Exception ex)
         {
             lblPayment.Text=ex.Message.ToString();
             Response.Write(ex.Message);
         }

     }
Community
  • 1
  • 1
Manish Sasmal
  • 73
  • 3
  • 5
  • 14
  • 1
    Can you provide the details of the exception that you get? – Ravi Y Nov 21 '12 at 07:23
  • no others error details are showing there only showing Failure sending mail. i am writing the code in catch block please view the edit @ ryadavilli – Manish Sasmal Nov 21 '12 at 07:31
  • What i meant was can you check the exception type, inner exception, etc.... That should give you an idea of what went wrong. – Ravi Y Nov 21 '12 at 07:33

4 Answers4

1

For gmail mail settings add Port number too

sc.Port = 587;

after this line

sc.Host = "smtp.gmail.com";
sajanyamaha
  • 3,119
  • 2
  • 26
  • 44
1

Only use port 587 and SSL if the SMTP server supports that (GMail and Hotmail for example). Some servers just use port 25 and no SSL.

Henk Mollema
  • 44,194
  • 12
  • 93
  • 104
0

Use below method and then check :

SmtpClient sc = new SmtpClient(string); //sends e-mail by using the specified SMTP server

Rakesh_HBK
  • 181
  • 3
  • 12
  • Which is equivalent to first creating an SmtpClient and then setting the Host property, which OP has already done. – J. Steen Nov 21 '12 at 08:03
  • can you please tell me what do you want to mean by specefied SMTP server. @rr_only4you – Manish Sasmal Nov 21 '12 at 08:04
  • i have used below code for sending a mail and it works :System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient("10.0.0.101");// please add your port number client.UseDefaultCredentials = false; – Rakesh_HBK Nov 21 '12 at 08:34
0

You can use this below given code for sending email. Here sending the error details through email is one method. Try this code for sending email.

using System.Web.Mail
public static bool SendErrorEmail(string to, string cc, string bcc, string subject,    string body, MailPriority priority, bool isHtml)
{
 try
{
using (SmtpClient smtpClient = new SmtpClient())
{
using (MailMessage message = new MailMessage())
 {
 MailAddress fromAddress = new MailAddress(“yourmail@domain.com”, “Your name”);
 // You can specify the host name or ipaddress of your server
 smtpClient.Host = “mail.yourdomain.com”; //you can specify mail server IP address here
 //Default port is 25
 smtpClient.Port = 25;
 NetworkCredential info = new NetworkCredential(“yourmail@domain.com”, “your password”);
 smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
 smtpClient.UseDefaultCredentials = false;
 smtpClient.Credentials = info;
 //From address will be given as a MailAddress Object
 message.From = from;
 message.Priority = priority;
 // To address collection of MailAddress
 message.To.Add(to);
 message.Subject = subject;
 // CC and BCC optional
 if (cc.Length > 0)
 {
 message.CC.Add(cc);
 }
 if (bcc.Length > 0)
 {
 message.Bcc.Add(bcc);
 }
 //Body can be Html or text format;Specify true if it is html message
 message.IsBodyHtml = isHtml;
 // Message body content
 message.Body = body;
 // Send SMTP mail
 smtpClient.Send(message);
 }
 }
 return true;
 }
 catch (Exception ee)
 {
 Logger.LogError(ee, “Error while sending email to ” + toAddress);
 throw;
 }
}
Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
ocp
  • 173
  • 1
  • 2
  • 9
  • can you please tell me for sending mail should i create my own mail server from my hosting site address? Please help me.@AndrewBarber – Manish Sasmal Nov 21 '12 at 13:33