프로젝트

일반

사용자정보

통계
| 브랜치(Branch): | 개정판:

markus / ConvertService / ServiceBase / EmailClient / EmailClient.cs @ 950e6b84

이력 | 보기 | 이력해설 | 다운로드 (7.35 KB)

1
using System;
2
using System.Collections.Generic;
3
using System.Linq;
4
using System.Text;
5
using System.Net.Mail;
6

    
7
namespace EmailClient
8
{
9
    public class EmailClient : IDisposable
10
    {
11
        public event EventHandler<SendEmailResult> EmailSendCompleted;
12
        private const string _CompanyAddress = "daelim.co.kr";
13
        private const string SmtpClientAddress = "any.daelim.co.kr";
14
        private Guid _token;
15
        private SendEmailResult _result;
16
        private bool _Async = false;
17
        MailMessage mail = new MailMessage();
18

    
19
        /// <summary>
20
        /// mail client 초기화
21
        /// </summary>
22
        /// <param name="fromAddress"></param>
23
        public EmailClient(string UserID)
24
        {
25
            mail.From = new MailAddress(UserID + "@" + _CompanyAddress);
26
        }
27

    
28
        /// <summary>
29
        /// mail client 초기화
30
        /// </summary>
31
        /// <param name="fromAddress"></param>
32
        public EmailClient(MailAddress fromAddress)
33
        {
34
            mail.From = fromAddress;
35
        }
36

    
37
        /// <summary>
38
        /// 비동기 email send
39
        /// </summary>
40
        /// <param name="toAddress"></param>
41
        /// <param name="ccAddress"></param>
42
        /// <param name="subject"></param>
43
        /// <param name="message"></param>
44
        public void SendEmailAsync(string toAddress, string ccAddress, string subject, string message,bool IsHtmlMsg)
45
        {
46
            _Async = true;
47
            mail.To.Clear();
48
            mail.CC.Clear();
49
            SendEmail(new List<string> { toAddress }, new List<string> { ccAddress }, subject, message, IsHtmlMsg);
50
        }
51

    
52
        /// <summary>
53
        /// 비동기 email send
54
        /// </summary>
55
        /// <param name="toAddress"></param>
56
        /// <param name="ccAddress"></param>
57
        /// <param name="subject"></param>
58
        /// <param name="message"></param>
59
        public void SendEmailAsync(List<string> toAddress, List<string> ccAddress, string subject, string message, bool isHtmlMsg)
60
        {
61
            _Async = true;
62
            Send(toAddress, ccAddress, subject, message, isHtmlMsg);
63
        }
64

    
65
        /// <summary>
66
        /// 동기 email send
67
        /// </summary>
68
        /// <param name="toAddress"></param>
69
        /// <param name="ccAddress"></param>
70
        /// <param name="subject"></param>
71
        /// <param name="message"></param>
72
        /// <returns></returns>
73
        public SendEmailResult SendEmail(string toAddress, string ccAddress, string subject, string message,bool isHtmlMsg)
74
        {
75
            List<string> _toAddr = new List<string>();
76
            List<string> _ccAddr = new List<string>();
77
            mail.To.Clear();
78
            mail.CC.Clear();
79
            if (!string.IsNullOrEmpty(toAddress)) _toAddr.Add(toAddress);
80
            if (!string.IsNullOrEmpty(ccAddress)) _ccAddr.Add(ccAddress);
81

    
82
            Send(_toAddr, _ccAddr, subject, message, isHtmlMsg);
83
            return _result;
84
        }
85

    
86
        public SendEmailResult SendEmail(List<string> toAddress, List<string> ccAddress, string subject, string message, bool isHtmlMsg)
87
        {
88
            Send(toAddress, ccAddress, subject, message, isHtmlMsg);
89
            return _result;
90
        }
91

    
92
        private void Send(string toAddress, string ccAddress, string subject, string message, bool isHtmlMsg)
93
        {
94
            List<string> _toAddr = new List<string>();
95
            List<string> _ccAddr = new List<string>();
96
            
97
            if (!string.IsNullOrWhiteSpace(toAddress)) _toAddr.Add(toAddress);
98
            if (!string.IsNullOrWhiteSpace(ccAddress)) _ccAddr.Add(ccAddress);
99

    
100
            Send(_toAddr,_ccAddr, subject, message,isHtmlMsg);
101
        }
102

    
103
        private void Send(List<string> toAddress, List<string> ccAddress, string subject, string message, bool isHtmlMsg)
104
        {
105
            try
106
            {
107
                /// foreach가 더 빠르다.
108

    
109
                //대림 메일 보류(강인구)
110
                //(from add in toAddress.Distinct()
111
                // let addr = (add.ToLower().IndexOf("@") < 0) ? add + "@" + _CompanyAddress: add
112
                // select addr)
113
                //.ToList().ForEach(ad => mail.To.Add(new MailAddress(ad)));
114

    
115
                //(from add in ccAddress.Distinct()
116
                // let addr = (add.ToLower().IndexOf("@") < 0) ? add + "@" + _CompanyAddress : add
117
                // select addr)
118
                //    .ToList().ForEach(ad => mail.CC.Add(new MailAddress(ad)));
119

    
120
                mail.To.Add(new MailAddress("kig881111@naver.com"));
121

    
122
                //Subject 
123
                //mail.Subject = subject;
124
                mail.Subject = subject.Replace('\r', ' ').Replace('\n', ' ');
125
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
126

    
127
                //Message           
128
                mail.Body = message;
129
                mail.BodyEncoding = System.Text.Encoding.UTF8;
130
                mail.IsBodyHtml = isHtmlMsg;
131

    
132
                //SmtpClient client = new SmtpClient(SmtpClientAddress);
133

    
134
                MailAddress sendAddress = new MailAddress("kig881111@gmail.com");
135

    
136

    
137
                SmtpClient client = null;
138
                //client = new SmtpClient
139
                //{
140
                //    Host = "smtp.gmail.com",
141
                //    EnableSsl = true,
142
                //    DeliveryMethod = SmtpDeliveryMethod.Network,
143
                //    Credentials = new System.Net.NetworkCredential("kig881111@gmail.com", "amlswud123!"),
144
                //    Timeout = 20000
145
                //};
146

    
147
                //mail = new MailMessage(sendAddress, sendAddress)
148
                //{
149
                //    Subject = "제목 없슴다",
150
                //    Body = "내용 없슴다"
151
                //};
152

    
153
                client = new SmtpClient
154
                {
155
                    Host = "smtp.gmail.com",
156
                    EnableSsl = true,
157
                    DeliveryMethod = SmtpDeliveryMethod.Network,
158
                    Credentials = new System.Net.NetworkCredential(sendAddress.Address, "amlswud123!"),
159
                    Timeout = 20000
160
                };
161

    
162
                //mail = new MailMessage(sendAddress, sendAddress)
163
                //{
164
                //    Subject = "Markup 메일 발송 테스트",
165
                //    Body = "메일 발송 테스트"
166
                //};
167

    
168
                client.Send(mail);
169

    
170

    
171
                //if (_Async)
172
                //{
173
                //    client.SendCompleted += new SendCompletedEventHandler(Smtpclient_SendCompleted);
174
                //    _token = Guid.NewGuid();
175
                //    client.SendAsync(mail, mail.Headers);
176
                //}
177
                //else
178
                //{
179
                //    client.Send(mail);
180
                //    _result = new SendEmailResult(true, null);
181
                //    GC.Collect(2);
182
                //    GC.Collect(2);
183
                //}
184
            }
185
            catch (Exception e)
186
            {
187
                _result = new SendEmailResult(false, e);
188
            }
189
        }
190

    
191
        void Smtpclient_SendCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
192
        {
193
            //if (_token == (Guid)e.UserState)
194
            //{
195
                EmailSendCompleted(this,new SendEmailResult(true, null));
196
                GC.Collect(2);
197
                GC.Collect(2);
198
            //}
199
        }
200

    
201
        public void Dispose()
202
        {
203
            mail.Dispose();
204
            GC.Collect(2);
205
            GC.Collect(2);
206
        }
207
    }
208
}
클립보드 이미지 추가 (최대 크기: 500 MB)