private bool CheckMailMessage(MailMessage message) { string returnMessage = "Mail Message is missing "; if (message.To == null || message.To.Count <= 0) { throw new SmtpException(returnMessage + "'To:' field"); } else { return true; } }
/// <summary>Sends a mail message using supplied MailMessage and Smtp properties</summary> /// <param name="msg">MailMessage instance</param> /// <param name="host">SMTP host address</param> /// <param name="port">Port used to connect to host</param> /// <example> /// <code> /// MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); /// msg.Subject = "Hi"; /// msg.Body = "Hello Joe Smith." /// Smtp smtp = new Smtp(); /// smtp.SendMail(msg, "mail.OpenSmtp.com", 25); /// </code> /// </example> public void SendMail(MailMessage msg, string host, int port) { this.host = host; this.port = port; SendMail(msg); }
/// <summary>Sends a mail message using supplied MailMessage</summary> /// <param name="msg">MailMessage instance</param> /// <example> /// <code> /// MailMessage msg = new MailMessage("*****@*****.**", "*****@*****.**"); /// msg.Subject = "Hi"; /// msg.Body = "Hello Joe Smith." /// Smtp smtp = new Smtp("mail.OpenSmtp.com", 25); /// smtp.SendMail(msg); /// </code> /// </example> public void SendMail(MailMessage msg) { NetworkStream nwstream = GetConnection(); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.HELO_REPLY); if (this.username != null && this.username.Length > 0 && this.password != null && this.password.Length > 0) { WriteToStream(ref nwstream, "EHLO " + Dns.GetHostName() + "\r\n"); } else WriteToStream(ref nwstream, "HELO " + Dns.GetHostName() + "\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); // Authentication is used if the u/p are supplied AuthLogin(ref nwstream); WriteToStream(ref nwstream, "MAIL FROM: <" + msg.from.address + ">\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); SendRecipientList(ref nwstream, msg.recipientList); SendRecipientList(ref nwstream, msg.ccList); SendRecipientList(ref nwstream, msg.bccList); WriteToStream(ref nwstream, "DATA\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.START_INPUT); OnStartedMessageTransfer(EventArgs.Empty); WriteToStream(ref nwstream, msg.ToString() + "\r\n.\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.OK); OnEndedMessageTransfer(EventArgs.Empty); WriteToStream(ref nwstream, "QUIT\r\n"); CheckForError(ReadFromStream(ref nwstream), ReplyConstants.QUIT); CloseConnection(); }
//======================================================================== // METHODS //======================================================================== /// <summary>Sends a mail message using supplied MailMessage properties as string params</summary> /// <param name="from">RFC 822 formatted email sender address</param> /// <param name="to">RFC 822 formatted email recipient address</param> /// <param name="subject">Subject of the email message</param> /// <param name="body">Text body of the email message</param> /// <example> /// <code> /// Smtp smtp = new Smtp("mail.OpenSmtp.com", 25); /// smtp.SendMail("*****@*****.**", "*****@*****.**", "Hi", "Hello Joe Smith"); /// </code> /// </example> public void SendMail(string from, string to, string subject, string body) { MailMessage msg = new MailMessage(from, to); msg.subject = subject; msg.body = body; SendMail(msg); }