Simple console application for sending mails not working

  • Thread starter Thread starter savvy
  • Start date Start date
S

savvy

i'm trying to compile a simple console application for sending a mail,
my main idea is to schedule it to a particular time for sending mails
using the windows schedular task lateron. Therefore i need an exe file
to make that work. I compiled my file Emailtest.cs using this command
line
csc /r:System.Web.dll Emailtest.cs
and i got an .exe file but when clicking it or executing it i'm not
receiving any mail. I dont know where the problem is
Can anyone help me out please
Thanks in Advance

I'm giving my code below

using System;
using System.Web.Mail;

public class Emailtest
{
public static void Main()
{
try
{
MailMessage objMail = new MailMessage();
objMail.From = "mailfrom";
objMail.To = "(e-mail address removed)";
objMail.Subject = "test mail";
objMail.BodyFormat = MailFormat.Html;
objMail.Body = "this is for testing automatic emails.";


SmtpMail.SmtpServer = "localhost";
SmtpMail.Send(objMail);
Console.Write("Mail Sent to John Gera");

}
catch (Exception ex)
{
Console.Write(ex.Message);
}
}
}
 
savvy said:
i'm trying to compile a simple console application for sending a mail,
my main idea is to schedule it to a particular time for sending mails
using the windows schedular task lateron. Therefore i need an exe file
to make that work. I compiled my file Emailtest.cs using this command
line
csc /r:System.Web.dll Emailtest.cs
and i got an .exe file but when clicking it or executing it i'm not
receiving any mail. I dont know where the problem is

<snip>

Well, when you executed it from the command line, does it show an
exception?

Jon
 
Hi,


Are you getting any exception?
Do you have configured your local mail server?

Check in the local mailer to see if the mail is in the queue

cheers,
 
Thanks for your time and responses
Its working now
I made a small change in my code .. its

SmtpMail.SmtpServer.Insert( 0, "localhost");

and I'm able to send mails now
Thank you very much once again
 
savvy said:
Thanks for your time and responses
Its working now
I made a small change in my code .. its

SmtpMail.SmtpServer.Insert( 0, "localhost");

and I'm able to send mails now

Hmm... that's actually not doing anything at all - it's just fetching
the current value of SmtpServer and calling Insert on it, then throwing
away the result. I very much suspect that if you remove that line of
code, it will still work...

Jon
 
savvy said:
Thanks for your time and responses
Its working now
I made a small change in my code .. its

SmtpMail.SmtpServer.Insert( 0, "localhost");

and I'm able to send mails now
Thank you very much once again

I don't get it, AFAIK there is no Insert method on SmtpServer.

Willy.
 
Willy Denoyette said:
I don't get it, AFAIK there is no Insert method on SmtpServer.

The SmtpServer property is of type String, however - so it's calling
String.Insert - which is a no-op, as the return value isn't being used.
 
Jon Skeet said:
The SmtpServer property is of type String, however - so it's calling
String.Insert - which is a no-op, as the return value isn't being used.

My bad, I did a simple search on SmtpServer.Insert, obviously without result
as SmtpServer is a property of type string, note that the SmtpMail class is
obsolete in v2.0, suggested replacement is System.Net.Mail.

Willy.
 
McAfee AV will stop exes from sending mail! Other AV packages also render
your PC so safe as to be unusable, usually without so much as a 'by your
leave'!
 
Back
Top