Friday 7 October 2016

how to send mail in MVC

first create HTML file
Equipment Installation Failure
Hello <<ServiceManagername>>,<br><br>
<p>This is to notify you that following piece of equipment has failed the installation test performed at <<wlocation>>. </p>
<p>Please find the equipment details below:</p>
<table width="100%" border="1" cellspacing="0" cellpadding="10" style="border:1px solid #555; font-family:Arial, sans-serif; font-size:14px">
<thead>
                        <tr style='background-color:silver'>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px;">Asset No</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Model Name</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Serial Number</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Performed By</th>
                            <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Performed On</th>
                          <th style="color:#1f497f; padding:5px 0; font-family:Arial, sans-serif; font-size:14px">Status</th>
                        </tr>
</thead>

<tr style="text-align:center">
<td><<AssetNo>></td>
<td><<ModelName>></td>
<td><<SerialNumber>></td>
<td><<PerformedBy>></td>
<td><<Performedon>></td>
        <td><<Status>></td>
</tr>
</table>
<br>
<p>To know the equipment details and its location history, please <a href='<<path>>'>Login</a></p>  
<br />
Thanks,<br />
Corporate Essentials


C # CODE :

 if (sid == 19)
                {
                    string EmailSystemName = WebConfigurationManager.AppSettings["DefaultEmailSystemName"].ToString();
                    EmailTemplate emailTemplate = new EmailTemplate();
                    emailTemplate.emailAccount = _EmailAccountService.GetEmailAccountBySystemName(EmailSystemName);
                    string txtFileName = Server.MapPath("~/Administration/Content/HTMLTemplates/barcodeInstallation.txt");
                    string[] lines;
                    string mailSubject = "";
                    string MailBody = "";
                    if (System.IO.File.Exists(txtFileName))
                    {
                        lines = System.IO.File.ReadAllLines(txtFileName);
                        mailSubject = lines[0];
                        var otherLines = new ArraySegment<String>(lines, 1, lines.Length - 1);
                        MailBody = String.Join("", otherLines);
                    }
                    var customer = EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer;
                    List<Customer> customerList = _customerService.GetAllCustomers(0, int.MaxValue, string.Empty).Where(c => c.CustomerRoles.Select(cr => cr.SystemName).Contains("CustomerServiceManager")).ToList();
                    foreach (Customer singleCustomer in customerList)
                    {
                        emailTemplate.fromAddress = customer.Email;
                        emailTemplate.fromName = "Corporate Essentials";
                        emailTemplate.toAddress = singleCustomer.Email;
                        string mailBody = MailBody;
                        mailBody = mailBody.Replace("<<ServiceManagername>>", singleCustomer.GetFullName() ?? string.Empty);
                        mailBody = mailBody.Replace("<<wlocation>>", mailLocation ?? string.Empty);
                        mailBody = mailBody.Replace("<<AssetNo>>", equipment.AssetID ?? string.Empty);
                        mailBody = mailBody.Replace("<<ModelName>>", equipment.Model.Name ?? string.Empty);
                        mailBody = mailBody.Replace("<<SerialNumber>>", equipment.SerialNo ?? string.Empty);
                        mailBody = mailBody.Replace("<<PerformedBy>>", EngineContext.Current.Resolve<IWorkContext>().CurrentCustomer.GetFullName() ?? string.Empty);
                        mailBody = mailBody.Replace("<<Performedon>>", DateTime.UtcNow.ToShortDateString() ?? string.Empty);
                        mailBody = mailBody.Replace("<<Status>>", status ?? string.Empty);
                        mailBody = mailBody.Replace("<<path>>", Request.Url.AbsoluteUri ?? string.Empty);
                        emailTemplate.replyToName = "Corporate Essentials";
                        emailTemplate.subject = mailSubject;
                        emailTemplate.body = mailBody;
                        _EmailSender.SendEmail(emailTemplate);
                    }
                }
                

No comments:

Post a Comment