coding to automatically email a line of text

Posted on 2011-02-18
Medium Priority
Last Modified: 2012-05-11
Ok I'm struggling a little bit on how to approach this. We are trying to get an email to go out every morning at a certain time with a random phrase that we specify. Here's the catch, the phrases come from an ebook, we use the kindle software and when you make notes on particular phrases you can export them to things like text files, etc. What we are looking to do is write a script or batch file or something that will look into this file pick a random line (phrase) that we had exported from our ebook and email it to the specified email address. Do you have any suggestions on what I should do to achieve this? what you would do? any programs or scripts to use or if I have to write a script should it be a batch or what? or would web based be the way to go?

Suggestions or advice would be highly appreciated.

Question by:dirtbikedude
LVL 10

Expert Comment

by:John Claes
ID: 34924603

You have already exported it to a TextFile.

Than I would suggest a Small app that you can schedule or a windowsService that has a Timer to run Once a day.

What must be done. :

Read the File to know the number of lines.
c  : http://www.mrx.net/c/readfunctions.html
c#: http://blogger.xs4all.nl/zomerf/archive/2008/10/02/416167.aspx
Use this as a max for your random generating number
Read that line of the file

Create the MailMessage and add the line to the body

Send the mail
c :  http://bytes.com/topic/c/answers/843485-how-send-email-c
c#: http://social.msdn.microsoft.com/Forums/en/netfxnetcom/thread/a75533eb-131b-4ff3-a3b2-b6df87c25cc8

Why a batch : Easy to use, easy to run, easy to schedule, easy to deploy :
Why a WindowsService : scheduling is automated, easy to deploy , invisible running.


Expert Comment

ID: 34924625
If you are already using a database, storing the lines of text in the database and scheduling it from the database would make controlling the aspects of the email much more dynamic and versatile

Author Comment

ID: 34935690
If I wanted to do it as a batch file. Can you guys give me the code that I would make a batch out of so that it takes a line of text from a text file and emails it?
LVL 10

Accepted Solution

John Claes earned 2000 total points
ID: 34941063

I'm not going to write the program for you :

but here some lines
reading file :
Randomize :
sending mail :

private void SendLineByMail(string FIlePath)
      int randomLineNumber = GetRandomLineNumber(FIlePath);
      string fileLine = 

private int GetRandomLineNumber(string FIlePath)
	List<string> lines = GetLineNumbers(FIlePath);
	int randomLine= random.Next(lines.Count);
	SentLineByMail(lines[randomLine], randomLine);

private List<string> GetLineNumbers(string FIlePath)
                List<string> lines = new List<string>();
		// Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader(FIlePath))
                    String line;
                    // Read and display lines from the file until the end of 
                    // the file is reached.
                    while ((line = sr.ReadLine()) != null)
		return lines ;
            catch (Exception e)
                return 0;


private void GetLineNumbers(string line, int lineNumber)
	System.Web.Mail.SmtpMail.SmtpServer="SMTP Host Address";
	System.Web.Mail.SmtpMail.Send("Fromtest@abc.com","Totest@abc.com","The Random Line Of today is ["+lineNumber+"]",line);

Open in new window

LVL 53

Expert Comment

ID: 35321628
This question has been classified as abandoned and is being closed as part of the Cleanup Program. See my comment at the end of the question for more details.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Make the most of your online learning experience.
There's never been a better time to become a computer scientist. Employment growth in the field is expected to reach 22% overall by 2020, and if you want to get in on the action, it’s a good idea to think about at least minoring in computer science …
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question