Example #1
0
        public void SendUserWelcomeEmail(User user)
        {
            MailMessage = new MailMessage(_cmsEntries.VideoClubEmailAddress,user.Email);
            MailMessage.Subject = _cmsEntries.WelcomeEmailSubject;
            MailMessage.Body = _cmsEntries.WelcomeEmailBody + user.Name;

            Console.WriteLine(MailMessage.ToString());
        }
Example #2
0
        public static string MailSending(string ToEmails, string Title, string Body, string AttachPath, string MailUser, string MailName, string MailHost, string MailPwd)
        {
            //string MailUser = ConfigurationManager.AppSettings["MailUser"].ToString();
            //string MailName = ConfigurationManager.AppSettings["MailName"].ToString();
            //string MailHost = ConfigurationManager.AppSettings["MailHost"].ToString();
            //string MailPwd = ConfigurationManager.AppSettings["MailPwd"].ToString();
            MailAddress from = new MailAddress(MailUser, MailName);
            MailMessage mail = new MailMessage();
            mail.Subject = Title;
            mail.From = from;
            string[] mailNames = (ToEmails + ";").Split(new char[]
			{
				';'
			});
            string[] array = mailNames;
            for (int i = 0; i < array.Length; i++)
            {
                string name = array[i];
                if (name != string.Empty)
                {
                    string displayName;
                    string address;
                    if (name.IndexOf('<') > 0)
                    {
                        displayName = name.Substring(0, name.IndexOf('<'));
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    else
                    {
                        displayName = string.Empty;
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    mail.To.Add(new MailAddress(address, displayName));
                }
            }
            mail.Body = Body;
            mail.BodyEncoding = Encoding.UTF8;
            mail.IsBodyHtml = true;
            mail.Priority = MailPriority.Normal;
            if (AttachPath != "")
            {
                mail.Attachments.Add(new Attachment(AttachPath));
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            }
            SmtpClient client = new SmtpClient();
            client.Host = MailHost;
            client.Port = 25;
            client.UseDefaultCredentials = false;
            client.Credentials = new NetworkCredential(MailUser, MailPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;
            client.Send(mail);
            return mail.ToString();
        }
Example #3
0
 /// <summary>
 /// Mail settings are obtained from root web.config
 /// </summary>
 /// <param name="mailMessage"></param>
 public static void SendEmail(MailMessage mailMessage)
 {
     var client = new SmtpClient();
     try
     {
         logger.Debug("Sending email: " + mailMessage.ToString());
         client.Send(mailMessage);
     }
     catch (Exception ex)
     {
         logger.Error("Error sending email " + ex.Message);
     }
 }
Example #4
0
        /// <summary>
        /// 发送邮件
        /// </summary>
        /// <param name="Dep_Email">发送人、支持发送多个人每个地址用 ; 号隔开</param>
        /// <param name="Mis_Name">任务名称</param>
        /// <param name="Mis_Describe">内容</param>
        /// <param name="File_Path">附件</param>
        /// <returns></returns>
        public static string MailSending(string Dep_Email, string Mis_Name, string Mis_Describe, string File_Path)
        {
            string MailUser = System.Configuration.ConfigurationManager.AppSettings["MailUser"].ToString();
            string MailName = System.Configuration.ConfigurationManager.AppSettings["MailName"].ToString();
            string MailHost = System.Configuration.ConfigurationManager.AppSettings["MailHost"].ToString();
            string MailPwd = System.Configuration.ConfigurationManager.AppSettings["MailPwd"].ToString();
            MailAddress from = new MailAddress(MailUser, MailName); //邮件的发件人
            MailMessage mail = new MailMessage();

            //设置邮件的标题
            mail.Subject = Mis_Name;//任务名称

            //设置邮件的发件人
            //Pass:如果不想显示自己的邮箱地址,这里可以填符合mail格式的任意名称,真正发mail的用户不在这里设定,这个仅仅只做显示用
            mail.From = from;

            //设置邮件的收件人
            string address = "";
            string displayName = "";
            /**/
            /*  这里这样写是因为可能发给多个联系人,每个地址用 ; 号隔开
一般从地址簿中直接选择联系人的时候格式都会是 :用户名1 < mail1 >; 用户名2 < mail 2>; 
因此就有了下面一段逻辑不太好的代码
如果永远都只需要发给一个收件人那么就简单了 mail.To.Add("收件人mail");
*/
            string[] mailNames = (Dep_Email + ";").Split(';');
            foreach (string name in mailNames)
            {
                if (name != string.Empty)
                {
                    if (name.IndexOf('<') > 0)
                    {
                        displayName = name.Substring(0, name.IndexOf('<'));
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    else
                    {
                        displayName = string.Empty;
                        address = name.Substring(name.IndexOf('<') + 1).Replace('>', ' ');
                    }
                    mail.To.Add(new MailAddress(address, displayName));
                }
            }

            //设置邮件的抄送收件人
            //这个就简单多了,如果不想快点下岗重要文件还是CC一份给领导比较好
            //mail.CC.Add(new MailAddress("[email protected]", "尊敬的领导");

            //设置邮件的内容
            mail.Body = Mis_Describe;
            //设置邮件的格式
            mail.BodyEncoding = System.Text.Encoding.UTF8;
            mail.IsBodyHtml = true;
            //设置邮件的发送级别
            mail.Priority = MailPriority.Normal;

            //设置邮件的附件,将在客户端选择的附件先上传到服务器保存一个,然后加入到mail中
            if (File_Path != "")
            {
                mail.Attachments.Add(new Attachment(File_Path));
                mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnSuccess;
            } SmtpClient client = new SmtpClient();
            //设置用于 SMTP 事务的主机的名称,填IP地址也可以了
            client.Host = MailHost;
            //设置用于 SMTP 事务的端口,默认的是 25
            client.Port = 25;
            client.UseDefaultCredentials = false;
            //这里才是真正的邮箱登陆名和密码, 我的用户名为 MailUser ,我的密码是 MailPwd
            client.Credentials = new System.Net.NetworkCredential(MailUser, MailPwd);
            client.DeliveryMethod = SmtpDeliveryMethod.Network;

            ////如果发送失败,SMTP 服务器将发送 失败邮件告诉我
            mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure;

            //都定义完了,正式发送了,很是简单吧!
            client.Send(mail);
            return mail.ToString();
        }
        public void TestMailWithHtmlBody()
        {          
            System.Text.StringBuilder builder = new StringBuilder();
            
            builder.Append("                                 \n\n\n                     \n  <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
            builder.Append("<HTML><HEAD>");
            builder.Append("<META http-equiv=Content-Type content=\"text/html; charset=iso-8859-1\">");
            builder.Append("<META content=\"MSHTML 6.00.2900.2180\" name=GENERATOR></HEAD>");
            builder.Append("<BODY><B>From:</B> Marc Bosma [[email protected]]<BR><B>Sent:</B> Friday, ");
            builder.Append("January 27, 2006 06:50 PM<BR><B>To:</B> Marc Bosma<BR><B>Subject:</B> Fwd: They ");
            builder.Append("found her<BR><BR><BR>---------- Forwarded message ----------<BR><SPAN ");
            builder.Append("class=gmail_quote>From: <B class=gmail_sendername>Wayne van Ry</B> &lt;<A ");
            builder.Append("href=\"mailto:[email protected]\">[email protected]</A>&gt;<BR>Date: Jan 20, 2006 ");
            builder.Append("10:56 AM<BR>Subject: They found her<BR>To: hano botha &lt;<A ");
            builder.Append("href=\"mailto:[email protected]\">[email protected]</A>&gt;, <A ");
            builder.Append("href=\"mailto:[email protected]\">[email protected]</A>, philip grobler ");
            builder.Append("");
            builder.Append("&lt;<A href=\"mailto:[email protected]\"> ");
            builder.Append("[email protected]</A>&gt;<BR><BR></SPAN><SPAN class=gmail_quote></SPAN>");
            builder.Append("<DIV>");
            builder.Append("<P><FONT face=\"Times New Roman\" size=3><SPAN ");
            builder.Append("style=\"FONT-SIZE: 12pt\"></SPAN></FONT>&nbsp;</P>");
            builder.Append("<P style=\"TEXT-ALIGN: center\" align=center><FONT face=Arial color=blue ");
            builder.Append("size=6><SPAN style=\"FONT-SIZE: 24pt; COLOR: blue; FONT-FAMILY: Arial\">They've ");
            builder.Append("</SPAN></FONT><FONT face=Arial color=navy size=6><SPAN ");
            builder.Append("style=\"FONT-SIZE: 24pt; COLOR: navy; FONT-FAMILY: Arial\">finally ");
            builder.Append("</SPAN></FONT><FONT face=Arial color=blue size=6><SPAN ");
            builder.Append("style=\"FONT-SIZE: 24pt; COLOR: blue; FONT-FAMILY: Arial\">found Popeye's ");
            builder.Append("Mom</SPAN></FONT><FONT face=Arial size=2><SPAN ");
            builder.Append("style=\"FONT-SIZE: 10pt; FONT-FAMILY: Arial\"> <FONT color=navy><SPAN ");
            builder.Append("style=\"COLOR: navy\">&nbsp;</SPAN></FONT></SPAN></FONT><BR><BR><BR><IMG ");
            builder.Append("height=400 src=\"cid:_2_03E0000060C40038FD80802570FC\" width=392><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55><BR><BR><BR><IMG ");
            builder.Append("height=50 src=\"cid:_1_0E14000062BC0038FD80802570FC\" width=55></P>");
            builder.Append("<P><FONT face=Arial size=3><SPAN ");
            builder.Append("style=\"FONT-SIZE: 12pt; FONT-FAMILY: Arial\"><BR></SPAN></FONT><BR>&nbsp; </P>");
            builder.Append("<P style=\"TEXT-ALIGN: center\" align=center><FONT face=\"Times New Roman\" ");
            builder.Append("size=3><SPAN style=\"FONT-SIZE: 12pt\"><BR><BR><IMG height=480 ");
            builder.Append("src=\"cid:_2_014C000066AC0038FD80802570FC\" width=640></SPAN></FONT></P>");
            builder.Append("<P style=\"MARGIN-BOTTOM: 12pt\"><FONT face=\"Times New Roman\" size=3><SPAN ");
            builder.Append("style=\"FONT-SIZE: 12pt\"><BR><BR></SPAN></FONT></P></DIV><BR>This transmission is ");
            builder.Append("confidential and intended solely for the person or<BR>organization to whom it is ");
            builder.Append("addressed. It may contain privileged and<BR>confidential information. If you are ");
            builder.Append("not the intended destination, you<BR>should not copy, distribute or take any ");
            builder.Append("action in reliance on it.<BR><BR>If you believe you received this transmission ");
            builder.Append("in error, please notify the sender.<BR><BR>This e-mail has been scanned for all ");
            builder.Append("viruses by Star Internet for IMD Plc. The service is powered by MessageLabs. For ");
            builder.Append("more information on a proactive anti-virus service working around the clock, ");
            builder.Append("around the globe, visit:<BR><A ");
            builder.Append("onclick=\"return top.js.OpenExtLink(window,event,this)\" ");
            builder.Append("href=\"http://www.star.net.uk\" ");
            builder.Append("target=_blank>http://www.star.net.uk</A><BR></BODY></HTML>");

            System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage("[email protected]", "[email protected],[email protected],[email protected]", "This is the subject", builder.ToString());
            
            message.Attachments.Add(new System.Net.Mail.Attachment(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\test.doc")));
            message.Attachments.Add(new System.Net.Mail.Attachment(Workshare.TestUtils.TestFileUtils.MakeRootPathAbsolute(@"\projects\Hygiene\src\TestDocuments\TestDoc.txt")));
            string emailString = message.ToString();
            System.IO.MemoryStream stream = new System.IO.MemoryStream();

            Workshare.Policy.Engine.EmailFormatHandler.Write(stream, message);

            Workshare.Policy.Engine.EmailFormatHandler readhandler = Workshare.Policy.Engine.EmailFormatHandler.ReadEmail(stream);

            Assert.AreEqual("This is the subject", readhandler.Subject);
            Assert.AreEqual(builder.ToString(), readhandler.Body);

            List<string>.Enumerator attachments = readhandler.GetEnumerator();

            Assert.IsTrue(attachments.MoveNext());
            Assert.AreEqual(@"test.doc", attachments.Current.ToLower());
            Assert.IsTrue(attachments.MoveNext());
            Assert.AreEqual(@"testdoc.txt", attachments.Current.ToLower());
            Assert.IsFalse(attachments.MoveNext());

            System.IO.MemoryStream memStream = new MemoryStream(UTF8Encoding.Default.GetBytes(builder.ToString()));
            
            IFile file = Workshare.Policy.Engine.FileFactory.Create(memStream, "html email body");
            Assert.AreEqual(Workshare.Policy.FileType.HTMLDocument, file.FileType);

            //System.IO.MemoryStream mailStream = new MemoryStream();
            //Workshare.Policy.Engine.EmailFormatHandler.Write(mailStream, message);
            //Workshare.Policy.Interfaces.IFile file2 = Workshare.Policy.Engine.FileFactory.Create(mailStream, "html email");
            //Workshare.Policy.Interfaces.IDocumentReader docReader = file2.Read();
        }
Example #6
0
        private void SendMessage(MailMessage message)
        {
            try
            {
                if (ShouldProcess(message.ToString()))
                {
                    _smtpClient.Send(message);
                    WriteVerbose(Properties.Resources.SmtpMailSent);

                    if (_passThru)
                    {
                        WriteObject(message);
                    }
                }
            }
            catch (InvalidOperationException ex)
            {
                this.ErrorHandler.WriteInvalidOperationError(_smtpClient, ex);
            }
            catch (SmtpException ex)
            {
                this.ErrorHandler.WriteSmtpSendMessageError(_smtpClient, ex);
            }
        }