locked
how to send attachment to multiple email RRS feed

  • Question

  • User-1634604574 posted

    i have this code used to send email i want to send attachment to multiple email how can i do it with this code

    Home controller

            [HttpPost]
            public ActionResult SendEmail(EmployeeModel obj)
            {
    
                try
                {
                    //Configuring webMail class to send emails  
                    //gmail smtp server  
                    WebMail.SmtpServer = "smtp.gmail.com";
                    //gmail port to send emails  
                    WebMail.SmtpPort = 587;
                    WebMail.SmtpUseDefaultCredentials = true;
                    //sending emails with secure protocol  
                    WebMail.EnableSsl = true;
                    //EmailId used to send emails from application  
                    WebMail.UserName = "zhyanadil.it@gmail.com";
                    WebMail.Password = "***";
    
                    //Sender email address.  
                    WebMail.From = "zhyanadil.it@gmail.com";
    
                    //Send email  
                    WebMail.Send(to: obj.ToEmail, subject: obj.EmailSubject, body: obj.EMailBody, cc: obj.EmailCC, bcc: obj.EmailBCC, isBodyHtml: true);
                    ViewBag.Status = "Email Sent Successfully.";
                }
                catch (Exception)
                {
                    ViewBag.Status = "Problem while sending email, Please check details.";
    
                }
                return View();
            }
    
    
    
            public ActionResult SendEmail()
            {
    
                return View();
            }

    SendEmail view

    @model MVC6.Models.EmployeeModel
    
    @{
        ViewBag.Title = "SendEmail";
    }
    
    <h2>SendEmail</h2>
    

    <table id="tb1">
    <tr><th>email</th> <th>messages</th></tr>
    <tr><td>zhyanadil.it@gmail.com</td><td>hello email 1</td></tr>
    <tr><td>zhyan.ise@outlook.com</td><td>hello email 2</td></tr>
    <tr><td>chnar.ise1992@yahoo.com</td><td>hello email 2</td></tr>
    <tr><td>chnar.ise.1992@gmail.com</td><td>hello email 2</td></tr>

    </table>

    @using (Html.BeginForm())
    {
        @Html.AntiForgeryToken()
    
    
        <div class="form-horizontal">
            <hr />
            @Html.ValidationSummary(true, "", new { @class = "text-danger" })
            <div class="form-group">
                @Html.LabelFor(model => model.ToEmail, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.ToEmail, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.ToEmail, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EMailBody, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EMailBody, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EMailBody, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmailSubject, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmailSubject, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmailSubject, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmailCC, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmailCC, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmailCC, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                @Html.LabelFor(model => model.EmailBCC, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.EditorFor(model => model.EmailBCC, new { htmlAttributes = new { @class = "form-control" } })
                    @Html.ValidationMessageFor(model => model.EmailBCC, "", new { @class = "text-danger" })
                </div>
            </div>
    
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Send" class="btn btn-primary" />
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10 text-success">
                    @ViewBag.Status
                </div>
            </div>
        </div>
    
    } 

    Wednesday, July 29, 2020 8:06 AM

All replies

  • User1686398519 posted

    Hi zhyanadil.it,

    According to your needs, I modified the code you provided and made a complete example, please refer to it.

    1. Two new fields "filesToAttach" and "Files" are added for sending attachments.
    2. You need to add "filesToAttach" in "WebMail.Send()" to send the attachment.
    3. If you use WebMail to send attachments, you can upload the files to the server first, and then send the attachments.
    4. If you want to send multiple recipients, you need to use "," to separate the email addresses.When I tested it, I found that using ";" cannot send to multiple recipients.

    Model

        public class EmployeeModel
        {
            public string ToEmail { get; set; }
            public string EMailBody { get; set; }
            public string EmailSubject { get; set; }
            public string EmailCC { get; set; }
            public string EmailBCC { get; set; }
            public List<string> filesToAttach { get; set; }
            public HttpPostedFileBase[] Files { get; set; }
        }

    Controller

            [HttpPost]
            public ActionResult SendEmail(EmployeeModel obj)
            {
                obj.filesToAttach = new List<string>();
                foreach (HttpPostedFileBase file in obj.Files)
                { 
                    if (file != null)
                    {
                        var uniqueFileName =Guid.NewGuid().ToString();
                        var InputFileName = uniqueFileName + Path.GetFileName(file.FileName);
                        var temporarypath = Server.MapPath("~/TemporaryUploadedFiles/");
                        if (!Directory.Exists(temporarypath))
                        {
                            Directory.CreateDirectory(temporarypath);
                        }
                        var ServerSavePath = Path.Combine(temporarypath,InputFileName);
                        //Save file to server folder  
                        file.SaveAs(ServerSavePath);
                        obj.filesToAttach.Add(ServerSavePath);
                    }
                }
                try
                {
                    WebMail.SmtpServer = "smtp.gmail.com";
                    WebMail.SmtpPort = 587;
                    WebMail.SmtpUseDefaultCredentials = true;
                    WebMail.EnableSsl = true;
                    WebMail.UserName = "xxx@gmail.com";
                    WebMail.Password = "xxx";
                    WebMail.From = "xxxx@gmail.com";
                    WebMail.Send(to: obj.ToEmail, subject: obj.EmailSubject, body: obj.EMailBody, cc: obj.EmailCC, filesToAttach:obj.filesToAttach, bcc: obj.EmailBCC, isBodyHtml: true);
                    ViewBag.Status = "Email Sent Successfully.";
                }
                catch (Exception e)
                {
                    ViewBag.Status = "Problem while sending email, Please check details.\n"+e.Message;
    
                }
                return View();
            }

    View

    @using (Html.BeginForm("SendEmail", "Test31", FormMethod.Post, new { enctype = "multipart/form-data" }))
    {
        @Html.AntiForgeryToken()
        <div class="form-horizontal">
            //The code here has not been modified, so the code is omitted.
            <div class="form-group">
                @Html.LabelFor(model => model.filesToAttach, htmlAttributes: new { @class = "control-label col-md-2" })
                <div class="col-md-10">
                    @Html.TextBoxFor(model => model.Files,"", new { @class = "form-control" ,@type = "file", @multiple = "multiple" })
              
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-offset-2 col-md-10">
                    <input type="submit" value="Send" class="btn btn-primary" />
                </div>
            </div>
             //The code here has not been modified, so the code is omitted.
        </div>
    }

    Here is the result.

    Best Regards,

    YihuiSun

    Thursday, July 30, 2020 7:40 AM
  • User1686398519 posted

    Hi zhyanadil.it,

    Has the problem you encountered solved? You can mark replies that are helpful to you, so that you can help other people who have similar problems.

    Best Regards,

    YihuiSun

    Monday, August 3, 2020 9:30 AM