Exemplo n.º 1
0
        /// <summary>
        /// send one or more emails
        /// </summary>
        /// <param name="from">sender's email</param>
        /// <param name="arTo">an array of EmailAddress objects to receive</param>
        /// <param name="subject">the subject</param>
        /// <param name="body">the body</param>
        /// <returns>null on success, or an error description when failed</returns>
        public string SendEmail(string from, ArrayList arTo, string subject, string body)
        {
            try
            {
                // no email format verification
                SmtpConfig.VerifyAddresses = false;
                // the message to send
                MailMessage msg = new MailMessage();
                msg.Charset = "gbk";
                msg.From = new EmailAddress(from, Conf.CreateFromWebConfig().get("AppNameShort") as string);
                msg.To = arTo;
                msg.Subject = subject;
                msg.Body = body;
                // send it
                new Smtp
                    (conf.get("primarySmtpHost") as string,
                    conf.get("emailAccountName") as string,
                    conf.get("emailAccountPwd") as string,
                    Helper.Function.IntSafeConvert(conf.get("primarySmtpPort")))
                    .SendMail(msg);

                return null;
            }
            catch (Exception e)
            {
                return e.Message + e.StackTrace;
            }
        }
        public void TestAttachmentSort()
        {
            // test MailMessage(string, string) constructor
                MailMessage msg		= new MailMessage("*****@*****.**", "*****@*****.**");

                // test MailMessage(EamilAddress, EmailAddress) constructor
                EmailAddress from 	= new EmailAddress("*****@*****.**", "Fake Sender");
                EmailAddress to		= new EmailAddress("*****@*****.**", "Fake Recipient");

                Attachment att = new Attachment(@"..\lib\test attachments\test.htm");
                msg.AddAttachment(att);

                Attachment att2 = new Attachment(@"..\lib\test attachments\test.gar");
                msg.AddAttachment(att2);

                Attachment att3 = new Attachment(@"..\lib\test attachments\test.zip");
                msg.AddAttachment(att3);

                Attachment att4 = new Attachment(@"..\lib\test attachments\test.longextension");
                msg.AddAttachment(att4);

                ArrayList attachments = msg.Attachments;
                attachments.Sort();

                Console.WriteLine("\r\n ----- MailMessage.Attachments after sorting -----");
                foreach (Attachment attachment in attachments)
                {
                    Console.WriteLine(attachment.Name);
                }
        }
        public void TestAllConstructors()
        {
            try
            {
                // test MailMessage(string, string) constructor
                MailMessage msg		= new MailMessage("*****@*****.**", "*****@*****.**");

                // test MailMessage(EamilAddress, EmailAddress) constructor
                EmailAddress from 	= new EmailAddress("*****@*****.**", "Fake Sender");
                EmailAddress to		= new EmailAddress("*****@*****.**", "Fake Recipient");
                msg					= new MailMessage(from, to);
            }
            catch(SmtpException)
            { Assertion.Fail("TestAllConstructors threw SmtpException"); }
        }
Exemplo n.º 4
0
        /// <summary>
        /// 同步发送
        /// </summary>
        /// <param name="subject">主题</param>
        /// <param name="body">邮件内容</param>
        /// <param name="attachment">附件</param>
        /// <param name="receptMail">接收地址[email protected]</param>
        /// <param name="sendMail">发送邮件地址</param>
        /// <param name="sendName">发送名称</param>
        /// <param name="userName">服务器账号</param>
        /// <param name="password">服务器账户密码</param>
        /// <param name="smtpHost">SMTP服务器</param>
        /// <param name="smtpPort">SMTP服务器端口</param>
        private void SendEmailSync(string subject, string body, string attachment, string receptMail, string sendMail, string sendName, string userName, string password, string smtpHost, int smtpPort)
        {
            try
            {
                SmtpConfig.VerifyAddresses = false;
                EmailAddress from = new EmailAddress(sendMail, sendName);
                EmailAddress to = new EmailAddress(receptMail);
                MailMessage msg = new MailMessage(from, to);
                msg.Charset = "gb2312";
                msg.Subject = subject;
                msg.HtmlBody = HttpUtility.HtmlDecode(body);
                Smtp smtp = new Smtp(smtpHost, smtpPort);

                // 在SMTP服务器上的用户名和密码
                smtp.Username = userName;
                smtp.Password = password;
                smtp.SendMail(msg);
            }
            catch
            {
            }
        }
        protected void Init()
        {
            sender 			= 	"*****@*****.**";
            recipient		= 	"*****@*****.**";
            cc				=	"*****@*****.**";
            senderName 		= 	"FromName";
            recipientName	= 	"ToName";
            ccName			=	"ccName";
            subject			= 	"Mail Message Test\r\n";
            body			=	"Hello from MailMessageTest";
            htmlBody 		= 	"<HTML><HEAD></HEAD><BODY bgColor=\"#00ffff\"><b>Hello Jane. This is the body of the HTML mail message.</b></BODY></HTML>";
            charset			= 	"us-ascii";

            senderEmail 	= new EmailAddress(sender, senderName);
            recipientEmail 	= new EmailAddress(recipient, recipientName);
            ccEmail			= new EmailAddress(cc, ccName);

            msg 			= new MailMessage(senderEmail, recipientEmail);
            msg.AddRecipient("*****@*****.**", AddressType.To);
            msg.AddRecipient("*****@*****.**", AddressType.To);
            msg.Subject 	= subject;
            msg.Body 		= body;
            msg.Charset		= charset;
            msg.Priority 	= MailPriority.High;

            msg.HtmlBody = htmlBody.ToString();
            msg.AddRecipient(ccEmail, AddressType.To);
            msg.AddRecipient(ccEmail, AddressType.Cc);
            msg.AddCustomHeader("X-Something", "Value");
            msg.AddCustomHeader("X-SomethingElse", "Value");
            msg.AddAttachment(@"..\lib\test attachments\test.jpg");
            msg.AddAttachment(@"..\lib\test attachments\test.htm");
            Attachment att = new Attachment(@"..\lib\test attachments\test.zip");
            msg.AddAttachment(att);

            msg.Notification = true;
        }
        public void TestCopy()
        {
            // test MailMessage(string, string) constructor
                MailMessage msg		= new MailMessage("*****@*****.**", "*****@*****.**");

                // test MailMessage(EamilAddress, EmailAddress) constructor
                EmailAddress from 	= new EmailAddress("*****@*****.**", "Fake Sender");
                EmailAddress to		= new EmailAddress("*****@*****.**", "Fake Recipient");
                msg					= new MailMessage(from, to);
                msg.Body			= "test body for clone test";

                MailMessage msg2 = msg.Copy();

                Assertion.AssertEquals(msg.To, msg2.To);
                Assertion.AssertEquals(msg.From, msg2.From);
                Assertion.AssertEquals(msg.Body, msg2.Body);
        }
