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; } }
public ReceiverMail(Guid Receiver) { _Pop3Client = new POP3_Client(); _Account = MailAccountHelper.GetMailAccount(Receiver); _InvalidMails = new ManageMail(); // _FE = new FleetEntities(); _ReceiverObjects = new List<ReceiverObject>(); }
public ISendMail GetSendClassBySender(BaseMailAccount Sender) { foreach (ISendMail ISM in _SendObjects) { if (ISM.CanSend(Sender)) { return ISM; } } return null; }
public BaseMailMessage GenMail(BaseMailAccount Sender, BaseMailAccount Receiver, object obj, string mailSubject, string attName) { // 生成邮件 BaseMailMessage message = new BaseMailMessage(); message.Sender = Sender.MailAddress; message.DisplayName = Sender.DisplayName; message.From = Sender.MailAddress; message.To = Receiver.MailAddress; message.Subject = mailSubject; message.Body = mailSubject + "\r\n" + "来自" + Sender.UserName; message.Priority = MailPriority.High; message.Attachments.Add(CreateMessageAttachment(obj, attName)); 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(BaseMailAccount Sender, BaseMailAccount Receiver, object obj, string mailSubject, string attName) { try { //生成邮件 BaseMailMessage message = this.GenMail(Sender, Receiver, obj, mailSubject, attName); //发送邮件 return this.SendMessage(Sender, Receiver, message); } catch (Exception ex) { return -3; } }
public int SendTest(BaseMailAccount Sender, BaseMailAccount Receiver) { Mail_Message msg = CreateTestMail(Sender, Receiver); MemoryStream m = MessageToStream(msg); try { SendMessage(Sender, Receiver, m); return 0; } catch (Exception ex) { this.EncodeErrorMessage(ex); return -1; throw ex; } }
public int SendNormalMail(BaseMailAccount sender, MailMessage message) { try { var sc = new SendContainer(); ISendMail ISM = sc.GetSendClassBySender(sender); if (ISM != null) { return ISM.SendMail(sender, message); } return -1; } catch (Exception ex) { return -2; } }
/// <summary> /// 发送邮件 /// </summary> /// <param name="sender">发件账户</param> /// <param name="message"></param> public int SendMail(BaseMailAccount sender, MailMessage message) { try { //邮箱设置 InitSmtpClient(sender); //发送邮件 int intSend = SendMessage(message); //断开连接 CloseConnection(); return intSend; } catch (Exception ex) { this.EncodeErrorMessage(ex); return -3; } }
private static BaseMailAccount SetDefaultProperty() { var account = new BaseMailAccount { MailAddress = "", DisplayName = "", UserName = "", Password = "", ReceiveHost = "", ReceivePort = 110, ReceiveSsl = false, SendHost = "", SendPort = 25, SendSsl = false, SendStartTLS = false }; return account; }
public static BaseMailAccount GetMailAccountFromAddr(string address, string displayName, string loginUser, string originPassword, string pop3Host,int receivePort,bool receiveSsl,string sendHost,int sendPort,bool sendSsl,bool startTls) { var account = new BaseMailAccount { MailAddress = address, DisplayName = displayName, UserName = loginUser, Password = originPassword, ReceiveHost = pop3Host, ReceivePort = receivePort, ReceiveSsl = receiveSsl, SendHost = sendHost, SendPort = sendPort, SendSsl = sendSsl, SendStartTLS = startTls }; return account; }
public int SendTest(BaseMailAccount Sender, BaseMailAccount Receiver) { try { //生成邮件 MailMessage msg = CreateTestMail(Sender, Receiver); //邮箱设置 InitSmtpClient(Sender); //发送邮件 int intSend = SendMessage(msg); //断开连接 CloseConnection(); return intSend; } catch (Exception ex) { this.EncodeErrorMessage(ex); return -3; } }
private BaseMailAccount CreateSender() { BaseMailAccount _Sender = new BaseMailAccount(); _Sender.DisplayName = tbMailAddress.Text; _Sender.MailAddress = tbMailAddress.Text; _Sender.UserName = tbUserName.Text; _Sender.Password = tbPassword.Text; _Sender.SendPort = int.Parse(tbSmtpPort.Text); _Sender.SendHost = this.tbSmtpName.Text; _Sender.SendSsl = this.cbSendSSl.Checked; return _Sender; }
private BaseMailAccount CreateReceiver() { BaseMailAccount _Receiver = new BaseMailAccount(); _Receiver.MailAddress = tbReceiveAccount.Text; _Receiver.UserName = tbReceiveAccount.Text; return _Receiver; }
/// <summary> /// 测试邮件发送 /// </summary> /// <param name="sender">发送的账号</param> /// <param name="receiver">接收的账号</param> public int SendTestMail(BaseMailAccount sender, string receiver) { try { var sc = new SendContainer(); ISendMail ISM = sc.GetSendClassBySender(sender); if (ISM != null) { MailMessage msg=new MailMessage(); msg.From = (new MailAddress(sender.MailAddress)); msg.To.Add(new MailAddress(receiver)); return ISM.SendTest(sender,msg); } return -1; } catch (Exception ex) { return -2; } }
/// <summary> /// 测试邮件发送 /// </summary> /// <param name="Sender">发送的账号</param> /// <param name="Receiver">接收的账号</param> public int SendTestMail(BaseMailAccount Sender, BaseMailAccount Receiver) { try { SendContainer sc = new SendContainer(); ISendMail ISM = sc.GetSendClassBySender(Sender); if (ISM != null) { return ISM.SendTest(Sender, Receiver); } return -1; } catch (Exception ex) { return -2; } }
private int SendMessage(BaseMailAccount Sender, BaseMailAccount Receiver, BaseMailMessage message) { try { SendContainer sc = new SendContainer(); ISendMail ISM = sc.GetSendClassBySender(Sender); if(ISM != null) { return ISM.SendMail(Sender, Receiver, message); } return -1; } catch (Exception ex) { return -2; } }
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; } }
private Mail_Message CreateTestMail(BaseMailAccount Sender, BaseMailAccount Receiver) { Mail_Message msg = new Mail_Message(); msg.MimeVersion = "1.0"; msg.MessageID = MIME_Utils.CreateMessageID(); msg.Date = DateTime.Now; msg.From = new Mail_t_MailboxList(); msg.From.Add(new Mail_t_Mailbox(Sender.DisplayName, Sender.MailAddress)); msg.To = new Mail_t_AddressList(); msg.To.Add(new Mail_t_Mailbox(Receiver.MailAddress, Receiver.MailAddress)); msg.Subject = "Test Send Mail"; //设置回执通知 //string notifyEmail = SystemConfig.Default.DispositionNotificationTo; //if (!string.IsNullOrEmpty(notifyEmail) && ValidateUtil.IsEmail(notifyEmail)) //{ // msg.DispositionNotificationTo = new Mail_t_Mailbox(notifyEmail, notifyEmail); //} #region MyRegion string BodyText = "这是一份测试邮件,来自<font color=red><b>厦门至正测试程序</b></font>"; CreateBodyPart(msg, BodyText); //测试邮件不发送附件 #endregion return msg; }
private MailMessage CreateTestMail(BaseMailAccount Sender, BaseMailAccount Receiver) { MailMessage msg = new MailMessage(Sender.MailAddress, Receiver.MailAddress); msg.Sender = new MailAddress(Sender.MailAddress, Sender.DisplayName); msg.Subject = "Test Send Mail By Net.Mail"; msg.Body = "这是一份测试邮件,来自厦门至正测试程序"; msg.Priority = MailPriority.High; //测试邮件不发送附件 return msg; }
private void InitSmtpClient(BaseMailAccount Sender) { // 指定 smtp 服务器地址 _Client.Host = Sender.SendHost; // 指定 smtp 服务器的端口,默认是25,如果采用默认端口,可省去 _Client.Port = Sender.SendPort; // 将smtp的出站方式设为 Network _Client.DeliveryMethod = SmtpDeliveryMethod.Network; // SMTP服务器需要身份认证 _Client.UseDefaultCredentials = true; // smtp服务器是否启用SSL加密 _Client.EnableSsl = false; //设置发信凭据 _Client.Credentials = new NetworkCredential(Sender.UserName, Sender.Password); }
private static BaseMailAccount SetPropertyFromAddr(MailAddress addr) { var account = new BaseMailAccount { MailAddress = addr.Address, DisplayName = addr.DisplayName, UserName = addr.LoginUser, Password = addr.OriginPassword, ReceiveHost = addr.Pop3Host, ReceivePort = addr.ReceivePort, ReceiveSsl = addr.ReceiveSSL, SendHost = addr.SmtpHost, SendPort = addr.SendPort, SendSsl = addr.SendSSL, SendStartTLS = addr.StartTLS }; return account; }
public bool CanSend(BaseMailAccount Sender) { return Sender.SendSsl == false; }
public ReceiverMail(BaseMailAccount receiver) { _pop3Client = new POP3_Client(); _receiveAccount = receiver;//需要提前设置好接收账号用户密码及接收服务器,接收端口,是否ssl _invalidMails = new ManageMail(); }
public bool CanSend(BaseMailAccount Sender) { return true; }