-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMailClient.cs
More file actions
102 lines (96 loc) · 3.33 KB
/
MailClient.cs
File metadata and controls
102 lines (96 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.IO;
using System.Reflection;
using Mail.Contracts;
using System.ServiceModel;
using NLog;
//using Utils;
namespace MailClient
{
public class MailClient
{
private static Logger logger = LogManager.GetCurrentClassLogger();
private readonly IConfigurationManager configuration;
string contentPath;
string content;
string attachementPath;
string sender;
string recipient;
string cc;
string subject;
public MailClient() : this(new ConfigurationManagerWrapper())
{
}
public MailClient(IConfigurationManager configuration)
{
this.configuration = configuration;
this.content = configuration.GetSetting("content");
this.attachementPath = configuration.GetSetting("attachementPath");
this.sender = configuration.GetSetting("sender");
this.recipient = configuration.GetSetting("recipient");
this.cc = configuration.GetSetting("cc");
this.subject = configuration.GetSetting("subject");
}
public void SendMail(string subject, string content)
{
try
{
var mail = new MailModel
{
Sender = sender,
Content = content,
Recipent = recipient,
CC = cc,
Subject = subject
};
if (!string.IsNullOrEmpty(attachementPath))
{
using (var stream = new StreamReader(attachementPath))
using (var memoryStream = new MemoryStream())
{
CopyStream(stream.BaseStream, memoryStream);
mail.Attachement = memoryStream;
mail.AttachementName = Path.GetFileName(attachementPath);
SendMailByService(mail);
}
}
else
{
SendMailByService(mail);
}
}
catch (Exception ex)
{
try
{
logger.Info("Sending mail finish with failure");
//logger.LogFullExceptionDetails(ex);
}
catch(Exception exe) //chce, aby to się pojawiło w logach klienta, a to jest, gdyby klient nie miał logowania
{
}
}
}
private void SendMailByService(MailModel mail)
{
var mailEndpoint = this.configuration.GetClient("IMailService");
BasicHttpBinding myBinding = new BasicHttpBinding();
EndpointAddress myEndpoint = new EndpointAddress(mailEndpoint.Address.AbsoluteUri);
IMailService client = new ChannelFactory<IMailService>(myBinding, myEndpoint).CreateChannel();
client.Send(mail);
}
static void CopyStream(Stream input, Stream output)
{
var buffer = new byte[8 * 1024];
int len;
while ((len = input.Read(buffer, 0, buffer.Length)) > 0)
{
output.Write(buffer, 0, len);
}
}
}
}