Ejemplo n.º 1
0
 /// <summary>
 ///  包装发送的邮件的相关信息
 /// </summary>
 /// <param name="model"></param>
 /// <returns></returns>
 public static MailMessage PacketMessage(SendModel model)
 {
     var mail = new MailMessage();
     mail.From = model.From;
     mail.Subject = model.Subject;
     mail.Body = model.Body;
     mail.BodyEncoding = System.Text.Encoding.UTF8;
     mail.IsBodyHtml = true;
     mail.Priority = model.Priority;
     if (model.To != null)
         foreach (var item in model.To)
             mail.To.Add(item);
     if (model.Attachments != null)
         foreach (var item in model.Attachments)
             mail.Attachments.Add(item);
     return mail;
 }
Ejemplo n.º 2
0
        public void SendMail(SendModel model)
        {
            var msg = SendHelper.PacketMessage(model);
            var host = SendHelper.GetHost(model.From.Address);

            client.Credentials = new System.Net.NetworkCredential(model.From.Address, model.Password);
            client.Port = host.Port;
            client.Host = host.Host;
            client.EnableSsl = host.Ssl;

            try
            {
                client.Send(msg);
            }
            catch (System.Net.Mail.SmtpException sex)
            {
                throw (sex);
            }
            catch (Exception ex)
            {
                throw (ex);
            }
        }
Ejemplo n.º 3
0
        static void SMTPTest()
        {
            using (SmtpClient smtpClient = new SmtpClient())
            {
                SendModel model = new SendModel
                {
                    Body = "测试测试这是测试",
                    Subject = "邮箱测试",
                    From = new System.Net.Mail.MailAddress("*****@*****.**", "ValueMember"),
                    Date = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                    Password = "******"
                };
                model.AddTo("*****@*****.**", "Hey ValueCEO");
                model.AddTo("*****@*****.**", "Hey ValueCEO");

                smtpClient.SendMail(model);
            }
        }