none
MailMessage Class RRS feed

  • Question

  • Hello Everyone.
    Could someone help me
    In my computer (Windows 7 - net framework 4.5). using Ninjatrader ( a C# trading  Program ) I could send emails using "MailMessage Class" correctly. but then I tried to replicate it in another pc ( a Dedicated server / Windows 2008 R2 -Net Framework 4.5) using the exactly same program ( Ninjatrader)  and code and all priviliges. 
    Even  could compile it without errors. But does not send any message.
    what could be wrong? 
    Thanks.

    using System.Net.Mail ;
    using System.Net ; 
    using System.Drawing.Imaging ;
    using System.IO ;
    using System.Windows.Forms ;

    string host = "smtp.gmail.com";
    string userName = "bla";
    string password = "password";

    string  path1 = @"c:\ScreenSaversNinja\";

    internal  void Enviando_Mensaje( )
     {
    try
             {
     
    MailMessage message = new MailMessage();
    message.To.Add("bla@gmail.com");
    message.From = new MailAddress("bla@gmail.com");


    message.Subject = "Title";
    message.Body =  "Body"; 
    //-- message
    file = path1  + ".jpg" ;

    if (host != string.Empty)
                    {
                        new SmtpClient(host) { Credentials = new NetworkCredential(userName, password), EnableSsl = true }.Send(message);
                    }
     
                }
                catch (Exception)
                {
                }
            }  

    Tuesday, June 7, 2016 11:39 PM

Answers

  • Hello,

    First thing to do is to get rid of the try-catch as it's hiding any exceptions. Next up, subscribe to SendCompleted event as shown here.

    Also, GMail may be blocking the operation, I remember seeing that you may have to allow a device to have permissions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Wednesday, June 8, 2016 12:33 AM
    Moderator

All replies

  • Hello,

    First thing to do is to get rid of the try-catch as it's hiding any exceptions. Next up, subscribe to SendCompleted event as shown here.

    Also, GMail may be blocking the operation, I remember seeing that you may have to allow a device to have permissions.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites



    Wednesday, June 8, 2016 12:33 AM
    Moderator
  • Hi,

    Check the solution for Kareninstructor.

    Other Reference for Gmail permission :

    http://www.c-sharpcorner.com/blogs/smtp-server-requires-a-secure-connection1

    Wednesday, June 8, 2016 8:44 AM
  • Look here:
    http://stackoverflow.com/questions/32260/sending-email-in-net-through-gmail

    Or test this:

    SmtpClient mailClient = new SmtpClient();
               mailClient.Host = "smtp.gmail.com"; 
               mailClient.Port = 587;
               
               System.Net.NetworkCredential credentials = 
               new System.Net.NetworkCredential();
               credentials.UserName = "bla";
               credentials.Password = "password";
               mailClient.Credentials = credentials;
    
               mailClient.Send(message);

    Wednesday, June 8, 2016 9:22 AM