SendMailJob.cs :  » Business-Application » Quartz-Enterprise-Scheduler » Quartz » Job » C# / CSharp Open Source

Home
C# / CSharp Open Source
1.2.6.4 mono .net core
2.2.6.4 mono core
3.Aspect Oriented Frameworks
4.Bloggers
5.Build Systems
6.Business Application
7.Charting Reporting Tools
8.Chat Servers
9.Code Coverage Tools
10.Content Management Systems CMS
11.CRM ERP
12.Database
13.Development
14.Email
15.Forum
16.Game
17.GIS
18.GUI
19.IDEs
20.Installers Generators
21.Inversion of Control Dependency Injection
22.Issue Tracking
23.Logging Tools
24.Message
25.Mobile
26.Network Clients
27.Network Servers
28.Office
29.PDF
30.Persistence Frameworks
31.Portals
32.Profilers
33.Project Management
34.RSS RDF
35.Rule Engines
36.Script
37.Search Engines
38.Sound Audio
39.Source Control
40.SQL Clients
41.Template Engines
42.Testing
43.UML
44.Web Frameworks
45.Web Service
46.Web Testing
47.Wiki Engines
48.Windows Presentation Foundation
49.Workflows
50.XML Parsers
C# / C Sharp
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source » Business Application » Quartz Enterprise Scheduler 
Quartz Enterprise Scheduler » Quartz » Job » SendMailJob.cs
/*
* Copyright 2004-2009 James House 
* 
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License. You may obtain a copy 
* of the License at 
* 
*   http://www.apache.org/licenses/LICENSE-2.0 
*   
* Unless required by applicable law or agreed to in writing, software 
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 
* License for the specific language governing permissions and limitations 
* under the License.
* 
*/

/*
* Previously Copyright (c) 2001-2004 James House
*/

using System;
using System.Globalization;

using Common.Logging;

#if NET_20
using System.Net.Mail;
#else
using System.Web.Mail;
#endif

namespace Quartz.Job{
  /// <summary>
  /// A Job which sends an e-mail with the configured content to the configured
  /// recipient.
  /// </summary>
  /// <author>James House</author>
  /// <author>Marko Lahma (.NET)</author>
  public class SendMailJob : IJob
  {
    private static readonly ILog Log = LogManager.GetLogger(typeof (SendMailJob));

      /// <summary> The host name of the smtp server. REQUIRED.</summary>
    public const string PropertySmtpHost = "smtp_host";

    /// <summary> The e-mail address to send the mail to. REQUIRED.</summary>
    public const string PropertyRecipient = "recipient";

    /// <summary> The e-mail address to cc the mail to. Optional.</summary>
    public const string PropertyCcRecipient = "cc_recipient";

    /// <summary> The e-mail address to claim the mail is from. REQUIRED.</summary>
    public const string PropertySender = "sender";

    /// <summary> The e-mail address the message should say to reply to. Optional.</summary>
    public const string PropertyReplyTo = "reply_to";

    /// <summary> The subject to place on the e-mail. REQUIRED.</summary>
    public const string PropertySubject = "subject";

    /// <summary> The e-mail message body. REQUIRED.</summary>
    public const string PropertyMessage = "message";

    /// <summary>
    /// Executes the job.
    /// </summary>
    /// <param name="context">The job execution context.</param>
    public virtual void Execute(JobExecutionContext context)
    {
      JobDataMap data = context.JobDetail.JobDataMap;

      string smtpHost = data.GetString(PropertySmtpHost);
      string to = data.GetString(PropertyRecipient);
      string cc = data.GetString(PropertyCcRecipient);
      string from = data.GetString(PropertySender);
      string replyTo = data.GetString(PropertyReplyTo);
      string subject = data.GetString(PropertySubject);
      string message = data.GetString(PropertyMessage);

      if (smtpHost == null || smtpHost.Trim().Length == 0)
      {
        throw new ArgumentException("PropertySmtpHost not specified.");
      }
      if (to == null || to.Trim().Length == 0)
      {
        throw new ArgumentException("PropertyRecipient not specified.");
      }
      if (from == null || from.Trim().Length == 0)
      {
        throw new ArgumentException("PropertySender not specified.");
      }
      if (subject == null || subject.Trim().Length == 0)
      {
        throw new ArgumentException("PropertySubject not specified.");
      }
      if (message == null || message.Trim().Length == 0)
      {
        throw new ArgumentException("PropertyMessage not specified.");
      }

      if (cc != null && cc.Trim().Length == 0)
      {
        cc = null;
      }

      if (replyTo != null && replyTo.Trim().Length == 0)
      {
        replyTo = null;
      }

      string mailDesc = string.Format(CultureInfo.InvariantCulture, "'{0}' to: {1}", subject, to);

      Log.Info(string.Format(CultureInfo.InvariantCulture, "Sending message {0}", mailDesc));

      try
      {
        SendMail(smtpHost, to, cc, from, replyTo, subject, message);
      }
      catch (Exception ex)
      {
        throw new JobExecutionException(string.Format(CultureInfo.InvariantCulture, "Unable to send mail: {0}", mailDesc), ex, false);
      }
    }


      private void SendMail(string smtpHost, string to, string cc, string from, string replyTo, string subject,
                          string message)
    {
#if NET_20
            MailMessage mimeMessage = new MailMessage(from, to, subject, message);
          if (!String.IsNullOrEmpty(cc))
          {
              mimeMessage.CC.Add(cc);
          }
          if (!String.IsNullOrEmpty(replyTo))
          {
              mimeMessage.ReplyTo = new MailAddress(replyTo);
          }

          Send(mimeMessage, smtpHost);
#else
            MailMessage mimeMessage = new MailMessage();
      mimeMessage.To = to;
            if (cc != null && cc.Length > 0) 
            {
                mimeMessage.Cc = cc;
            }
      mimeMessage.From = from;
      mimeMessage.Subject = subject;
      mimeMessage.Body = message;
      
            SmtpMail.SmtpServer = smtpHost;
            SmtpMail.Send(mimeMessage);
#endif
        }

#if NET_20
      protected virtual void Send(MailMessage mimeMessage, string smtpHost) 
        {
          SmtpClient client = new SmtpClient(smtpHost);
          client.Send(mimeMessage);
      }
#endif
  }
}
www.java2v.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.