Exemplo n.º 7
0
        public static string SendPassword(string strEmail)
        {

            try
            {
                string smtpHost = "smtp.163.com";
                int smtpPort = 25;
                string senderEmail = "*****@*****.**";
                string senderName = "zhuangrong";
                string recipientEmail = strEmail;
                string subject = "您在PowerSite的密码";
                string body = "这是来自PowerSite的密码回复邮件。";

                SmtpConfig.VerifyAddresses = false;
                EmailAddress from = new EmailAddress(senderEmail, senderName);
                EmailAddress to = new EmailAddress(recipientEmail);
                MailMessage msg = new MailMessage(from, to);
                msg.Charset = "gb2312";
                msg.Subject = subject;
                msg.Body = body;

                Smtp smtp = new Smtp(smtpHost, smtpPort);
                smtp.Username = "******";
                smtp.Password = "******";
                smtp.SendMail(msg);
                return "OK";

            }
            catch (MalformedAddressException mfa)
            {
                return mfa.Message.Substring(0, mfa.Message.Length - 4);
            }
            catch (SmtpException se)
            {
                return se.Message.Substring(0, se.Message.Length - 4);
            }
            catch (Exception ex)
            {
                return ex.Message.Substring(0, ex.Message.Length - 4);
            }
        }       
Exemplo n.º 8
0
        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; }
        }
Exemplo n.º 9
0
        /// <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 " + host + "\r\n");
            }
            else
                WriteToStream(ref nwstream, "HELO " + host + "\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();
        }
Exemplo n.º 10
0
 /// <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);
 }
Exemplo n.º 11
0
        //========================================================================
        // 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);
        }
Exemplo n.º 12
0
        protected void Init()
        {
            subject 		= "Test Subject. International chars: ËÇÅÃÄÄÅÂÀèéêëìíîïñaÿc + 功能超强得不得了";
                body 			= "Hello Jane.\r\n This is the body of the mail message. \r\nInternational chars: ËÇÅÃÄÄÅÂÀèéêë\r\nìíîïñaÿc";

                htmlBody = new StringBuilder();
                htmlBody.Append("<HTML><HEAD></HEAD><BODY bgColor=\"#00ffff\"><b>Hello Jane. This is the body of the HTML mail message. International chars: ËÇÅÃÄÄÅÂÀèéêëìíîïñaÿc</b></BODY></HTML>");

                senderAddress 		= new EmailAddress("sender@localhost", "John Sender");
                recipientAddress 	= new EmailAddress("administrator@localhost", "Jane Doe");

                replyToAddress 		= new EmailAddress("replyTo@localhost", "ReplyTo Name");
                ccAddress 		= new EmailAddress("ccAddress@localhost", "CC Name");
                bccAddress		= new EmailAddress("bccAddress@localhost", "BCC Name");

                msg 			= new MailMessage("*****@*****.**", "*****@*****.**");

                smtpHost 		= "localhost";
                smtpPort 		= 25;

                smtp			= new Smtp();
                smtp.Host 		= smtpHost;
                smtp.Port 		= smtpPort;

                // Add Smtp event listener
                SmtpEventListener listener = new SmtpEventListener(smtp);
        }