Free cookie consent management tool by TermsFeed Policy Generator

source: branches/3.1/sources/HeuristicLab.CEDMA.Operators/ItemMailer.cs

Last change on this file was 521, checked in by gkronber, 16 years ago

added an operator to e-mail items

File size: 2.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using HeuristicLab.Core;
23using HeuristicLab.Data;
24using System.Net.Mail;
25using System.IO;
26
27namespace HeuristicLab.CEDMA.Operators {
28  public class ItemMailer : OperatorBase {
29    public override string Description {
30      get { return "TASK."; }
31    }
32
33    public ItemMailer()
34      : base() {
35      AddVariableInfo(new VariableInfo("Server", "SMTP server", typeof(StringData), VariableKind.In));
36      AddVariableInfo(new VariableInfo("To", "Recipient address", typeof(StringData), VariableKind.In));
37      AddVariableInfo(new VariableInfo("Item", "The item to send", typeof(IItem), VariableKind.In));
38    }
39
40    public override IOperation Apply(IScope scope) {
41      string server = GetVariableValue<StringData>("Server", scope, true).Data;
42      string to = GetVariableValue<StringData>("To", scope, true).Data;
43      IItem item = GetVariableValue<IItem>("Item", scope, true);
44
45      MailMessage message = new MailMessage();
46      message.To.Add(to);
47      message.Subject = "HL3 Item - " + GetVariableInfo("Item").ActualName;
48      message.From = new MailAddress(@"robot@heuristiclab.com");
49      using(MemoryStream memStream = new MemoryStream()) {
50        PersistenceManager.Save(item, memStream);
51        memStream.Flush();
52        memStream.Position = 0;
53        message.Attachments.Add(new Attachment(memStream, "Scope"));
54
55        SmtpClient smtpClient = new SmtpClient(server, 2525);
56        smtpClient.UseDefaultCredentials = true;
57        smtpClient.Send(message);
58      }
59      return null;
60    }
61  }
62}
Note: See TracBrowser for help on using the repository browser.