zoukankan      html  css  js  c++  java
  • Send Mail

    using System;
    using System.Web.Mail;
    using System.Configuration;
    using System.Web ;
    using System.IO;
    using DAL;

    namespace BLL
    {
     /// <summary>
     /// Summary description for SMTP.
     /// </summary>
     ///
     public class SMTP
     {
      protected static string SMTP_MAILSERVER = ConfigurationSettings.AppSettings["SMTPServer"];
      protected static string MAILACCOUNT = ConfigurationSettings.AppSettings["MailFrom"];
      protected static string MAILTOCC = ConfigurationSettings.AppSettings["MailCC"];
      protected static string MAILBCC = ConfigurationSettings.AppSettings["BCC"];

      public static void SendTruckerMail(string strTo, string strMailType, string[] BodyArray, string strAtta,
       string strAttachmentDirectory)
      { 
       string strBody = string.Empty ;
       string strSubject = string.Empty ;
       string maillist = string.Empty;
       string cclist = string.Empty ;

       string strAppointID = Convert.ToString(BodyArray[4]).Trim();
       GenericSqlDAL sqlbaseInfo = new GenericSqlDAL(); 
       string TruckerName = sqlbaseInfo.GetTruckerNameByAppointID(strAppointID);

       //For FCL shipment, it is allow to attach the S/O into trucking appointment system now, please email this attachment to vendor and trucker accompany with appointment form too.
       string strSOAttachment = sqlbaseInfo.GetAttacmentRecord(strAppointID);

       switch (strMailType)
       {
        case "Delete":
         maillist = strTo;   //Trucker email address
         cclist = Convert.ToString(BodyArray[5]); //Vendor email address
         if(maillist == "")
         {
          maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
          cclist = "";
         }
         
         strSubject = "Trucking Appointment(" + Convert.ToString(BodyArray[3]).Trim() + ")--(Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") was cancelled.";
         strBody = "Hi " + TruckerName + ",<br><br>";
         strBody = strBody + "There is an appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") from " + Convert.ToString(BodyArray[1]).Trim() + ", and it has been deleted by staff (" + Convert.ToString(BodyArray[0]).Trim() + ").<br><br>";
         strBody = strBody + " Please refer to the attachment and check the appointment.<br><br><br>";
         strBody = strBody + "Best Regards <br>";
         strBody = strBody + "Trucking Appointmnet System <br>";
         break;
        case "Pass":
         maillist = strTo;
         cclist = Convert.ToString(BodyArray[5]);
         if(maillist == "")
         {
          maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
          cclist = "";
         }
         strSubject = "Trucking Appointment("  +  Convert.ToString(BodyArray[3]).Trim()  +  ")--(Appointment No: "  +  Convert.ToString(BodyArray[4]).Trim()  +  ")";
                   
         strBody = "Hi " + TruckerName + ",<br><br>";
         strBody = strBody + "There is an appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") from " +  Convert.ToString(BodyArray[1]).Trim() + ", and it has been confirmed by staff (" + Convert.ToString(BodyArray[0]).Trim() + ").<br><br>";
         strBody = strBody + " Please refer to the attachment and check the appointment.<br><br><br>";

         strBody = strBody + "Best Regards <br>";
         strBody = strBody + "Trucking Appointmnet System <br>";
         break;

        case "TruckerPass":
         maillist = strTo;
         cclist = Convert.ToString(BodyArray[5]);
         if(maillist == "")
         {
          maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
          cclist = "";
         }
         strSubject = "Trucking Appointment("  +  Convert.ToString(BodyArray[3]).Trim()  +  ")--(Appointment No: "  +  Convert.ToString(BodyArray[4]).Trim()  +  ") -- Trucker Confirm";
                   
         strBody = "Hi " + Convert.ToString(BodyArray[2]).Trim() + ",<br><br>";
         strBody = strBody + "There is an appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") from " +  Convert.ToString(BodyArray[1]).Trim() + ", and it has been confirmed by Trucker staff (" + Convert.ToString(BodyArray[0]).Trim() + ").<br><br>";
         strBody = strBody + " Please refer to the attachment and check the appointment.<br><br><br>";

         strBody = strBody + "Best Regards <br>";
         strBody = strBody + "Trucking Appointmnet System <br>";
         break;

        case "TruckerReject":
         maillist = strTo;
         cclist = Convert.ToString(BodyArray[5]);
         if(maillist == "")
         {
          maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
          cclist = "";
         }
         strSubject = "Trucking Appointment("  +  Convert.ToString(BodyArray[3]).Trim()  +  ")--(Appointment No: "  +  Convert.ToString(BodyArray[4]).Trim()  +  ") -- Trucker Reject";
                   
         strBody = "Hi " + Convert.ToString(BodyArray[2]).Trim() + ",<br><br>";
         strBody = strBody + "There is an appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") from " +  Convert.ToString(BodyArray[1]).Trim() + ", and it has been rejected by Trucker staff (" + Convert.ToString(BodyArray[0]).Trim() + ").<br><br>";
         strBody = strBody + " Please refer to the attachment and check the appointment.<br><br><br>";

         strBody = strBody + "Best Regards <br>";
         strBody = strBody + "Trucking Appointmnet System <br>";
         break;

        case "Reject":
         maillist = strTo;
         cclist = Convert.ToString(BodyArray[5]);
         if(maillist == "")
         {
          maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
          cclist = "";
         }
         strSubject = "[Reject]Trucking Appointment(" + Convert.ToString(BodyArray[3]).Trim() + ")--(Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ")";
         strBody = "Hi " + Convert.ToString(BodyArray[1]).Trim() + ",<br><br>";
         strBody = strBody + "Your trucking appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") to <b>" + Convert.ToString(BodyArray[2]).Trim() + "</b> has been rejected by MAPS staff ( " + Convert.ToString(BodyArray[0]).Trim() + ") <br><br>";
         strBody = strBody + "Please visit MAPS Trucking Appointmnet system to check or revise the apppoitment: <br><br><br>";
         strBody = strBody + "Best Regards <br>";
         strBody = strBody + "Trucking Appointmnet System <br>";
         break;
    //    case "BatchPass":
    //     maillist = strTo;
    //     cclist = Convert.ToString(BodyArray[5]);
    //     if(maillist == "")
    //     {
    //      maillist =  ConfigurationSettings.AppSettings["AdminAccount"];
    //      cclist = "";
    //     }
    //     strSubject = "Trucking Appointment(" + Convert.ToString(BodyArray[3]).Trim() + ")--(Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ")";
    //     strBody = "Hi " + Convert.ToString(BodyArray[2]).Trim() + ",<br><br>";
    //     strBody = strBody + "There is an appointment (Appointment No: " + Convert.ToString(BodyArray[4]).Trim() + ") from " + Convert.ToString(BodyArray[1]).Trim() + ", and it has been confirmed by staff (" + Convert.ToString(BodyArray[0]).Trim() + ").<br><br>";
    //     strBody = strBody + " Please refer to the attachment and check the appointment.<br><br><br>";
    //     strBody = strBody + "Best Regards <br>";
    //     strBody = strBody + "Trucking Appointmnet System <br>";
    //     break;
       }
       SendEmail(maillist, cclist,MAILBCC, strSubject, strBody, strAtta,strAttachmentDirectory+strSOAttachment, true);
       // SendEmail(maillist, cclist, MAILBCC,strSubject, strBody, strAtta);
      }

      
      public static void SendEmail( string strTo, string strCC, string strBCC, string strSubject,
       string strBody, string strAttachment, bool useTest)
      {
       SendEmail(strTo,strCC, strBCC, strSubject,strBody,strAttachment,"", useTest);
      }

      public static void SendEmail( string strTo, string strCC, string strBCC, string strSubject,
       string strBody, string strAttachment)
      {
       SendEmail(strTo,strCC, strBCC, strSubject,strBody,strAttachment,"", true);
      }

      private static void SendEmail( string strTo, string strCC, string strBCC,
       string strSubject, string strBody, string strAttachment,string strSOAttachment, bool useTest)
      {
       if (strTo.Length == 0)
        //return false;
        return;
       System.Web.Mail.MailMessage myMail;
       try
       {
        myMail = new MailMessage();
        if (IsTesting(useTest))
        {
         myMail.From = burning2007@163.com;
         myMail.To = "burning2007@163.com";
         myMail.Cc = "burning2007@163.com;";
         if( strBCC != null && strBCC != string.Empty )
          myMail.Bcc = strBCC;
         myMail.Subject = "ConnieTesting: "+strSubject;
        }
        else
        {
         myMail.From = MAILACCOUNT;
         myMail.To = strTo;
         myMail.Cc = strCC;
         if( strBCC != null && strBCC != string.Empty )
          myMail.Bcc = strBCC;
         myMail.Subject = strSubject;
        }
        myMail.BodyFormat = System.Web.Mail.MailFormat.Html;
        myMail.BodyEncoding = System.Text.UTF8Encoding.UTF8;
        myMail.Body = strBody;

        if(System.IO.File.Exists(strAttachment))
         myMail.Attachments.Add(new System.Web.Mail.MailAttachment(strAttachment));
        if(File.Exists(strSOAttachment))
         myMail.Attachments.Add(new MailAttachment(strSOAttachment));
        System.Web.Mail.SmtpMail.SmtpServer = SMTP_MAILSERVER;
    //    System.Web.Mail.SmtpMail.SmtpServer = System.Configuration.ConfigurationSettings.AppSettings["SMTPServer"];
        System.Web.Mail.SmtpMail.Send(myMail);
        myMail = null;
        
       }
       catch(System.Runtime.InteropServices.COMException ex)
       {
        
        throw ex;
        
       }
       catch(System.Exception ex)
       {
        
        throw ex;
        
       }
       catch
       {
        
        throw ;
        
       }
      }

      private static bool IsTesting(bool useTest)
      {
       if( !useTest )
        return false;

       string strTesting=ConfigurationSettings.AppSettings["Testing"];
       try
       {
        return Convert.ToBoolean(strTesting);
       }
       catch
       {
        return false;
       }
      }

     }
    }

  • 相关阅读:
    C#实现按键精灵的'找图' '找色' '找字'的功能
    Amazon SES SPF和DKIM设置教程
    你应该知道的最好Webmail邮件客户端,
    8款世界级Webmail工具推荐
    AWS邮件通知服务:实时监控邮件状态
    XenServer:使用XenCenter开设VPS(多图完整版)
    Thinkpad机器BIOS下清除安全芯片和指纹数据的方法
    在ASP.NET Web Application中通过SOAP协议调用Bing搜索服务
    怎么申请 bing api key
    linux挂载硬盘失败,报错!
  • 原文地址:https://www.cnblogs.com/zhangchenliang/p/1793382.html
Copyright © 2011-2022 走看看