zoukankan      html  css  js  c++  java
  • 康尼机电的发送邮件的功能2

    康尼机电的委托协议发送邮件的功能

     

    加载邮件初始信息的代码

    public void LoadMailInfo() {

            try

            {

                XElement xl = new XElement("root", new XElement("Type", "QuoteMailINFO"), new XElement("MainAID", MainAID.Text)  ,new XElement("Creator", getUserName()));             

                DataTable dt = Govaze.SQLServerDAL.Factory.getDataXmlDAL().ProcDataTable(xl.ToString(), "DataXmlForQuote");

                TBX_Tittle.Text = dt.Rows[0]["Tittle"].ToString();

                TBX_SendTo.Text = dt.Rows[0]["SendTo"].ToString();

                StringBuilder sb = new StringBuilder();

                sb.Append(dt.Rows[0]["CuntomerName"].ToString());

                sb.Append("<br/>");sb.Append("<br/>");

                sb.AppendLine(dt.Rows[0]["Messeage"].ToString() + " ( " + dt.Rows[0]["SalsName"].ToString() + ", 手机:" + dt.Rows[0]["SalsPhone"].ToString() + ", 邮箱:" + dt.Rows[0]["SalsEmail"].ToString() + " ) ");

                //sb.AppendLine(dt.Rows[0]["Messeage"].ToString() + " ( " + dt.Rows[0]["SalsName"].ToString() + ", 手机:" + dt.Rows[0]["SalsPhone"].ToString() + ", 邮箱:<a href=" + dt.Rows[0]["SalsEmail"].ToString() + ">"

        + dt.Rows[0]["SalsEmail"].ToString() + "</a> )");

                sb.Append("<br/>");

                sb.Append("<br/>");

                sb.Append("<br/>");

                sb.AppendLine(dt.Rows[0]["BottomText"].ToString());

                HED_MailHtml.Text = Server.HtmlDecode(sb.ToString());

              

          

            }

            catch { }

     

        }

        }

      #endregion

     

    发送邮件的代码

    protected void Send_Click(object sender, EventArgs e) {

            //1 创建文件夹

            string FilePath = MainAID.Text + "_" + System.DateTime.Now.Ticks.ToString();

            System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(Server.MapPath("..\UploadFiles\MailFiles\") + FilePath);

            di.Create();

            //2 保存html 编辑器中的内容为txt到服务器

            string MailHtml = FilePath + ".txt";

            FileStream fs1 = new FileStream(Server.MapPath("..\UploadFiles\MailFiles\") + FilePath+"\" + MailHtml, FileMode.Create, FileAccess.Write);

            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs1);

            sw.Write(HED_MailHtml.Text);

            sw.Flush();

            sw.Close();

            //3 生成文件保存到1创建的文件夹 并将路径添加到附件集

            List<string> AttachFiles = new List<string>();

            //AttachFiles.Add(GetQuoteFile(int.Parse(MainAID.Text), FlowNode.Text, SnID.Text, FilePath));

            string path = Server.MapPath("~\dot\") + "BGMB_JZ.docx";AttachFiles.Add(path);

         

            //4 保存到数据库

            XElement xl = new XElement("root", new XElement("Type", "MailLogAdd"), new XElement("MainAID", MainAID.Text) //委托单AID

                , new XElement("MailType", "委托协议") //日志类型  

                , new XElement("MailTittle", TBX_Tittle.Text) //邮件标题

                ,new XElement("SendTo",TBX_SendTo.Text) //收件人

                ,new XElement("CCTo", TBX_CCTo.Text) //抄送人

                ,new XElement("MailContentPath", MailHtml) //邮件正文

                ,new XElement("AttachFilePath", FilePath) // 附件路径文件夹

                ,new XElement("Creator",getUserName()) //创建人

                ,new XElement("CurrentIP", HttpContext.Current.Request.UserHostAddress) //IP地址

                );

            Govaze.SQLServerDAL.Factory.getDataXmlDAL().ExecProc(xl.ToString(), "DataXmlForQuote");

            

            //5 获取邮件服务器配置信息

            List<aSYSMailServer> m = Govaze.SQLServerDAL.Factory.getSystemConfigDAL().aSYSMailServerDis(getUserName());

     

            //6 发送邮件

            //Mail.SendEmailWithFile(m[0], TBX_SendTo.Text, TBX_Tittle.Text, HED_MailHtml.Text, getUserName(),AttachFiles);

            if (Mail.SendEmailWithFile(m[0], TBX_SendTo.Text, TBX_Tittle.Text, HED_MailHtml.Text, getUserName(), AttachFiles))

            {

                Alert.Show("发送成功!");

            }

            else { Alert.Show("发送失败!"); }        

        }

        }

        #endregion

    调用的Mail.cs中的代码

     

    public static bool SendEmailWithFile(aSYSMailServer item, string mailTo, string mailSubject, string mailContent, string UserName, List<string> fileArray)

            {

     

                try

                {

                    if (mailTo == "")

                    {

                        return false;

                    }

     

                    string smtpServer = item.SMTPServer; //SMTP服务器

                    string mailFrom = item.Mail; //登陆用户名

                    //加密乱码了,暂时不用

                    //byte[] byteRC4Array = System.Text.Encoding.Default.GetBytes(item.PassWord);

                    //byte[] byteArray = Govaze.Components.Module.RC4Crypto.RC4.DecryptEx(byteRC4Array, "ly406@hotmail.com");

                    //string Password = System.Text.Encoding.Default.GetString(byteArray);

                    string userPassword = item.PassWord;//登陆密码

     

                    // 邮件服务设置

                    SmtpClient smtpClient = new SmtpClient();

                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式

                    smtpClient.Host = smtpServer; //指定SMTP服务器

                    smtpClient.EnableSsl = item.enableSsl.Trim().ToLower() == "true" ? true : false;//cfg.Network.enableSsl;

                    smtpClient.Port = item.Port;//cfg.Network.Port;

                    smtpClient.UseDefaultCredentials = true;

                    smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码,此密码是客户端授权码

                    //smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, "XBKRLMWBMLRWYKKL");//用户名和密码

     

                    // 发送邮件设置       

                    string[] mailTo1 = mailTo.Split(';');

                    try

                    {

                        MailMessage mailMessage = new MailMessage(mailFrom, mailTo1[0]);

                        mailMessage.From = new MailAddress(mailFrom);

     

                        for (int i = 1; i < mailTo1.Length; i++)

                        {

                            if (mailTo1[i] != "")

                            {

                                mailMessage.To.Add(mailTo1[i]);

                            }

                        }

                        if (fileArray != null)

                        {

                            for (int i = 0; i < fileArray.Count; i++)

                            {

                                //为邮件创建文件附件

                                Attachment attr = new Attachment(fileArray[i], MediaTypeNames.Application.Octet);

                                //添加邮件时间戳信息

                                ContentDisposition conDispositon = attr.ContentDisposition;

                                conDispositon.CreationDate = System.IO.File.GetCreationTime(fileArray[i]);//文件的创建日期

                                conDispositon.ModificationDate = System.IO.File.GetLastWriteTime(fileArray[i]);//文件的修改日期

                                conDispositon.ReadDate = System.IO.File.GetLastAccessTime(fileArray[i]);//文件的读取日期

                                //给邮件添加附件

                                mailMessage.Attachments.Add(attr);

                            }

                        }

     

                        mailMessage.Subject = mailSubject;//主题

                        mailMessage.Body = mailContent;//内容

                        mailMessage.BodyEncoding = Encoding.UTF8;//正文编码

                        mailMessage.IsBodyHtml = true;//设置为HTML格式

                        mailMessage.Priority = MailPriority.Normal;//优先级

     

                        smtpClient.Send(mailMessage); // 发送邮件

                    }

                    catch (SmtpException ex)

                    {

                        return false;

                    }

                    return true;

                }

                catch (SmtpException ex)

                {

                    return false;

                }

            }

    在点击发送按钮的时候遇到的第一个错误,正文报错,

    解决方法对正文中的代码进行html解码

    HED_MailHtml.Text = Server.HtmlDecode(sb.ToString());

    第二个错误

    {System.Net.Mail.SmtpException:不允许使用邮箱名称。 服务器响应为:authentication is required,163 smtp10,DsCowAC310MnYjRg06uNmA--.49299S2 1614045736
    解决方法:

     

     

    选择发件人邮箱的邮箱服务器,开启POP3/SMTP/IMAP服务,

     

    开始之后仍然发送报错{System.Net.Mail.SmtpException:不允许使用邮箱名称。 服务器响应为:authentication is required,163 smtp10,DsCowAC310MnYjRg06uNmA--.49299S2 1614045736

    发送成功之后会出现一个客户端授权码,添加到发送邮件的Mail.cs对应的代码中

    后来想到客户授权码不应该是死的,所以将客户授权码粘贴到系统设置中的配置发送人的页面中

    将授权码粘贴到密码中,点击发送邮件报错,

    原因是因为密码有加密功能,将加密功能给去掉

    最后发送邮件的时候报错

    {System.Net.Mail.SmtpException: 事务失败。 服务器响应为:DT:SPM 163 smtp9,DcCowAD3QWp+bTRg3sGlgQ--.49580S2 16140

    原因是测试用的收件人的账号总是同一个,换一个账号即可。

    发送成功之后,

  • 相关阅读:
    新概念英语(1-121)The man in a hat
    新概念英语(1-119)who call out to the thieves in the dark?
    画像分析(1-1)如何为客户画像?
    大数据分析师
    英语词汇(2)fall down,fall off和fall over
    把梳子卖给和尚的故事
    洛谷P1970 花匠(dp)
    2018.10.24模拟赛2解题报告
    2018.10.24模拟赛1解题报告
    洛谷P1941 飞扬的小鸟(背包 dp)
  • 原文地址:https://www.cnblogs.com/sanshengshitouhua/p/14439711.html
Copyright © 2011-2022 走看看