tags:

views:

265

answers:

6

Hi I have code,

 System.Web.Mail.MailMessage oMailMessage = new MailMessage();
            oMailMessage.From = strFromEmaild;
            oMailMessage.To = strToEmailId;
            oMailMessage.Subject = strSubject;
            oMailMessage.Body = strBody;
            SmtpMail.SmtpServer = "localhost";
            SmtpMail.Send(oMailMessage);

(all variables have values)

I have installed SMTP virtual services. why it is unable to send emails. why it is not working ??

EDIT

public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody)
{
    try
    {
        System.Web.Mail.MailMessage oMailMessage = new MailMessage();
        oMailMessage.From = strFromEmaild;
        oMailMessage.To = strToEmailId;
        oMailMessage.Subject = strSubject;
        oMailMessage.Body = strBody;
        SmtpMail.SmtpServer = "SERVERNAME";
        SmtpMail.Send(oMailMessage);

        return true;
     }
     catch (Exception ex)
     {
         return false;
     }
 }

I have this code. It is executing fine and is returning true, but I'm not getting any email in the inbox.

What else could be wrong?

Getting some mails in BadMail Dir at C:\Inetpub\mailroot\Badmail also in Queue Directory getting some mails here ... what does that means..??

I found that mail only can sent to gmail accounts... why it is?

+1  A: 

Determine what the error is:

try
{
 SmtpMail.Send(oMailMessage);
}
catch (Exception ex)
{
//breakpoint here to determine what the error is:
Console.WriteLine(ex.Message);
}

From here, please edit your question with that exception details.

p.campbell
No...If i get erroor or exception then that will catch before .. see my another post . I have given total coded function of mine.
Lalit
A: 

have you tried 127.0.0.1 instead of Localhost? Also have you tested that the SMTP service is working, check out this link for details.

Ioxp
+1  A: 

Its hard to tell, but one possibility is that you haven't enabled anonymous access on the SMTP virtual server. Go to the the virtual server properties dialog, select the Access tab, click the Access Control button, and make sure that Anonymous Access is enabled.

Andy Johnson
+1 Nice i forgot about that.
Ioxp
i HAVE CHECKED IT, IT IS ANNONYMOUS ENABLED
Lalit
@Lalit, why are you so mad? No caps please.
Nathan Koop
sorry nathann.. really i m mad dude
Lalit
You could try installing an email client and configuring it to use the virtual smtp server. Can you successfully send email using that?
Andy Johnson
+1  A: 

There doesn't appear to be anything functionally wrong with your program. It's likely a configuration issue between your program and the mail server. I would try the following to diagnose the problem.

  1. Wrap the code in a try/catch block and see if the exception message contains useful data
  2. Use 127.0.0.1 instead of localhost just to rule out anything crazy
  3. Ensure your SMTP server is running on the standard port (25 I believe)
JaredPar
+2  A: 

As mentioned by others, your code is fine and is most likely something in your SMTP configuration or maybe your email client your sending your test emails to is marking them as spam. If it's spam, well that's easy enoughto figure out. If it's something with the email, you can go to your mailroot folder and their will be some folders there with the email files along with a description. See if there's anything in the BadMail folder or the queue folder and open them up in notepad and view what error is given for why they weren't sent.

Justen
A: 

public bool SendMail(string strToEmailId, string strFromEmaild, string strSubject, string strBody) { try { System.Web.Mail.MailMessage oMailMessage = new MailMessage(); oMailMessage.From = strFromEmaild; oMailMessage.To = strToEmailId; oMailMessage.Subject = strSubject; oMailMessage.Body = strBody; SmtpMail.SmtpServer = "SERVERNAME"; SmtpMail.Send(oMailMessage);

            return true;
        }
        catch (Exception ex)
        {
            return false;

        }
    }

See I have this code. ok? iT IS EXECUTING FINE. BUT NOT GETTING ANY MAIL IN MAIL BOX? RETURNING "TRUE".HOW TO CHECK SmtpMail.Send(oMailMessage); WORKING OR NOT ?

Lalit
@Lalit, please add any updates to your question into your question, not into the answer portion. I have edited your question to include this information. If you could delete this answer that would be great.
Nathan Koop
sorryforget yaar r u here to catching foults ?
Lalit
@Lalit, I'm sorry, I don't quite understand your last comment. If you asking if I'm here catching faults, I kinda am, just trying to assist you to get the best answer by ensuring all the relevant data is in the proper place. and BTW my "are you mad" comment was supposed to be more light-hearted. Remember we're here trying to help you. :-)
Nathan Koop