3

This code works fine on my local machine ut when I deploy it. it gives Failure sending mail error.. Please Help...

MailAddress addrsTo = new MailAddress(toEmail);
MailAddress addrsFrom = new MailAddress("XXX@XXX.com", "XXX Title");

MailMessage mailmsg = new MailMessage(addrsFrom, addrsTo);
mailmsg.Subject = mailSbjct;

mailmsg.Body = "XXX Body";

SmtpClient smtp = new SmtpClient("mail.XXX.com");
smtp.EnableSsl = false;
smtp.Port = 26;
smtp.Credentials = new NetworkCredential("XXX@XXX.com", "XXXXXXX");

try {
    smtp.Send(mailmsg);
} catch (Exception exc) {
    throw new XXXException(1234, "---" + exc.Message);

}
Anujith
  • 9,370
  • 6
  • 33
  • 48
Arahim
  • 303
  • 1
  • 6
  • 14

2 Answers2

7

you can try this, if you are using gmail :

 MailMessage mail = new MailMessage();
 mail.Subject = "Your Subject";
 mail.From = new MailAddress("senderMailAddress");
 mail.To.Add("ReceiverMailAddress");
 mail.Body = "Hello! your mail content goes here...";
 mail.IsBodyHtml = true;

 SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
 smtp.EnableSsl = true;
 NetworkCredential netCre = new NetworkCredential("SenderMailAddress","SenderPassword" );
 smtp.Credentials = netCre;

 try
  {
   smtp.Send(mail);                
  }
  catch (Exception ex)
  {               
  }
Mohammad Arshad Alam
  • 9,694
  • 6
  • 38
  • 61
  • I am using my custom address. Actually It was working last month, but yesterday I have received an email from customer that the email is not working and then I go to check and it is not working. Then I try from my local machine and it is working working fine, then I deploy the files again and the problem exist... Please Help... – Arahim Mar 21 '13 at 07:50
  • then check the port number 26 is allowed in your hosting server's firewall. – Mohammad Arshad Alam Mar 21 '13 at 08:50
  • suggested works with gmail account, are you using gmail account for that ? – Mohammad Arshad Alam Mar 21 '13 at 10:57
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/26627/discussion-between-arshad-and-arahim) – Mohammad Arshad Alam Mar 21 '13 at 11:08
-1

As you have mentioned in your question works fine on my local machine.
It is suggesting that the problem is of credential which you are providing to send the mail.

Edit 1

If you are using you own domain credential then it is not going to work on the server.
The user IIS should have enough authority to send mail.
IIS User More detail
http://www.iis.net/learn/get-started/planning-for-security/understanding-built-in-user-and-group-accounts-in-iis

Here is a SO link
How to send email through IIS7?

Community
  • 1
  • 1
शेखर
  • 17,412
  • 13
  • 61
  • 117
  • How to set IIS on server?. I have my own credentials and it is working fine on my local machine. But not in server. – Arahim Mar 21 '13 at 07:52