■ 구글 G메일을 보내는 방법을 보여준다.
▶ WebMailManager.cs
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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
using System.Collections.Generic; using System.Net; using System.Net.Mail; using System.Net.Mime; /// <summary> /// 웹 메일 관리자 /// </summary> public class WebMailManager { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 호스트 /// </summary> private string host = "smtp.gmail.com"; /// <summary> /// 송신자 메일 주소 /// </summary> private string fromMailAddress; /// <summary> /// 송신자명 /// </summary> private string fromName; /// <summary> /// 송신자 패스워드 /// </summary> private string fromPassword; /// <summary> /// 참조 메일 주소 리스트 /// </summary> private List<MailAddress> bccMailAddressList; /// <summary> /// 첨부 리스트 /// </summary> private List<Attachment> attachmentList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 호스트 - Host /// <summary> /// 호스트 /// </summary> public string Host { get { return this.host; } set { this.host = value; } } #endregion #region 송신자 메일 주소 - SenderMailAddress /// <summary> /// 송신자 메일 주소 /// </summary> public string SenderMailAddress { get { return this.fromMailAddress; } set { this.fromMailAddress = value; } } #endregion #region 송신자명 - SenderName /// <summary> /// 송신자명 /// </summary> public string SenderName { get { return this.fromName; } set { this.fromName = value; } } #endregion #region 송신자 패스워드 - SenderPassword /// <summary> /// 송신자 패스워드 /// </summary> public string SenderPassword { get { return this.fromPassword; } set { this.fromPassword = value; } } #endregion #region 참조 메일 주소 리스트 - BCCMailAddressList /// <summary> /// 참조 메일 주소 리스트 /// </summary> public List<MailAddress> BCCMailAddressList { get { return this.bccMailAddressList; } set { this.bccMailAddressList = value; } } #endregion #region 첨부 리스트 - BCCMailAddressList /// <summary> /// 첨부 리스트 /// </summary> public List<Attachment> AttachmentList { get { return this.attachmentList; } set { this.attachmentList = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - WebMailManager(fromMailAddress, fromName, fromPassword) /// <summary> /// 생성자 /// </summary> /// <param name="fromMailAddress">송신자 메일 주소</param> /// <param name="fromName">송신자명</param> /// <param name="fromPassword">송신자 패스워드</param> public WebMailManager(string fromMailAddress, string fromName, string fromPassword) { this.fromMailAddress = fromMailAddress; this.fromName = fromName; this.fromPassword = fromPassword; this.bccMailAddressList = new List<MailAddress>(); this.attachmentList = new List<Attachment>(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 참조 메일 주소 리스트 지우기 - ClearBCCMailAddressList() /// <summary> /// 참조 메일 주소 리스트 지우기 /// </summary> public void ClearBCCMailAddressList() { this.bccMailAddressList.Clear(); } #endregion #region 참조 추가하기 - AddBCC(mailAddress) /// <summary> /// 참조 추가하기 /// </summary> /// <param name="mailAddress">메일 주소</param> public void AddBCC(MailAddress mailAddress) { this.bccMailAddressList.Add(mailAddress); } #endregion #region 참조 추가하기 - AddBCC(mailAddress, displayName) /// <summary> /// 참조 추가하기 /// </summary> /// <param name="mailAddress">메일 주소</param> /// <param name="displayName">표시명</param> public void AddBCC(string mailAddress, string displayName) { AddBCC(new MailAddress(mailAddress, displayName)); } #endregion #region 첨부 리스트 지우기 - ClearAttachmentList() /// <summary> /// 첨부 리스트 지우기 /// </summary> public void ClearAttachmentList() { this.attachmentList.Clear(); } #endregion #region 첨부 추가하기 - AddAttachment(attachment) /// <summary> /// 첨부 추가하기 /// </summary> /// <param name="attachment">첨부</param> public void AddAttachment(Attachment attachment) { this.attachmentList.Add(attachment); } #endregion #region 첨부 추가하기 - AddAttachment(filePath, contentType) /// <summary> /// 첨부 추가하기 /// </summary> /// <param name="filePath">파일 경로</param> /// <param name="contentType">컨텐트 타입</param> public void AddAttachment(string filePath, ContentType contentType) { AddAttachment(new Attachment(filePath, contentType)); } #endregion #region 보내기 - Send(receiveMailAddress, receiveName, subject, isHTEMLContent, content) /// <summary> /// 보내기 /// </summary> /// <param name="receiveMailAddress">수신자 메일 주소</param> /// <param name="receiveName">수신자명</param> /// <param name="subject">주제</param> /// <param name="isHTEMLContent">HTML 내용 여부</param> /// <param name="content">내용</param> public void Send(string receiveMailAddress, string receiveName, string subject, bool isHTEMLContent, string content) { MailAddress fromMailAddress = new MailAddress(this.fromMailAddress, this.fromName); MailAddress toMailAddress = new MailAddress(receiveMailAddress , receiveName ); SmtpClient smtpClient = new SmtpClient(); smtpClient.Host = this.host; smtpClient.Port = 587; smtpClient.EnableSsl = true; smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Credentials = new NetworkCredential(fromMailAddress.Address, this.fromPassword); smtpClient.Timeout = 20000; using(MailMessage mailMessage = new MailMessage(fromMailAddress, toMailAddress)) { mailMessage.Subject = subject; mailMessage.IsBodyHtml = isHTEMLContent; mailMessage.Body = content; foreach(MailAddress mailAddress in this.bccMailAddressList) { mailMessage.Bcc.Add(mailAddress); } foreach(Attachment attachment in this.attachmentList) { mailMessage.Attachments.Add(attachment); } smtpClient.Send(mailMessage); } } #endregion } |