0

When I try to send email on computer with enviroment(Visual Studio 2015) everything is fine but when I copied it to another computer(VMWare Virtual Machine) and try to run it shutdown, here is main code:

    try
    {
        SmtpClient client = new SmtpClient();
        client.Port = 587;
        client.Host = "smtp.gmail.com";
        client.EnableSsl = true;
        client.Timeout = 10000;
        client.DeliveryMethod = SmtpDeliveryMethod.Network;
        client.UseDefaultCredentials = false;
        client.Credentials = new System.Net.NetworkCredential("bazymysql@gmail.com", "xyz");

        MailMessage mm = new MailMessage("donotreply@domain.com", "bazymysql@gmail.com", "Przechwycony ciag znakow", tresc.Text);
        mm.BodyEncoding = UTF8Encoding.UTF8;
        mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

        client.Send(mm);
    }
    catch (SmtpException ex)
    {
        MessageBox.Show(ex.Message);
    }

What I've tried:

  • copy System.Net dll
  • developing 86x 64x
  • run as admin
KariiO
  • 9
  • 1
  • 5
  • It might be that it's running the thread into infinity. I would suggest that you enclose the part in a `Thread` so that it runs separately. The `SmtpClient` will run until it sends or fails the email. – Theunis Feb 06 '16 at 19:29
  • @Theunis I cant understand what you mean :( Can you explain this on code? – KariiO Feb 06 '16 at 19:31
  • Don't add your password in code please, and check below what I was trying to explain – Theunis Feb 06 '16 at 19:35
  • @Theunis that wasnt my password ;) – KariiO Feb 06 '16 at 19:37

1 Answers1

0

Maybe you can try the following

try
{
    SmtpClient client = new SmtpClient();
    client.Port = 587;
    client.Host = "smtp.gmail.com";
    client.EnableSsl = true;
    client.Timeout = 10000;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Credentials = new System.Net.NetworkCredential("bazymysql@gmail.com", "xyz");

    MailMessage mm = new MailMessage("donotreply@domain.com", "bazymysql@gmail.com", "Przechwycony ciag znakow", tresc.Text);
    mm.BodyEncoding = UTF8Encoding.UTF8;
    mm.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
    Thread sendMailThread = new Thread(()=>{
        client.Send(mm); //This will ensure that your sending happens on a second thread and that it does not crash your main thread.
    });
    sendMailThread.Start();
}
catch (SmtpException ex)
{
    MessageBox.Show(ex.Message);
}
Theunis
  • 238
  • 1
  • 15
  • then it might be the virtual machine that shuts down to another reason, can't comment on things that I can't test – Theunis Feb 08 '16 at 08:17