public int SendMail(MailAccount Sender, MailAccount Receiver, string mailSubject) { //获得要处理的邮件信 try { string mailUser = Sender.UserName; string mailPass = Sender.Password; string mailFrom = Sender.UserName; string smtpserver = Sender.SmtpHost; CDO.Message message = new CDO.Message(); message.To = Receiver.UserName; message.TextBody = "Test Send"; //邮件标题 message.Subject = "Test"; message.From = Sender.UserName; message.Sender = Sender.UserName; CDO.IConfiguration iConfig = message.Configuration; ADODB.Fields fields = iConfig.Fields; fields["http://schemas.microsoft.com/cdo/configuration/sendusing"].Value = 2; fields["http://schemas.microsoft.com/cdo/configuration/sendemailaddress"].Value = mailFrom; fields["http://schemas.microsoft.com/cdo/configuration/sendusername"].Value = mailFrom; fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"].Value = mailPass; fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"].Value = 1; fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"].Value = smtpserver; fields.Update(); message.Send(); message = null; return 0; } catch (Exception ex) { return -1; } }
//public MailMessage GenMail(string mailSubject) //{ // // 生成邮件 // MailMessage message = new MailMessage("*****@*****.**", "*****@*****.**"); // message.Sender = new MailAddress("*****@*****.**"); // message.Subject = mailSubject; // message.Body = "*****@*****.**" + " Send " + mailSubject; // message.Priority = MailPriority.High; // return message; //} public int SendMail(MailAccount Sender, MailAccount Receiver, string mailSubject) { Mail_Message msg = Create_PlainText_Html_Attachment_Image(Receiver.UserName, Sender.MailAddress, Sender.UserName); MemoryStream m = new MemoryStream(); MIME_Encoding_EncodedWord ew = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8); msg.ToStream(m, ew, Encoding.UTF8, false); m.Position = 0; LumiSoft.Net.SMTP.Client.SMTP_Client smtpClient; smtpClient = new LumiSoft.Net.SMTP.Client.SMTP_Client(); try { smtpClient.Connect(Sender.SmtpHost, Sender.SendPort, Sender.SendSsl); try { smtpClient.EhloHelo(Sender.SmtpHost); smtpClient.Authenticate(Sender.UserName, Sender.Password); smtpClient.MailFrom(Sender.UserName, -1); smtpClient.RcptTo(Receiver.UserName); smtpClient.SendMessage(m); } finally { smtpClient.Disconnect(); } } finally { smtpClient = null; } return 0; }
public MailMessage GenMail(MailAccount Sender, MailAccount Receiver, string mailSubject) { // 生成邮件 MailMessage message = new MailMessage(Sender.UserName, Receiver.UserName); MailAddress _Sender = new MailAddress(Sender.UserName); message.Sender = _Sender; message.Subject = mailSubject; message.Body = Sender.UserName + " Send " + mailSubject; message.Priority = MailPriority.High; return message; }
/// <summary> /// 通过邮件发送数据文件的方法 /// </summary> /// <param name="Sender">发送的账号</param> /// <param name="Receiver">接收的账号</param> /// <param name="obj">发送的对象</param> /// <param name="mailSubject">发送邮件的主题</param> /// <param name="attName">附件的名称</param> public int SendEntity(MailAccount Sender, MailAccount Receiver, string mailSubject) { try { //生成邮件 MailMessage message = this.GenMail(Sender, Receiver, mailSubject); //邮箱设置 InitSmtpClient(Sender); //发送邮件 return this.SendMessage(message); } catch (Exception ex) { EncodeErrorMessage(ex); return -2; } }
private void InitSmtpClient(MailAccount _Sender) { // 指定 smtp 服务器地址 _Client.Host = _Sender.SmtpHost; // 指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 _Client.Port = _Sender.SendPort; // 将smtp的出站方式设为 Network _Client.DeliveryMethod = SmtpDeliveryMethod.Network; // SMTP服务器需要身份认证 _Client.UseDefaultCredentials = true; // smtp服务器是否启用SSL加密 _Client.EnableSsl = _Sender.SendSsl; _Client.Credentials = new NetworkCredential(_Sender.UserName, _Sender.Password); }