How to send mail from C #

I have a 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 installed virtual SMTP services. why can't he send emails. why doesn't it work?

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 does a fine and returns true, but I do not receive any mail in the inbox.

What else could be wrong?

Receiving some letters in the BadMail Dir folder in the C: \ Inetpub \ mailroot \ Badmail folder also in the Queue folder, where there are some letters ... what does this mean .. ??

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

+5
source share
7 answers

Determine what the error is:

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

, , .

+1

, , , - SMTP , , , , . , , . - , mailroot, . , - BadMail , , .

+2

, , SMTP-. , "", " " , .

+1

. , . , .

  • try/catch , .
  • 127.0.0.1 localhost, - .
  • , SMTP- (25, )
+1
source

Hello, you can use 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);
       }
+1
source

Have you tried 127.0.0.1 instead of Localhost? Also, you checked if the SMTP service is working, check out this link.

0
source

On a virtual SMTP server Add restrictions on relays and connection control so that no external connections are allowed

enter image description here

0
source

All Articles