/// <summary> /// 发送邮件 /// </summary> /// <param name="message">消息体,若未填充From属性,则将以配置填充</param> /// <exception cref="Adf.SmtpException"></exception> /// <exception cref="Adf.ConfigException">No configuration or set smtp sender</exception> /// <exception cref="ArgumentOutOfRangeException">tos is empty</exception> /// <exception cref="ArgumentException">No set receiver</exception> /// <exception cref="ArgumentNullException"></exception> /// <returns></returns> public virtual void Send(Adf.Mail.MailMessage message) { if (message == null) { throw new ArgumentNullException("message"); } if (this.Enabled == false) { return; } if (message.To.Count == 0) { throw new ArgumentException("message", "No set receiver"); } if (message.From == null) { if (!string.IsNullOrEmpty(Name)) { message.From = new Adf.Mail.MailAddress(this.GetSenderAddress(), Name); } else { message.From = new Adf.Mail.MailAddress(this.GetSenderAddress()); } } if (string.IsNullOrEmpty(Host)) { //无代理发送 SendNoPorxy(message); } else { using (var smtpClient = new Adf.Mail.SmtpClient(Host, Port)) { smtpClient.EnableSsl = this.SSLEnabled; var account = this.Account; var password = this.Password; if (!string.IsNullOrEmpty(account) && !string.IsNullOrEmpty(password)) { smtpClient.UserName = account; smtpClient.Password = password; } smtpClient.Send(message); } } }
public void Test() { var host = "192.168.199.10"; var port = 25; var from = "*****@*****.**"; var to = "*****@*****.**"; to = "*****@*****.**"; var domain = "caping.co.id"; var selector = "newsletter17110"; var dkim = new Adf.Mail.DKIM(domain, selector); dkim.LoadKey(@"-----BEGIN RSA PRIVATE KEY----- MIICXQIBAAKBgQCxcjbutjZfE1trW5oFt7t4AnjDRHeHwbi2AGE5n1M8YZSO2fGi fBnsSy/qNoaKwoROhNl9S0mya7Q5odloyN3IEVoUCZjnd3onTsZ4vmXD/Ei4r0+S /ZdUxVtMxQoqRd/4NgW0M+OyGez2rNPfvbF18aa0RdIZxaLRDcpp4RZ1rwIDAQAB AoGAT4/fk479uAmM3wk2eUPVeczZ6uvjEGrK8EghT93hS1yRaK/OCUXNtcZMmJ6U ... beMUEFuoc9NvfOLP7RkCQQCcow3n3YOX/yJB+2EN2272Uo07hbVxcA7sbUYLfYuh ba3kqofe7BP7QpMwqZmLALDngIp4htRrTYFehzZ6zavB -----END RSA PRIVATE KEY----- "); var message = new Adf.Mail.MailMessage(); message.Subject = "You password expired"; message.IsBodyHtml = false; message.Body = @"hi\r\nyou password expired, please check.\r\n\r\n services."; message.From = new Adf.Mail.MailAddress(from); message.To.Add(new Adf.Mail.MailAddress(to)); message.Dkim = dkim; //message.Save("c:\\" + message.MessageId.Replace("@", "_") + ".eml"); //message.Save(Console.OpenStandardOutput()); using (var smtpClient = new Adf.Mail.SmtpClient(host, port)) { smtpClient.Send(message); } Console.WriteLine(message.MessageId.Replace("@", "_")); Console.WriteLine("Send a mail, entry continue."); Console.ReadLine(); }