I just tried my hand at making a c# console program. The idea is to make a small self contained application that’ll check against a database and send a list via email to a bunch of users, it’d be run automatically every morning for instance.
I have not yet gotten so far, just trying to get the basics down now first. By looking at tutorials online I’ve been able to connect to the database and display a list in the console. Sending the mail is less successful however.
If I include the code for sending mail inside the while loop, it’ll generate a bunch of individual mails so for some reason that works. But that’s not what I want. I want the list to run and compile a list that’ll become the message body. After that part of the program has run I want to send the mail. Problem is it doesn’t work, when I look at the server log, an SMTP session is opened but it terminates and says socket error sending response to DATA next line says Winsock error 10045 and the session is terminated.
Any idea why this happens? The thing that really makes no sense to me is that it works if run inside the while loop…
Here is the whole program as it currently stands:
namespace SendMails
{
class ConnectToMySQL
{
[STAThread]
static void Main(string[] args)
{
string connStr = createConnstr("192.168.111.1","database","login","passwd");
MySqlConnection conn = null;
MySqlDataReader reader = null;
try
{
conn = new MySqlConnection(connStr);
conn.Open();
string stm = "SELECT id, name, surname FROM Kat WHERE Contact = 'Yes' AND Contactwhen <= '"+ DateTime.Today + "'";
MySqlCommand cmd = new MySqlCommand(stm, conn);
reader = cmd.ExecuteReader();
//The actual output on the screen.
while (reader.Read())
{
Console.WriteLine(reader.GetInt32(0) + ": " + reader.GetString(1) + " " + reader.GetString(2));
}
}
catch (MySqlException ex)
{
Console.WriteLine("Error: {0}", ex.ToString());
}
finally
{
if (conn != null)
{
conn.Close();
}
}
createMail("me@company.com", "info@company.com", "Test message", "Here is your test message.");
}
//This builds a mysql connection string
public static string createConnstr(string server, string databaseName, string user, string passw)
{
//build connection string
string connStr = "server=" + server + ";database=" + databaseName + ";uid=" + user + ";password=" + passw + ";";
//return connection string
return connStr;
}
//This sends an email
static void createMail(string recipient, string from, string subject, string msg)
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add(recipient);
message.Subject = subject;
message.From = new System.Net.Mail.MailAddress(from);
message.Body = msg;
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("192.168.111.1");
try
{
Console.WriteLine("Sending mail...");
smtp.Send(message);
Console.WriteLine("Mail was sent successfully!");
}
catch (Exception ep)
{
Console.WriteLine("failed to send mail:");
Console.WriteLine(ep.Message);
}
}
}
}
The problem is now solved. The solution was embarrassingly trivial.
Merely needed to add smtp.Dispose(); after the line smtp.Send(message);