Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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.

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)

Hello you can follow the following code:

try
        {
            SmtpClient client = new SmtpClient("smtp.gmail.com", 587);
            client.EnableSsl = true;
            client.Timeout = 100000;
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential("your gmail id", "password");
            MailMessage msg = new MailMessage();
            msg.To.Add(textBoxTo.Text);
            msg.From = new MailAddress("your gmail id");
            msg.Subject = textBoxSubject.Text;
            msg.Body = textBoxMsg.Text;
            Attachment data = new Attachment(textBoxAttachment.Text);
            msg.Attachments.Add(data);
            client.Send(msg);
            MessageBox.Show("Successfully Sent Message.");
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
       }

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.

In the virtual smtp server Add relay restrictions and connection control so that none of the outside connections are allowed

enter image description here

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top