Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send the mail from c#

Tags:

c#

email

send

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?

like image 840
Red Swan Avatar asked Jan 22 '10 15:01

Red Swan


People also ask

What is e-mail in C?

e-mail, in full electronic mail, messages transmitted and received by digital computers through a network. An e-mail system allows computer users on a network to send text, graphics, sounds, and animated images to other users.

Can we send email using C++?

The following example codes demonstrate how to send email using SMTP/SSL/EWS/Queue, it also demonstrate how to sign email, encrypt email and add embedded images to email.


1 Answers

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.

like image 196
Justen Avatar answered Oct 18 '22 21:10

Justen