티스토리 뷰

프로그래밍/ASP.NET

C# SMTP 대량메일 보내기

쇠주는참이슬 2012. 10. 23. 10:01

출처 : Code Snipet


C# SMTP로 대량 메일 발송하기.

Soket 이용

It’s easy to use System.net.mail namespace to send emails from asp.net. But the problem is system.net.mail.send() method does not close the connection to SMTP server. This is causing SMTP 4.2.2 error if you try to send multiple emails. While as per Microsoft this issue has been resolved in .net Framework 4.0 but people who are using 3.5 its still the issue. The workaround is to use sockets and System.Web.Mail namespace. Here is the sample code to send SMTP email using C# 3.5 -

workaround may be set maxtimeout=1

.NET Framework 4.0 has new property called SMTPClient.Dispose() to fix the issue

001using System;
002using System.Text;
003using System.IO;
004using System.Net.Sockets;
005using System.Net;
006using System.Web.Mail;
007 
008namespace mySMTP
009{
010    /// <summary>
011    /// provides methods to send email via smtp direct to mail server
012    /// </summary>
013    public class SmtpDirect
014    {
015        /// <summary>
016        /// Get / Set the name of the SMTP mail server
017        /// </summary>
018        private static string SmtpServer = "outbound-mail.yourorg.com";
019        private enum SMTPResponse : int
020        {
021            CONNECT_SUCCESS = 220,
022            GENERIC_SUCCESS = 250,
023            DATA_SUCCESS = 354,
024            QUIT_SUCCESS = 221
025        }
026        public static bool Send(MailMessage message)
027        {
028            IPHostEntry IPhst = Dns.Resolve(SmtpServer);
029            IPEndPoint endPt = new IPEndPoint(IPhst.AddressList[0], 25);
030            Socket s = new Socket(endPt.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
031            s.Connect(endPt);
032 
033            if (!Check_Response(s, SMTPResponse.CONNECT_SUCCESS))
034            {
035                s.Close();
036                return false;
037            }
038 
039            Senddata(s, string.Format("HELO {0}\r\n", Dns.GetHostName()));
040            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
041            {
042                s.Close();
043                return false;
044            }
045 
046            Senddata(s, string.Format("MAIL From: {0}\r\n", message.From));
047            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
048            {
049 
050                s.Close();
051                return false;
052            }
053 
054            string _To = message.To;
055            string[] Tos = _To.Split(new char[] { ';' });
056            foreach (string To in Tos)
057            {
058                Senddata(s, string.Format("RCPT TO: {0}\r\n", To));
059                if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
060                {
061                    s.Close();
062                    return false;
063                }
064            }
065 
066            if (message.Cc != null)
067            {
068                Tos = message.Cc.Split(new char[] { ';' });
069                foreach (string To in Tos)
070                {
071                    Senddata(s, string.Format("RCPT TO: {0}\r\n", To));
072                    if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
073                    {
074                        s.Close();
075                        return false;
076                    }
077                }
078            }
079 
080            StringBuilder Header = new StringBuilder();
081            Header.Append("From: " + message.From + "\r\n");
082            Tos = message.To.Split(new char[] { ';' });
083            Header.Append("To: ");
084            for (int i = 0; i < Tos.Length; i++)
085            {
086                Header.Append(i > 0 ? "," "");
087                Header.Append(Tos[i]);
088            }
089            Header.Append("\r\n");
090            if (message.Cc != null)
091            {
092                Tos = message.Cc.Split(new char[] { ';' });
093                Header.Append("Cc: ");
094                for (int i = 0; i < Tos.Length; i++)
095                {
096                    Header.Append(i > 0 ? "," "");
097                    Header.Append(Tos[i]);
098                }
099                Header.Append("\r\n");
100            }
101            Header.Append("Date: ");
102            Header.Append(DateTime.Now.ToString("ddd, d M y H:m:s z"));
103            Header.Append("\r\n");
104            Header.Append("Subject: " + message.Subject + "\r\n");
105            Header.Append("X-Mailer: SMTPDirect v1\r\n");
106            string MsgBody = message.Body;
107            if (!MsgBody.EndsWith("\r\n"))
108                MsgBody += "\r\n";
109            if (message.Attachments.Count > 0)
110            {
111                Header.Append("MIME-Version: 1.0\r\n");
112                Header.Append("Content-Type: multipart/mixed; boundary=unique-boundary-1\r\n");
113                Header.Append("\r\n");
114                Header.Append("This is a multi-part message in MIME format.\r\n");
115                StringBuilder sb = new StringBuilder();
116                sb.Append("--unique-boundary-1\r\n");
117                sb.Append("Content-Type: text/plain\r\n");
118                sb.Append("Content-Transfer-Encoding: 7Bit\r\n");
119                sb.Append("\r\n");
120                sb.Append(MsgBody + "\r\n");
121                sb.Append("\r\n");
122 
123                foreach (object in message.Attachments)
124                {
125                    MailAttachment a = o as MailAttachment;
126                    byte[] binaryData;
127                    if (a != null)
128                    {
129                        FileInfo f = new FileInfo(a.Filename);
130                        sb.Append("--unique-boundary-1\r\n");
131                        sb.Append("Content-Type: application/octet-stream; file=" + f.Name + "\r\n");
132                        sb.Append("Content-Transfer-Encoding: base64\r\n");
133                        sb.Append("Content-Disposition: attachment; filename=" + f.Name + "\r\n");
134                        sb.Append("\r\n");
135                        FileStream fs = f.OpenRead();
136                        binaryData = new Byte[fs.Length];
137                        long bytesRead = fs.Read(binaryData, 0, (int)fs.Length);
138                        fs.Close();
139                        string base64String = System.Convert.ToBase64String(binaryData, 0, binaryData.Length);
140 
141                        for (int i = 0; i < base64String.Length; )
142                        {
143                            int nextchunk = 100;
144                            if (base64String.Length - (i + nextchunk) < 0)
145                                nextchunk = base64String.Length - i;
146                            sb.Append(base64String.Substring(i, nextchunk));
147                            sb.Append("\r\n");
148                            i += nextchunk;
149                        }
150                        sb.Append("\r\n");
151                    }
152                }
153                MsgBody = sb.ToString();
154            }
155 
156            Senddata(s, ("DATA\r\n"));
157            if (!Check_Response(s, SMTPResponse.DATA_SUCCESS))
158            {
159                s.Close();
160                return false;
161            }
162            Header.Append("\r\n");
163            Header.Append(MsgBody);
164            Header.Append(".\r\n");
165            Header.Append("\r\n");
166            Header.Append("\r\n");
167            Senddata(s, Header.ToString());
168            if (!Check_Response(s, SMTPResponse.GENERIC_SUCCESS))
169            {
170                s.Close();
171                return false;
172            }
173 
174            Senddata(s, "QUIT\r\n");
175            Check_Response(s, SMTPResponse.QUIT_SUCCESS);
176            s.Close();
177            return true;
178        }
179        private static void Senddata(Socket s, string msg)
180        {
181            byte[] _msg = Encoding.ASCII.GetBytes(msg);
182            s.Send(_msg, 0, _msg.Length, SocketFlags.None);
183        }
184        private static bool Check_Response(Socket s, SMTPResponse response_expected)
185        {
186            string sResponse;
187            int response;
188            byte[] bytes = new byte[1024];
189            while (s.Available == 0)
190            {
191                System.Threading.Thread.Sleep(100);
192            }
193 
194            s.Receive(bytes, 0, s.Available, SocketFlags.None);
195            sResponse = Encoding.ASCII.GetString(bytes);
196            response = Convert.ToInt32(sResponse.Substring(0, 3));
197            if (response != (int)response_expected)
198                return false;
199            return true;
200        }
201    }
202 
203}

Usage

1string sBody = string.Empty;
2System.Web.Mail.MailMessage message = new System.Web.Mail.MailMessage();
3message.From = "def@hotmail.com";
4message.To="abc@yahoo.com";
5message.Subject = "Hello subject";
6sBody = "Hello World" ;
7mySMTP.Send(message)

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/12   »
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
글 보관함