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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
|
public class MailHelper { private static string MailServer = Config.GetValue("MailHost"); private static string MailUserName = Config.GetValue("MailUserName"); private static string MailPassword = Config.GetValue("MailPassword"); private static string MailName = Config.GetValue("MailName");
private static string MailServerN = Config.GetValue("host"); private static string MailUserNameN = Config.GetValue("mailFrom"); private static string MailPasswordN = Config.GetValue("mailPwd"); private static string MailNameN = Config.GetValue("mailFrom"); public static bool Send(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false) { try { MailMessage message = new MailMessage(); message.To.Add(new MailAddress(to)); message.From = new MailAddress(MailUserName, MailName); message.BodyEncoding = Encoding.GetEncoding(encoding); message.Body = body; message.SubjectEncoding = Encoding.GetEncoding(encoding); message.Subject = subject; message.IsBodyHtml = isBodyHtml;
SmtpClient smtpclient = new SmtpClient(MailServer, 25); smtpclient.Credentials = new System.Net.NetworkCredential(MailUserName, MailPassword); smtpclient.EnableSsl = enableSsl; smtpclient.Send(message); return true; } catch (Exception ex) { Console.WriteLine(ex.ToString()); return false; } } public static bool SendMine(string to, string subject, string body, string encoding = "UTF-8", bool isBodyHtml = true, bool enableSsl = false) { try { MailMessage message = new MailMessage(); message.To.Add(new MailAddress(to)); message.From = new MailAddress(MailUserNameN, MailNameN); message.BodyEncoding = Encoding.GetEncoding(encoding); message.Body = body; message.SubjectEncoding = Encoding.GetEncoding(encoding); message.Subject = subject; message.IsBodyHtml = isBodyHtml;
SmtpClient smtpclient = new SmtpClient(MailServerN, 25); smtpclient.Credentials = new System.Net.NetworkCredential(MailUserNameN, MailPasswordN); smtpclient.EnableSsl = enableSsl; smtpclient.Send(message); return true; } catch (Exception ex) { throw ex; } }
public static void SendByThread(string to, string title, string body, int port = 25) { new Thread(new ThreadStart(delegate () { try { SmtpClient smtp = new SmtpClient(); smtp.Host = MailServer; smtp.Port = port; smtp.Credentials = new NetworkCredential(MailUserName, MailPassword); MailMessage objMailMessage = new MailMessage(); objMailMessage.Priority = MailPriority.High; objMailMessage.From = new MailAddress(MailUserName, "提醒", System.Text.Encoding.UTF8); objMailMessage.To.Add(to); objMailMessage.Subject = title.Trim(); objMailMessage.SubjectEncoding = System.Text.Encoding.UTF8; objMailMessage.Body = body.Trim(); objMailMessage.IsBodyHtml = true; objMailMessage.BodyEncoding = System.Text.Encoding.UTF8; smtp.Send(objMailMessage); } catch (Exception ex) { Console.WriteLine(ex.ToString()); }
})).Start(); } public static void Send(MailAccount account, MailModel mailModel) { try { MailMessage mailMessage = new MailMessage(); mailMessage.From = new MailAddress(account.Account, account.AccountName); if (!string.IsNullOrEmpty(mailModel.To)) { var ToArray = mailModel.To.Split(','); foreach (var item in ToArray) { mailMessage.To.Add(new MailAddress(item)); } } if (!string.IsNullOrEmpty(mailModel.CC)) { var CCArray = mailModel.CC.Split(','); foreach (var item in CCArray) { mailMessage.CC.Add(new MailAddress(item)); } } if (!string.IsNullOrEmpty(mailModel.Bcc)) { var BccArray = mailModel.Bcc.Split(','); foreach (var item in BccArray) { mailMessage.Bcc.Add(new MailAddress(item)); } } mailMessage.Subject = mailModel.Subject; mailMessage.Body = mailModel.BodyText; mailMessage.IsBodyHtml = true; mailMessage.Priority = MailPriority.Normal; mailMessage.SubjectEncoding = Encoding.UTF8; mailMessage.BodyEncoding = Encoding.UTF8; mailMessage.Headers.Add("X-Priority", "3"); mailMessage.Headers.Add("X-MSMail-Priority", "Normal"); mailMessage.Headers.Add("X-Mailer", "Microsoft Outlook Express 6.00.2900.2869"); mailMessage.Headers.Add("X-MimeOLE", "Produced By Microsoft MimeOLE V6.00.2900.2869"); mailMessage.Headers.Add("ReturnReceipt", "1"); using (SmtpClient smtpClient = new SmtpClient(account.SMTPHost, account.SMTPPort)) { smtpClient.UseDefaultCredentials = false; smtpClient.Credentials = new NetworkCredential(account.Account, account.Password); smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network; smtpClient.Send(mailMessage); } } catch (Exception ex) { throw ex; } } public static void Delete(MailAccount account, string UID) { try { using (POP3_Client pop3Client = new POP3_Client()) { pop3Client.Connect(account.POP3Host, account.POP3Port, false); pop3Client.Login(account.Account, account.Password); if (pop3Client.Messages.Count > 0) { foreach (POP3_ClientMessage messages in pop3Client.Messages) { if (messages.UID == UID) { messages.MarkForDeletion(); } } } } } catch (Exception ex) { throw ex; } } public static List<MailModel> Get(MailAccount account, int receiveCount) { try { var filePath = DirFileHelper.GetAbsolutePath("~/Resource/EmailFile/"); var resultList = new List<MailModel>(); using (POP3_Client pop3Client = new POP3_Client()) { pop3Client.Connect(account.POP3Host, account.POP3Port, account.Ssl); pop3Client.Login(account.Account, account.Password); var messages = pop3Client.Messages; if (receiveCount == messages.Count) { return resultList; } for (int i = messages.Count - 1; receiveCount <= i; i--) { var messageItem = messages[i]; var messageHeader = Mail_Message.ParseFromByte(messageItem.MessageToByte()); resultList.Add(new MailModel() { UID = messageItem.UID, To = messageHeader.From == null ? "" : messageHeader.From[0].Address, ToName = messageHeader.From == null ? "" : messageHeader.From[0].DisplayName, Subject = messageHeader.Subject, BodyText = messageHeader.BodyHtmlText, Attachment = GetFile(filePath, messageHeader.GetAttachments(true, true), messageItem.UID), Date = messageHeader.Date, }); } } return resultList; } catch (Exception ex) { throw ex; } } private static List<MailFile> GetFile(string filePath, MIME_Entity[] messageFile, string UID) { var resultList = new List<MailFile>(); foreach (MIME_Entity entity in messageFile) { var fileName = entity.ContentType.Param_Name; var fileByte = (MIME_b_SinglepartBase)entity.Body; var fileId = UID + "_" + fileName; DirFileHelper.CreateFile(filePath + fileId, fileByte.Data); var fileSize = DirFileHelper.GetFileSize(filePath + fileId); resultList.Add(new MailFile { FileId = fileId, FileName = fileName, FileSize = DirFileHelper.ToFileSize(fileSize) }); } return resultList; } }
|