Search code examples
c#emailsend

How to send the mail from c#


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?


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.