Esempio n. 1
0
        public void SendMailMessage(string Subject, string MessageBody, int IsMailRequired, int IsAttachFilesRequired)
        {
            string serverDt;
            string logPath;

            serverDt = DateTime.Now.ToString("MM/dd/yyyy");
            serverDt = serverDt.Replace('/', '-');
            serverDt = serverDt.Replace(':', ' ');

            logPath = System.AppDomain.CurrentDomain.BaseDirectory + "\\Log";

            try
            {
                if (IsMailRequired == 1)
                {
                    string        EndIndexMessageBody = "";
                    StringBuilder Sup = GetIndexNotificationContent(Subject, MessageBody);
                    EndIndexMessageBody = Sup.ToString();
                    EndIndexMessageBody = EndIndexMessageBody.Replace("#####", MessageBody);

                    string   AdminReceiver              = ConfigurationManager.AppSettings["AdminReceiver"].ToString();
                    string   AdminReceiverName          = ConfigurationManager.AppSettings["ReceiverName"].ToString();
                    string   AuthenticationUserName     = ConfigurationManager.AppSettings["AuthenticationUserName"].ToString();
                    string   AuthenticationUserPassword = ConfigurationManager.AppSettings["AuthenticationUserPassword"].ToString();
                    string   SMTPHost          = ConfigurationManager.AppSettings["SMTPHost"].ToString();
                    string   SMTPPort          = ConfigurationManager.AppSettings["SMTPPort"].ToString();
                    bool     IsBodyHtml        = Convert.ToBoolean(ConfigurationManager.AppSettings["IsBodyHtml"].ToString());
                    string   SenderMailID      = ConfigurationManager.AppSettings["SenderMailID"].ToString();
                    string   SenderDisplayName = ConfigurationManager.AppSettings["SenderDisplayName"].ToString();
                    string   ReceiverCC        = ConfigurationManager.AppSettings["ReceiverCC"].ToString();
                    string   ReceiverNameCC    = ConfigurationManager.AppSettings["ReceiverNameCC"].ToString();
                    string[] MailCC            = new string[100];
                    string[] NameCC            = new string[100];

                    string[] ReceiverMail = AdminReceiver.Split(',');
                    string[] ReceiverName = AdminReceiverName.Split(',');
                    if (ReceiverCC != string.Empty && ReceiverNameCC != string.Empty)
                    {
                        MailCC = ReceiverCC.Split(',');
                        NameCC = ReceiverNameCC.Split(',');
                    }

                    Dictionary <string, string> Receiver       = new Dictionary <string, string>();
                    Dictionary <string, string> ReceiverCCMail = new Dictionary <string, string>();

                    for (int i = 0; i < ReceiverMail.Count(); i++)
                    {
                        string mail = ReceiverMail[i];
                        string name = ReceiverName[i];
                        Receiver.Add(mail, name);
                    }
                    for (int i = 0; i < MailCC.Count(); i++)
                    {
                        string ccmail = MailCC[i];
                        string CCName = NameCC[i];
                        ReceiverCCMail.Add(ccmail, CCName);
                    }
                    MailMessage objMailMessage = new MailMessage();
                    objMailMessage.From = new MailAddress(SenderMailID, SenderDisplayName);

                    foreach (KeyValuePair <string, string> ReceiverInfo in Receiver)
                    {
                        objMailMessage.To.Add(new MailAddress(ReceiverInfo.Key, ReceiverInfo.Value));
                    }

                    if (ReceiverCCMail.Count > 0)
                    {
                        foreach (KeyValuePair <string, string> ReceiverCCInfo in ReceiverCCMail)
                        {
                            objMailMessage.CC.Add(new MailAddress(ReceiverCCInfo.Key, ReceiverCCInfo.Value));
                        }
                    }
                    objMailMessage.Priority   = MailPriority.High;
                    objMailMessage.Body       = EndIndexMessageBody;
                    objMailMessage.IsBodyHtml = IsBodyHtml;
                    objMailMessage.Subject    = Subject;
                    if (IsAttachFilesRequired == 1)
                    {
                        string[] AttachedFiles = Directory.GetFiles(logPath, "PTABDocumentDownloadLog_" + serverDt + ".txt");
                        //Adding attached files into mail
                        if (AttachedFiles != null && AttachedFiles.Length > 0)
                        {
                            foreach (string AttachedFilePath in AttachedFiles)
                            {
                                if (AttachedFilePath + "" != "")
                                {
                                    FileStream   inStream    = File.OpenRead(AttachedFilePath);
                                    MemoryStream storeStream = new MemoryStream();
                                    // copy all data from in to store
                                    storeStream.SetLength(inStream.Length);
                                    inStream.Read(storeStream.GetBuffer(), 0, (int)inStream.Length);
                                    storeStream.Flush();
                                    inStream.Close();
                                    string filename = AttachedFilePath.Substring(AttachedFilePath.LastIndexOf("\\"));

                                    filename = filename.Replace("\\", "");

                                    Attachment myAttachment = new Attachment(storeStream, filename);
                                    objMailMessage.Attachments.Add(myAttachment);
                                }
                            }
                        }
                    }

                    using (SmtpClient objSMTPClient = new SmtpClient(SMTPHost))
                    {
                        objSMTPClient.Port           = Convert.ToInt32(SMTPPort);
                        objSMTPClient.Credentials    = new NetworkCredential(AuthenticationUserName, AuthenticationUserPassword);
                        objSMTPClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                        // objSMTPClient.Send(objMailMessage);
                    }
                }
            }
            catch (Exception ex)
            {
                LogFile.WriteToFile("Mail Send Failed because of " + ex.ToString());
            }
        }
Esempio n. 2
0
        public void 添付ファイルパスを正しく取得()
        {
            var act = new AttachedFilePath("/attached/filePath");

            Assert.Equal("/attached/filePath", act.Value);
        }