MailSendHelper.cs 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Net.Mail;
  5. using System.Text;
  6. namespace Muchinfo.PC.Common.Helpers
  7. {
  8. public class MailSendHelper
  9. {
  10. /// <summary>
  11. /// 客户端发送邮件
  12. /// </summary>
  13. /// <param name="ToAddress">收件人邮箱地址,多个收件人用;隔开</param>
  14. /// <param name="Address">发送人邮箱地址</param>
  15. /// <param name="Password">发送人邮箱密码</param>
  16. /// <param name="Host">服务器</param>
  17. /// <param name="port">端口</param>
  18. /// <param name="DisplayName">默认发送人名称</param>
  19. /// <param name="Title">主题</param>
  20. /// <param name="Content">内容</param>
  21. /// <param name="Attachments">附件地址,多个附件用;隔开</param>
  22. public static bool MailSend(
  23. string ToAddress,
  24. string Address,
  25. string Password,
  26. string Host,
  27. int port,
  28. string DisplayName,
  29. string Title,
  30. string Content,
  31. string Attachments,
  32. out string Error)
  33. {
  34. Error = "";
  35. char[] ch = new char[] { ';' };
  36. try
  37. {
  38. MailMessage ms = new MailMessage();
  39. foreach (string str in ToAddress.Split(ch))
  40. {
  41. ms.To.Add(str);
  42. }
  43. ms.From = new MailAddress(Address, DisplayName, System.Text.Encoding.UTF8);
  44. ms.Priority = MailPriority.High;
  45. ms.Subject = Title;
  46. ms.SubjectEncoding = System.Text.Encoding.UTF8;
  47. ms.Body = Content;
  48. if (!string.IsNullOrWhiteSpace(Attachments))
  49. {
  50. foreach (string str in Attachments.Split(ch))
  51. {
  52. Attachment ath = new Attachment(str);
  53. ms.Attachments.Add(ath);
  54. }
  55. }
  56. ms.BodyEncoding = System.Text.Encoding.UTF8;
  57. ms.IsBodyHtml = false;
  58. SmtpClient sc = new SmtpClient();
  59. sc.Credentials = new System.Net.NetworkCredential(Address, Password);
  60. sc.Host = Host;
  61. sc.Port = port;
  62. sc.Send(ms);
  63. return true;
  64. }
  65. catch (Exception ex)
  66. {
  67. Error = ex.Message;
  68. return false;
  69. }
  70. }
  71. }
  72. }