Send Mail

weixin_34321977發表於2019-01-13

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;
   }
  }

 }
}

相關文章