Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send email from a Windows service?

I need to send lots of emails (probably hundreds a day) on a schedule. The way I'm thinking to do it is as follows but the problem is that my Body field can get very big, and if I add it as a string, it gets ugly.

SmtpClient client = new SmtpClient(); //host and port picked from web.config
client.EnableSsl = true;

MailMessage message = new MailMessage();

message.Body = "test from winservice"; // HERE IS MY PROBLEM
message.IsBodyHtml = true;
message.From = new MailAddress("[email protected]");
message.Subject = "My subject";
message.To.Add(new MailAddress("[email protected]"));
try
{
    client.Send(message);
}
catch (Exception)
{
   
}

When I had to do it from an aspx page, I used:

MailDefinition message = new MailDefinition();  
        
message.BodyFileName = @"~\EmailTemplate\Template1.htm";
ListDictionary replacements = new ListDictionary();
replacements.Add("<% Name %>", this.txtName.Text);
replacements.Add("<% PhoneOrEmail %>", this.txtPhoneOrEmail.Text);
replacements.Add("<% Message %>", this.txtMessage.Text);
MailMessage msgHtml = message.CreateMailMessage(RECIPIENTS, replacements, new LiteralControl());

I think it is an elegant solution, but I don't want to reference to System.Web.UI.WebControls.MailDefinition because I'm in Winservice.

  1. How can I send bulk emails from Winservice?
  2. Is it possible to send the emails from a Gmail account? Or they are going to block me after a while?
like image 948
UshaP Avatar asked Mar 10 '10 16:03

UshaP


People also ask

How do I send an email from Windows?

Select Home > New Email. Add recipients, a subject, and a message in the email body. Select Send.

Can you send an email from CMD?

In Windows there is no way to natively send mail from the Command Prompt, but because PowerShell allows you to use the underlying . Net Framework, you can easily create and send an e-mail from the command line.

How do I stop Windows Service from sending emails?

Go to the Services snap-in, find the service, double click, and go to the Recovery tab. There you can choose what happens when it fails. One of the options is Run a Program . You can write a script or program that sends an email, and simply point this at it.

How do I send an email in Windows 7?

Compose an emailSelect New Email. Add recipients, a subject, and type your message. If you want to send a file, select Attach File. Select Send.


1 Answers

Why would you not use the exact same concept as the MailDefinition uses? Load the body from your template file, replace some markers with the text from another list - mail merge style?

All you're doing is a foreach over a data set of information to be merged with the template. Load your merge data, loop over the merge data replacing the tokens in your template with the current merge record. Set the message body as the currently built message. Attach the message to the message queue or send it now, whichever you choose.

It's not rocket science. You've got the code to create the message, so it's just a case of loading your merge data and looping through it. I've simplified to demonstrate the concept and I've used a CSV for the merge data and assumed that no field contains any commas:

message.IsBodyHtml = true;
message.From = new MailAddress("[email protected]");
message.Subject = "My bogus email subject";

string[] lines = File.ReadAllLines(@"~\MergeData.csv");
string originalTemplate = File.ReadAllText(@"~\Template.htm");

foreach(string line in lines)
{
    /* Split out the merge data */
    string[] mergeData = line.Split(',');

    /* Reset the template - to revert changes made in previous loop */
    string currentTemplate = originalTemplate;

    /* Replace the merge tokens with actual data */
    currentTemplate = currentTemplate.Replace("[[FullNameToken]]", mergeData[0]); 
    currentTemplate = currentTemplate.Replace("[[FirstNameToken]]", mergeData[1]);
    currentTemplate = currentTemplate.Replace("[[OtherToken]]", mergeData[2]);

    /*... other token replacements as necessary.
     * tokens can be specified as necessary using whatever syntax you choose
     * just make sure that there's something denoting the token so you can
     * easily replace it */

    /* Transfer the merged template to the message body */
    message.Body = currentTemplate;

    /* Clear out the address from the previous loop before adding the current one */
    message.To.Clear();
    message.To.Add(new MailAddress(mergeData[3]));
    client.Send(message);
}
like image 119
BenAlabaster Avatar answered Sep 29 '22 09:09

BenAlabaster