欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SMTP email from C#

程序员文章站 2022-03-06 20:51:46
/// /// 一人一附件发送邮件 /// 2017-05-17 涂聚文 GeovinDu /// /// 接收邮件人 /// 邮件标题 /// 邮件内容 /// 是否網頁 /// /// 附件 /// ... ......
       /// <summary>
        /// 一人一附件发送邮件
        ///  2017-05-17 涂聚文  geovindu
        /// </summary>
        /// <param name="to">接收邮件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="msg">邮件内容</param>
        /// <param name="bodyhtml">是否網頁</param>
        /// <param name="attachmentstream"></param>
        /// <param name="attachmentfilename">附件</param>
        /// <returns></returns>
        public static bool sendemaildu(string to, string subject, string msg, bool bodyhtml, memorystream attachmentstream,string attachmentfilename)
        {
            bool sendok = false;
            try
            {
                system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
                newmsg.bodyencoding = system.text.encoding.utf8;
                newmsg.headersencoding = system.text.encoding.utf8;
                newmsg.subjectencoding = system.text.encoding.utf8;

                system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
                //smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
                //smtpclient.port = 25;
                //smtpclient.enablessl = false;
                if (attachmentstream != null && attachmentfilename != null)
                {

                    // add an attachment.
                    //string sfile = @"c:\source\lns_is.txt";
                    //mailattachment oattch = new mailattachment(sfile, mailencoding.base64);
                    //string filepath = @"c:\testemail\skype11月黑咭生日會員.xls";
                    //memorystream memstream = new memorystream();
                    //using (filestream filestream = file.openread(filepath))
                    //{

                    //    memstream.setlength(filestream.length);
                    //    filestream.read(memstream.getbuffer(), 0, (int)filestream.length);
                    //}

                    newmsg.attachments.add(new attachment(attachmentstream, attachmentfilename)); //mediatypenames.image.jpeg)                
                  

                    //system.net.mail.attachment attachment = new system.net.mail.attachment(attachmentstream, attachmentfilename);
                    //system.net.mime.contentdisposition disposition = attachment.contentdisposition;
                    //disposition.filename = attachmentfilename;
                    //disposition.dispositiontype = system.net.mime.dispositiontypenames.attachment;
                    //newmsg.attachments.add(attachment);

                }
                newmsg.isbodyhtml = bodyhtml;
               
                smtpclient.send(newmsg);
                sendok = true;
                //return sent_ok;
            }
            catch (exception ex)
            {
                ex.message.tostring();
                attachmentstream.close();
                sendok = false;
                //"error: " + ex.message + "<br/><br/>inner exception: "+ ex.innerexception;

                
            }
            attachmentstream.close();
            return sendok;

        }
        /// <summary>
        /// 一人一附件发送邮件
        /// 2017-05-17 涂聚文  geovindu
        /// </summary>
        /// <param name="to">接收邮件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="msg">邮件内容</param>
        /// <param name="bodyhtml">是否網頁</param>
        /// <param name="attachmentfilename">附件</param>
        /// <returns></returns>
        public static bool sendemaildu(string to, string subject, string msg, bool bodyhtml, string attachmentfilename)
        {
            memorystream attachmentstream = new memorystream();
            bool sendok = false;
            try
            {
                system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
                newmsg.bodyencoding = system.text.encoding.utf8;
                newmsg.headersencoding = system.text.encoding.utf8;
                newmsg.subjectencoding = system.text.encoding.utf8;

                system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
                //smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
                //smtpclient.port = 25;
                //smtpclient.enablessl = false;
                if (attachmentstream != null && attachmentfilename != null)
                {

                    // add an attachment.
                    //string sfile = @"c:\source\lns_is.txt";
                    //mailattachment oattch = new mailattachment(sfile, mailencoding.base64);
                    //string filepath = @"c:\testemail\skype11月黑咭生日會員.xls";
                    //memorystream memstream = new memorystream();
                    //using (filestream filestream = file.openread(filepath))
                    //{

                    //    memstream.setlength(filestream.length);
                    //    filestream.read(memstream.getbuffer(), 0, (int)filestream.length);
                    //}

                    newmsg.attachments.add(new attachment(attachmentstream, attachmentfilename)); //mediatypenames.image.jpeg)                


                    //system.net.mail.attachment attachment = new system.net.mail.attachment(attachmentstream, attachmentfilename);
                    //system.net.mime.contentdisposition disposition = attachment.contentdisposition;
                    //disposition.filename = attachmentfilename;
                    //disposition.dispositiontype = system.net.mime.dispositiontypenames.attachment;
                    //newmsg.attachments.add(attachment);

                }
                newmsg.isbodyhtml = bodyhtml;                
                smtpclient.send(newmsg);
                sendok = true;
                //return sent_ok;
            }
            catch (exception ex)
            {
                ex.message.tostring();
                attachmentstream.close();
                sendok = false;
                //"error: " + ex.message + "<br/><br/>inner exception: "+ ex.innerexception;


            }
            attachmentstream.close();
            return sendok;

        }

        /// <summary>
        /// 一人多附件发送邮件
        /// 2019-06-18 涂聚文 geovindu
        /// </summary>
        /// <param name="to">接收邮件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="msg">邮件内容</param>
        /// <param name="bodyhtml">是否網頁</param>
        /// <param name="attachmentfiles">多附件</param>
        /// <returns></returns>
        public static bool sendemaildufiles(string to, string subject, string msg, bool bodyhtml, list<string> attachmentfiles)
        {
            memorystream attachmentstream = new memorystream();
            bool sendok = false;
            try
            {
                system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
                newmsg.bodyencoding = system.text.encoding.utf8;
                newmsg.headersencoding = system.text.encoding.utf8;
                newmsg.subjectencoding = system.text.encoding.utf8;

                system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
                //smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
                //smtpclient.port = 25;
                //smtpclient.enablessl = false;
               


                if (attachmentstream != null && attachmentfiles != null)
                {
 
                 
                    foreach (string filename in attachmentfiles)
                    {
                        newmsg.attachments.add(new attachment(attachmentstream, filename)); //mediatypenames.image.jpeg)   
                       
                    }     

                }
                newmsg.isbodyhtml = bodyhtml;               
                smtpclient.send(newmsg);
                sendok = true;
            }
            catch (exception ex)
            {
                ex.message.tostring();
                attachmentstream.close();
                sendok = false;
            }
            attachmentstream.close();
            return sendok;
        }

        /// <summary>
        /// 多人多附件发送邮件
        /// 不考虑统计每封邮件发送状态(另外考虑完善代码)
        ///  2019-06-18 涂聚文 geovindu
        /// </summary>
        /// <param name="tos">多人接收邮件人</param>
        /// <param name="subject">邮件标题</param>
        /// <param name="msg">邮件内容</param>
        /// <param name="bodyhtml">是否網頁</param>
        /// <param name="attachmentfiles">多附件</param>
        /// <returns></returns>
        public static bool sendemailsandfiles(list<string> tos, string subject, string msg, bool bodyhtml, list<string> attachmentfiles)
        {
            memorystream attachmentstream = new memorystream();
            bool sendok = false;
            list<attachment> all = new list<attachment>();
            try
            {
                if (attachmentstream != null && attachmentfiles != null)
                {
                    foreach (string filename in attachmentfiles)
                    {

                        all.add(new attachment(attachmentstream, filename));
                    }
                }
                if (tos.count > 0)
                {


                    foreach (string to in tos)
                    {
                        system.net.mail.mailmessage newmsg = new system.net.mail.mailmessage(system.configuration.configurationmanager.appsettings["mailcfg"], to, subject, msg);
                        newmsg.bodyencoding = system.text.encoding.utf8;
                        newmsg.headersencoding = system.text.encoding.utf8;
                        newmsg.subjectencoding = system.text.encoding.utf8;
                        system.net.mail.smtpclient smtpclient = new system.net.mail.smtpclient();
                        //smtpclient.credentials = new networkcredential("geovindu@dusystem.com","888888");
                        //smtpclient.port = 25;
                        //smtpclient.enablessl = false;
                        if (attachmentstream != null && attachmentfiles != null)
                        {
                            foreach (attachment attname in all)
                            {
                                newmsg.attachments.add(attname); //mediatypenames.image.jpeg)   

                            }
                        }
                        newmsg.isbodyhtml = bodyhtml;                        
                        smtpclient.send(newmsg);
                        sendok = true;
                   }
                }
            }
            catch (exception ex)
            {
                ex.message.tostring();
                attachmentstream.close();
                sendok = false;
            }
            attachmentstream.close();
            return sendok;
        }