Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have the following C# code for sending an email in several applications
C#
private void MailSend()
{
    DateTime Now = System.DateTime.Now;
    MailMessage message = new MailMessage();
    SmtpClient smtp = new SmtpClient("corimc04");
    smtp.Timeout = 5000;
    string recipient = recipientCB.SelectedItem.ToString();
    try
    {
        var _with1 = message;
        _with1.From = new MailAddress("Non-Stock_MRO_Administrator@gmail.com");
        _with1.To.Add(new MailAddress(email));        
        _with1.Body = "You have a package in receiving ready for pickup from " + supplier + ".\nOn Shelf: " + shelf + ".\nTracking Number: " + trackingNum;
        _with1.Subject = "MRO Automated Email for " + recipient;
        smtp.Send(message);
        message = null;
    }
    catch (Exception ex)
    {
        MessageBox.Show("Error Sending emails. " + ex.Message);
    }
    smtp = null;
    return;
}

I also have the following VB code for sending an email in other applications.

C#
Sub SendEmail()
    Dim smtp As New SmtpClient("corimc04")
    smtp.UseDefaultCredentials = True
    smtp.Timeout = 5000
    
    Try
        
        Dim message As New MailMessage()
        Dim _with1 = message
        _with1.From = New MailAddress("Rework_Reporter@gmail.com")
        _with1.To.Add(New MailAddress("email"))
        _with1.Body = "Rework Status for Rework WIP" & vbLf & data
        _with1.Subject = "Rework Report Updates"
        smtp.Send(message)
        message.Dispose()
    Catch ex As Exception
        Console.WriteLine("Error Sending emails. " & ex.Message)
    End Try

    smtp = Nothing
End Sub


Today my C# code just stopped working for some reason and none of my C# applications will send mail, but my VB applications still send mail. I have many projects that use this C# code and do not want to have to convert them. Does anyone have any ideas why this would just stop all of a sudden?

What I have tried:

I tried writing a VB DLL to reference in my C# project, but could not get this to work.
Posted
Updated 6-Sep-16 7:50am
Comments
[no name] 2-Sep-16 16:25pm    
My idea would be that your C# code stopped working because something changed. What happened we would have no idea because "stopped working" means nothing at all to us.
Philippe Mori 2-Sep-16 16:39pm    
If it was working before, then either the server is down or its configuration has changed (for ex. increased security). It would help, if you could find and share information on the actual error.
[no name] 4-Sep-16 19:34pm    
please check the below answer
Richard Deeming 5-Sep-16 10:19am    
The only obvious difference between the two methods is the line:
smtp.UseDefaultCredentials = True
which is missing from your C# code.

Otherwise, talk to your network administrators to find out what changed.

Doesn't work ? What "Doesn't work" means ? I am impressed how this code worked. You don't even provide the credentials for your email in order to actually send it... Anyway... you should try to input smtp.UseDefaultCredentials = true at your C# code.. which is something VB.NET code does... but C# code don't.
 
Share this answer
 
I receive "A first chance exception of type 'System.Net.Mail.SmtpException' occurred in System.dll Error Sending emails. The operation has timed out.The thread '<No Name>' (0x2658) has exited with code 0 (0x0)"
This code has been working for over a year, but is now giving this exception.

It is not an issue with the code. It seems like it has to do with the .NET controls for SMTP. When I use the C# applications on the server side, the application sends the email as expected, yet when I use this code from the client side, it seems like it's a connection issue, although the error I get is a timeout error.

This will not be solved on this thread.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900