用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - c#代码库

发邮件 发送邮件

2015-11-12 作者: ddlucky举报

[c#]代码库

        public static void SendMailLocalhost(string MailType,XmlOpseae Opseae)
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //首先获取发件人 发件公司 发件邮箱(委托方) // 收件人 收件公司 收件邮箱(承运方)
            var strSql = new StringBuilder();
            strSql.Append(" select case AUDITOR WHEN '' then ");
            strSql.Append(" (select email from userinfo where userid in (select userid from login where companyID=FORWARDER) and departmentID='CompanyMaster') ");
            strSql.Append(" else (select email from userinfo where userid=AUDITOR) end ReceiverEmail ");
            strSql.Append(" ,case AUDITOR WHEN '' then  ");
            strSql.Append(" (select username from userinfo where userid in (select userid from login where companyID=FORWARDER) and departmentID='CompanyMaster') ");
            strSql.Append(" else (select username from userinfo where userid=AUDITOR) end Receivername ");
            strSql.Append(" ,u.username sendername,u.email senderEmail ");
            strSql.Append(" ,(select fullname from company where companyid=bcs.forwarder) receiverCompany ");
            strSql.Append(" ,(select fullname from company where companyid=bcs.companyid) sendercompany ");
            strSql.Append(" ,(select Mobile from userinfo where userid=bcs.inputby) senderMobile ");
            strSql.Append(" ,(select Mobile from userinfo where userid=bcs.Auditor) receiverMobile ");
            strSql.Append(" ,bcs.* ");
            strSql.Append(" from BookingCargoSpace bcs ");
            strSql.Append(" left join userinfo U on u.userid = bcs.inputby ");
            strSql.Append(" where bsno='" + Opseae.BSNO + "' ");

            DataSet ds = new DataSet();
            Database db = DatabaseFactory.CreateDatabase();

            var SenderName = "";
            var SenderEmail = "";
            var SenderCompany = "";
            var SenderMobile = "";
            var ReceiverName = "";
            var ReceiverEmail = "";
            var ReceiverCompany = "";
            var ReceiverMobile = "";
            var EmailText = new StringBuilder();
            var EmailTitle = new StringBuilder();
            try
            {
                using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()) )
                {                
                    while (reader.Read())
                    {

                            SenderName = Convert.ToString(reader["SenderName"]);
                            SenderEmail = Convert.ToString(reader["SenderEmail"]);
                            SenderCompany = Convert.ToString(reader["SenderCompany"]);
                            SenderMobile = Convert.ToString(reader["SenderMobile"]);
                            ReceiverName = Convert.ToString(reader["ReceiverName"]);
                            ReceiverEmail = Convert.ToString(reader["ReceiverEmail"]);
                            ReceiverCompany = Convert.ToString(reader["ReceiverCompany"]);
                            ReceiverMobile = Convert.ToString(reader["ReceiverMobile"]);
                            EmailTitle.Clear();
                            EmailTitle.Append("");

                    }
                    reader.Close();
                }
            }
            catch (Exception)
            {

            }
            finally
            {

            }
            if (MailType == "拒绝订舱" || MailType == "待定" || MailType == "拒绝提单确认")
            {
                msg.To.Add( SenderEmail);
                msg.From = new MailAddress(ReceiverEmail, ReceiverName, System.Text.Encoding.UTF8);
            }
            else {
                msg.To.Add(ReceiverEmail);
                msg.From = new MailAddress(SenderEmail, SenderName, System.Text.Encoding.UTF8);            
            }
            /* 上面3个参数分别是发件人地址(可以随便写),发件人姓名,编码*/
            var Subject = "";
            var Body = new StringBuilder();

            if (MailType == "订舱") {
                Subject = " 订舱:From "+SenderCompany;
                if (Opseae.ETD!="")
                    Subject = Subject+" 船期"+Opseae.ETD;
                if (Opseae.CNTR != "")
                Subject = Subject + " 箱型箱量" + Opseae.CNTR;
                if (Opseae.DESTINATION != "")
                Subject = Subject + " 目的地" + Opseae.DESTINATION;

                //Body.Append(Subject);
                Body.Append("<html> <Body>订舱公司:" + SenderCompany);

                Body.Append("<br />订舱人:" + SenderName + "&nbsp;&nbsp;&nbsp;电话:" + SenderMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + SenderEmail);
                if (Opseae.CNTR != "")
                    Body.Append("<br />箱型箱量:" + Opseae.CNTR);
                if (Opseae.ETD != "")
                    Body.Append("<br />船期:" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append("<br />目的地:" + Opseae.DESTINATION);
                if (Opseae.BOOKINGDESCRIPTION != "")
                    Body.Append("<br />订舱说明:" + Opseae.BOOKINGDESCRIPTION);
            }
            if (MailType == "退舱")
            {
                Subject = " 退舱申请:From " + SenderCompany;
                if (Opseae.ETD != "")
                    Subject = Subject + " 船期" + Opseae.ETD;
                if (Opseae.CNTR != "")
                    Subject = Subject + " 箱型箱量" + Opseae.CNTR;
                if (Opseae.DESTINATION != "")
                    Subject = Subject + " 目的地" + Opseae.DESTINATION;

                
                Body.Append("<html> <Body>");
                Body.Append("<br />您好,我公司希望撤回通过ShippingBox所订的此条业务,原因如下:<br />" + REHH(Opseae.PendingReason));
                Body.Append("<br />订舱公司:" + SenderCompany);
                Body.Append("<br />订舱人:" + SenderName + "&nbsp;&nbsp;&nbsp;电话:" + SenderMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + SenderEmail);
                if (Opseae.CNTR != "")
                    Body.Append("<br />箱型箱量:" + Opseae.CNTR);
                if (Opseae.ETD != "")
                    Body.Append("<br />船期:" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append("<br />目的地:" + Opseae.DESTINATION);
                if (Opseae.BOOKINGDESCRIPTION != "")
                    Body.Append("<br />订舱说明:" + Opseae.BOOKINGDESCRIPTION);
            }
            if (MailType == "拒绝订舱")
            {
                Subject = " 拒绝订舱提醒:From " + ReceiverCompany;
                if (Opseae.ETD != "")
                    Subject = Subject + " 船期" + Opseae.ETD;
                if (Opseae.CNTR != "")
                    Subject = Subject + " 箱型箱量" + Opseae.CNTR;
                if (Opseae.DESTINATION != "")
                    Subject = Subject + " 目的地" + Opseae.DESTINATION;

                Body.Append("<html> <Body>");
                Body.Append("<br />您好,您通过ShippingBox所订业务被拒绝,理由如下:<br />" + REHH(Opseae.PendingReason));
                Body.Append("<br />订舱代理公司:" + ReceiverCompany);
                Body.Append("<br />操作人:" + ReceiverName + "&nbsp;&nbsp;&nbsp;电话:" + ReceiverMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + ReceiverEmail);
                
                if (Opseae.CNTR != "")
                    Body.Append("<br />箱型箱量:" + Opseae.CNTR);
                if (Opseae.ETD != "")
                    Body.Append("<br />船期:" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append("<br />目的地:" + Opseae.DESTINATION);
                if (Opseae.BOOKINGDESCRIPTION != "")
                    Body.Append("<br />订舱说明:" + Opseae.BOOKINGDESCRIPTION);
            }
            if (MailType == "待定")
            {
                Subject = " 订舱被待定:From " + SenderCompany;
                if (Opseae.ETD != "")
                    Subject = Subject + " 船期" + Opseae.ETD;
                if (Opseae.CNTR != "")
                    Subject = Subject + " 箱型箱量" + Opseae.CNTR;
                if (Opseae.DESTINATION != "")
                    Subject = Subject + " 目的地" + Opseae.DESTINATION;

                //Body.Append(Subject);
                Body.Append("<html> <Body>接收订舱公司:" + SenderCompany);

                Body.Append("<br />操作人:" + SenderName + "&nbsp;&nbsp;&nbsp;电话:" + SenderMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + SenderEmail);
                if (Opseae.CNTR != "")
                    Body.Append("<br />箱型箱量:" + Opseae.CNTR);
                if (Opseae.ETD != "")
                    Body.Append("<br />船期:" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append("<br />目的地:" + Opseae.DESTINATION);
                if (Opseae.BOOKINGDESCRIPTION != "")
                    Body.Append("<br />订舱说明:" + Opseae.BOOKINGDESCRIPTION);
                if (Opseae.PendingReason != "")
                    Body.Append("<br />待定理由:" + REHH(Opseae.PendingReason));
            }
            if (MailType == "拒绝提单确认")
            {
                Subject = " 拒绝提单确认:From " + ReceiverCompany;
                if (Opseae.ETD != "")
                    Subject = Subject + " 船期" + Opseae.ETD;
                if (Opseae.CNTR != "")
                    Subject = Subject + " 箱型箱量" + Opseae.CNTR;
                if (Opseae.DESTINATION != "")
                    Subject = Subject + " 目的地" + Opseae.DESTINATION;

                Body.Append("<html> <Body>");
                Body.Append("<br />您好,您通过ShippingBox所提交提单确认被拒绝,理由如下:<br />" + REHH(Opseae.PendingReason));
                Body.Append("<br />订舱代理公司:" + ReceiverCompany);
                Body.Append("<br />操作人:" + ReceiverName + "&nbsp;&nbsp;&nbsp;电话:" + ReceiverMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + ReceiverEmail);

                if (Opseae.CNTR != "")
                    Body.Append("<br />箱型箱量:" + Opseae.CNTR);
                if (Opseae.ETD != "")
                    Body.Append("<br />船期:" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append("<br />目的地:" + Opseae.DESTINATION);
                if (Opseae.BOOKINGDESCRIPTION != "")
                    Body.Append("<br />订舱说明:" + Opseae.BOOKINGDESCRIPTION);
            }
            Body = SetBottom(Body, Opseae.BSNO);

            msg.Subject = Subject;//邮件标题  
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码  
            msg.Body = Body.ToString();//邮件内容  
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码  
            msg.IsBodyHtml = true;//是否是HTML邮件  
            msg.Priority = MailPriority.High;//邮件优先级 
            var content = "";
            AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(content, null, "text/html");
            /*
            LinkedResource lrImage = new LinkedResource(@"D:\DSWebservice\a.jpg", "image/gif");
            lrImage.ContentId = "Email001";
            htmlBody.LinkedResources.Add(lrImage);
            msg.AlternateViews.Add(htmlBody);*/
            
            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("admin@dongshengsoft.com", "ds!@#)(*");
            //在71info.com注册的邮箱和密码    
            client.Host = "smtpcom.263xmail.com";
            object userState = msg;
            var Result = "";
            try
            {
                //client.SendAsync(msg, userState);  
                //简单一点儿可以
                client.Send(msg);
                //MessageBox.Show("发送成功");  
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Result = ex.Message;
            }
        }
        
        public static void SendMSGMail ( XmlMSGInfo Mail )
        {
            System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
            //首先获取发件人 发件公司 发件邮箱(委托方) // 收件人 收件公司 收件邮箱(承运方)
            var strSql = new StringBuilder();
            strSql.Append(" select M.*,S.userName S_userName,S.mobile S_mobile,S.email S_email,S.fullName S_Company ");
            strSql.Append(" ,r.userName R_userName,r.mobile R_mobile,r.email  R_email,R.fullName R_Company from BookingMSG m ");
            strSql.Append(" left join vw_user S on s.userID=m.SENDERID ");
            strSql.Append(" left join vw_user R on R.userID=m.RECEIVERID ");
            strSql.Append(" where BSNO='" + Mail.BSNO + "' and MSGDate='" + Mail.MSGDate  + "' ");

            DataSet ds = new DataSet();
            Database db = DatabaseFactory.CreateDatabase();

            var SenderName = "";
            var SenderEmail = "";
            var SenderCompany = "";
            var SenderMobile = "";
            var ReceiverName = "";
            var ReceiverEmail = "";
            var ReceiverCompany = "";
            var ReceiverMobile = "";
            var BSNO = "";
            var EmailText = new StringBuilder();
            var EmailTitle = new StringBuilder();
            try
            {
                using (IDataReader reader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
                {
                    while (reader.Read())
                    {
                        SenderName = Convert.ToString(reader["S_userName"]);
                        SenderEmail = Convert.ToString(reader["S_email"]);
                        SenderCompany = Convert.ToString(reader["S_Company"]);
                        SenderMobile = Convert.ToString(reader["S_mobile"]);
                        ReceiverName = Convert.ToString(reader["R_userName"]);
                        ReceiverEmail = Convert.ToString(reader["R_email"]);
                        ReceiverCompany = Convert.ToString(reader["R_Company"]);
                        ReceiverMobile = Convert.ToString(reader["R_mobile"]);
                        BSNO = Convert.ToString(reader["BSNO"]);
                        EmailTitle.Clear();
                        EmailTitle.Append("");
                    }
                    reader.Close();
                }


            }
            catch (Exception)
            {

            }
            finally
            {

            }

            msg.To.Add(ReceiverEmail);

            msg.From = new MailAddress(SenderEmail, SenderName, System.Text.Encoding.UTF8);

            var Subject = "";
            var Body = new StringBuilder();
            var Opseae = GetBookingCargoSpace(BSNO);

            Subject = " 沟通消息: " + SenderCompany;
            if (Opseae.VESSEL != "")
                Subject = Subject + "--" + Opseae.VESSEL;
            if (Opseae.VOYNO != "")
                Subject = Subject + "--" + Opseae.VOYNO;
            if (Opseae.MBLNO != "")
                Subject = Subject + "--" + Opseae.MBLNO;               
            if (Opseae.ETD != "")                   
                Subject = Subject + "--" + Opseae.ETD;                
            if (Opseae.CNTR != "")                    
                Subject = Subject + "--" + Opseae.CNTR;                
            if (Opseae.DESTINATION != "")                    
                Subject = Subject + "--" + Opseae.DESTINATION;

                //Body.Append(Subject);
            Body.Append("<html> <Body>关于  ");
                if (Opseae.VESSEL != "")
                    Body.Append("--" + Opseae.VESSEL);
                if (Opseae.VOYNO != "")
                    Body.Append( "--" + Opseae.VOYNO);
                if (Opseae.MBLNO != "")
                    Body.Append( "--" + Opseae.MBLNO);
                if (Opseae.ETD != "")
                    Body.Append( "--" + Opseae.ETD);
                if (Opseae.CNTR != "")
                    Body.Append( "--" + Opseae.CNTR);
                if (Opseae.DESTINATION != "")
                    Body.Append( "--" + Opseae.DESTINATION);

                Body.Append(":<br />" + REHH( Mail.FieldValue));

                Body.Append("<br /><br />联系人:" + SenderName + "&nbsp;&nbsp;&nbsp;电话:" + SenderMobile + "&nbsp;&nbsp;&nbsp;邮箱:" + SenderEmail);


                Body = SetBottom(Body, Opseae.BSNO);

            msg.Subject = Subject;//邮件标题  
            msg.SubjectEncoding = System.Text.Encoding.UTF8;//邮件标题编码  
            msg.Body = Body.ToString();//邮件内容  
            msg.BodyEncoding = System.Text.Encoding.UTF8;//邮件内容编码  
            msg.IsBodyHtml = true;//是否是HTML邮件  
            msg.Priority = MailPriority.High;//邮件优先级 
            var content = "";
            AlternateView htmlBody = AlternateView.CreateAlternateViewFromString(content, null, "text/html");

            SmtpClient client = new SmtpClient();
            client.Credentials = new System.Net.NetworkCredential("admin@dongshengsoft.com", "ds!@#)(*");
            //在71info.com注册的邮箱和密码    
            client.Host = "smtpcom.263xmail.com";
            object userState = msg;
            var Result = "";
            try
            {
                //client.SendAsync(msg, userState);  
                //简单一点儿可以
                client.Send(msg);
                //MessageBox.Show("发送成功");  
            }
            catch (System.Net.Mail.SmtpException ex)
            {
                Result = ex.Message;
            }
        }


网友评论    (发表评论)

共1 条评论 1/1页

发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...