//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 int SendTest(BaseMailAccount sender, MailMessage message) { LumiSoft.Net.SMTP.Client.SMTP_Client smtpClient; smtpClient = new LumiSoft.Net.SMTP.Client.SMTP_Client(); Mail_Message msg = CreateTestMail(message); MemoryStream m = MessageToStream(msg); try { smtpClient.Connect(sender.SendHost, sender.SendPort, sender.SendSsl); try { smtpClient.EhloHelo(sender.SendHost); smtpClient.Auth(new LumiSoft.Net.AUTH.AUTH_SASL_Client_Login(sender.UserName, sender.Password)); //smtpClient.Authenticate(Sender.UserName, Sender.Password); smtpClient.MailFrom(sender.UserName, -1); message.To.ToList().ForEach(p => smtpClient.RcptTo(p.Address)); smtpClient.SendMessage(m); } finally { smtpClient.Disconnect(); } return 0; } catch (Exception ex) { this.EncodeErrorMessage(ex); return -1; throw ex; } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="sender">发件账户</param> /// <param name="message"></param> public string SendMail(BaseMailAccount sender, MailMessage message) { LumiSoft.Net.SMTP.Client.SMTP_Client smtpClient; smtpClient = new LumiSoft.Net.SMTP.Client.SMTP_Client(); try { smtpClient.Connect(sender.SendHost, sender.SendPort, sender.SendSsl); Mail_Message msg = TransferMessage(message); MemoryStream m = MessageToStream(msg); try { smtpClient.EhloHelo(sender.SendHost); smtpClient.Auth(new LumiSoft.Net.AUTH.AUTH_SASL_Client_Login(sender.UserName,sender.Password)); //smtpClient.Authenticate(Sender.UserName, Sender.Password); smtpClient.MailFrom(sender.UserName, -1); message.To.ToList().ForEach(p => smtpClient.RcptTo(p.Address)); smtpClient.SendMessage(m); } finally { smtpClient.Disconnect(); } return "邮件已发送,请查收!"; } catch (Exception ex) { return "邮件发送失败,原因:" + ex.Message; } }
/// <summary> /// 發送eml文件 /// </summary> /// <param name="authhh"></param> /// <param name="toMail"></param> /// <param name="pathMel"></param> public static void SendMailToMel(AUTH_SASL_Client_Plain authhh, string toMail, string pathMel, string smtp, int port, bool ssl) { try { using (LumiSoft.Net.SMTP.Client.SMTP_Client client = new LumiSoft.Net.SMTP.Client.SMTP_Client()) { client.Connect(smtp, port, ssl); client.EhloHelo(smtp); client.Auth(authhh); client.RcptTo(toMail); var msg = Mail_Message.ParseFromFile(pathMel); MemoryStream stream = new MemoryStream(); msg.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8); stream.Position = 0; client.SendMessage(stream); client.Disconnect(); } } catch (Exception ex) { throw ex; } }
private static void SendMessage(BaseMailAccount Sender, BaseMailAccount Receiver, MemoryStream m) { LumiSoft.Net.SMTP.Client.SMTP_Client smtpClient; smtpClient = new LumiSoft.Net.SMTP.Client.SMTP_Client(); try { smtpClient.Connect(Sender.SendHost, Sender.SendPort, Sender.SendSsl); try { smtpClient.EhloHelo(Sender.SendHost); smtpClient.Auth(new LumiSoft.Net.AUTH.AUTH_SASL_Client_Login(Sender.UserName, Sender.Password)); // smtpClient.Authenticate(Sender.UserName, Sender.Password); smtpClient.MailFrom(Sender.UserName, -1); smtpClient.RcptTo(Receiver.UserName); smtpClient.SendMessage(m); } finally { smtpClient.Disconnect(); } } finally { smtpClient = null; } }
//lumisoft smtp public void sendMailSmtp(string adressFrom, string psw, int port, string hostname, bool ssl, string stradresseRecu, string stradresseCC, string strSujet, string strbodymailhtml, int indexPriorite) { var Host = new LumiSoft.Net.SMTP.Client.SMTP_Client(); Host.Connect(hostname, port, ssl); Host.EhloHelo(hostname); Host.Auth(Host.AuthGetStrongestMethod(adressFrom, psw)); LumiSoft.Net.Mail.Mail_Message mailMsg = new LumiSoft.Net.Mail.Mail_Message(); mailMsg.MessageID = LumiSoft.Net.MIME.MIME_Utils.CreateMessageID(); string[] adresseRecu = getListAdress(stradresseRecu); string[] adressCC = getListAdress(stradresseCC); string mailSujet = strSujet; for (int i = 0; i < adresseRecu.Length; i++) { if (!string.IsNullOrEmpty(adresseRecu[i])) { mailMsg.To = new LumiSoft.Net.Mail.Mail_t_AddressList(); } mailMsg.To.Add(new LumiSoft.Net.Mail.Mail_t_Mailbox(adresseRecu[i], adresseRecu[i])); } for (int i = 0; i < adressCC.Length; i++) { if (!string.IsNullOrEmpty(adressCC[i])) { mailMsg.Cc = new LumiSoft.Net.Mail.Mail_t_AddressList(); } mailMsg.Cc.Add(new LumiSoft.Net.Mail.Mail_t_Mailbox(adressCC[i], adressCC[i])); } mailMsg.Subject = mailSujet; mailMsg.From = new LumiSoft.Net.Mail.Mail_t_MailboxList(); mailMsg.From.Add(new LumiSoft.Net.Mail.Mail_t_Mailbox(adressFrom, adressFrom)); string mailbody = HttpUtility.UrlDecode(strbodymailhtml); switch (indexPriorite) { case 0: mailMsg.Priority = "normal"; break; case 1: mailMsg.Priority = "urgent"; break; case 2: mailMsg.Priority = "non-urgent"; break; } var body = new MIME_b_Text(MIME_MediaTypes.Text.html); mailMsg.Body = body; //il faut avoir l'autorisation faute de quoi il va envoyer "Body must be bounded to some entity first" pour l'exception body.SetText(MIME_TransferEncodings.Base64, Encoding.UTF8, mailbody); if (WebUserControl1.Emaildatastatic.getInstance().getDataTable().Rows.Count > 0) { foreach (DataRow dr in WebUserControl1.Emaildatastatic.getInstance().getDataTable().Rows) { } } try { } catch (System.Net.Mail.SmtpException ex) { Console.WriteLine(ex.ToString()); } mailMsg.Dispose(); Console.WriteLine("Goodbye."); }