songjun
2024-09-04 cc908053e0b5075fbfd27350b6da4b39bca9e550
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Net;
using System.Net.Mail;
 
namespace sbcLabSystem.Service.Task
    public class Email
    {
        /// <summary>
        /// 发送者
        /// </summary>
        public string mailFrom { get; set; }
 
        /// <summary>
        /// 收件人
        /// </summary>
        public string[] mailToArray { get; set; }
 
        /// <summary>
        /// 抄送
        /// </summary>
        public string[] mailCcArray { get; set; }
 
        /// <summary>
        /// 标题
        /// </summary>
        public string mailSubject { get; set; }
 
        /// <summary>
        /// 正文
        /// </summary>
        public string mailBody { get; set; }
 
        /// <summary>
        /// 发件人密码
        /// </summary>
        public string mailPwd { get; set; }
 
        /// <summary>
        /// SMTP邮件服务器
        /// </summary>
        public string host { get; set; }
        public int SSLPort { get; set; }
        public bool IsEnableSSL { get; set; }
 
        /// <summary>
        /// 正文是否是html格式
        /// </summary>
        public bool isbodyHtml { get; set; }
 
        /// <summary>
        /// 附件
        /// </summary>
        public string[] attachmentsPath { get; set; }
 
        public bool Send()
        {
            //使用指定的邮件地址初始化MailAddress实例
            MailAddress maddr = new MailAddress(mailFrom);
            //初始化MailMessage实例
            MailMessage myMail = new MailMessage();
 
 
            //向收件人地址集合添加邮件地址
            if (mailToArray != null)
            {
                for (int i = 0; i < mailToArray.Length; i++)
                {
                    myMail.To.Add(mailToArray[i].ToString());
                }
            }
 
            //向抄送收件人地址集合添加邮件地址
            if (mailCcArray != null)
            {
                for (int i = 0; i < mailCcArray.Length; i++)
                {
                    myMail.CC.Add(mailCcArray[i].ToString());
                }
            }
            //发件人地址
            myMail.From = maddr;
 
            //电子邮件的标题
            myMail.Subject = mailSubject;
 
            //电子邮件的主题内容使用的编码
            myMail.SubjectEncoding = Encoding.UTF8;
 
            //电子邮件正文
            myMail.Body = mailBody;
            //电子邮件正文的编码
            myMail.BodyEncoding = Encoding.Default;
 
            myMail.Priority = MailPriority.High;
 
            myMail.IsBodyHtml = isbodyHtml;
 
            //在有附件的情况下添加附件
            try
            {
                if (attachmentsPath != null && attachmentsPath.Length > 0)
                {
                    Attachment attachFile = null;
                    foreach (string path in attachmentsPath)
                    {
                        attachFile = new Attachment(path);
                        myMail.Attachments.Add(attachFile);
                    }
                }
            }
            catch (Exception err)
            {
                throw new Exception("在添加附件时有错误:" + err);
            }
            SmtpClient smtp = new SmtpClient();
            smtp.DeliveryMethod = SmtpDeliveryMethod.Network; 
            //指定发件人的邮件地址和密码以验证发件人身份
            smtp.Credentials = new System.Net.NetworkCredential(mailFrom, mailPwd);
            //设置SMTP邮件服务器
            smtp.Host = host;
            smtp.EnableSsl = IsEnableSSL;
            if (smtp.EnableSsl)
            {
                smtp.Port = SSLPort;
            }
            try 
            {
                BatchService.Framework.Utility.LogHelper.Debug(string.Format("发送参数:{0}:{1}:{2}:{3}:{4}", mailFrom,mailPwd,host, IsEnableSSL,SSLPort));
                //将邮件发送到SMTP邮件服务器 
                smtp.Send(myMail);
                return true;
 
            }
            catch (System.Net.Mail.SmtpException ex) 
            {
                BatchService.Framework.Utility.LogHelper.Error(ex.ToString());
                throw ex;
            }
 
        }
    }
}