Click here to Skip to main content
15,882,063 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
Hi,

I need some help as I am at wits end!
I need to send an email using windows task scheduler. I need to pick up a file from a folder. Send via email to the SMTP server and when it's sent, it move to another folder.

If the file has 0kb then send email to someone else. :omg:

UPDATE by OP:
My main problem is that I am not that experianced in C#
Each time I use examples I get errors that I cant fix?
I have used:-
C#
public void SendMail(string mailFrom, string mailTo, string copyTo)
{
mail = new MailMessage();
mail.From = mailFrom;
mail.To = mailTo;
mail.Cc = copyTo;
mail.Subject = this.subject;
mail.Attachments.Add(new MailAttachment("c:\test.txt");
mail.Body = this.body;
mail.Priority = MailPriority.High;
mail.BodyFormat = MailFormat.html;
SmtpMail.SmtpServer = "172.16.0.23";
SmtpMail.Send(mail);
}

and get four errors:
Error 1 Expected class, delegate, enum, interface, or struct
Posted
Updated 6-Aug-10 5:53am
v2
Comments
Martin Jarvis 6-Aug-10 9:59am    
Can you give some detail about which part your having trouble with? Email is handled in c# within the System.Net.Mail namespace.

Well sending an email using C# is not that difficult. There are tones of artices all over the internet, including bunch of them here at cp. This is just one example[^]
 
Share this answer
 
public class EmailClass
{
public void SendMail(string mailFrom, string mailTo, string copyTo)
{
mail = new MailMessage();
mail.From = mailFrom;
mail.To = mailTo;
mail.Cc = copyTo;
mail.Subject = this.subject;
mail.Attachments.Add(new MailAttachment("c:\test.txt");

mail.Body = this.body;
mail.Priority = MailPriority.High;
mail.BodyFormat = MailFormat.html;
SmtpMail.SmtpServer = "172.16.0.23";

//You also need to add this code to send to someone else if file size
//is 0

if(new FileInfo(c:\test.txt)==0)
{
mail.To = "someoneElse@someoneelse.com";
}


SmtpMail.Send(mail);
}
}

The method should be in a class.
I think you should read up on some basic .Net programming
Email Notification Framework[^]
 
Share this answer
 
The error message implies that you have created an orphan method - one that is not part of a class or struct. If this is the complete code, then that is certainly the case. If so, you need to create a class with the above code as a member, then create an instance of the class and call it from Main. Alternatively, you could make the class containing the SendMail method static, then call the method without the need for instantiating it.

If there's more code to show, of course, I could be completely wrong; post some more! :)
 
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