0

A question regarding send mail in mvc 3.

When I click on btnApply it should send 2 emails to abcd@gmail.com and also send an acknowledgement to (who filled email id in apply form like be 123@gmail.com)

For example:

  • Email1 : xyz@gamail.com
  • Email2 : abcd@gmail.com
  • Email3 : email entered in apply form, e.g 123@gmail.com

when a Email3 click apply send mail from Email1(Sender) to Email2(receiver) & Email3(receiver)

or

when a Email3 click apply send mail from Email2(Sender) to Email2(receiver) & Email3(receiver)

  1. I have form in popup:

     @using (Html.BeginForm()){
     Your Full Name
        <input type="text" value="" id="txtname" name="txtname" required />
        Your Email          
        <input type="email" value="" id="txtemail" name="txtemail"  required />    
        Upload Your Resume
        <input name="Upload Saved Replay" id="btnFile" type="file" />
        <input type="button" id="btnApply" name="btnApply" value="Apply" />
    }
    
  2. I have a email manager, it only send 1 mail that from xyz@gmail.com to email id that specified in apply form (123@gmail.com )

    public class EmailManager
    {
        private const string EmailFrom = "xyz@gmail.com";
        public static void Enquiry( int JobId, string UserName, string Email, string Massage)
        {
            using (var client = new SmtpClient()) {
                using (var message = new MailMessage(EmailFrom, Email)) {
                    message.Subject = "Successful";
                    message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + UserName +
                        ", </p> <p>Thankyou for Registering</p>"
                        + "</a></p><div>Best regards,</div><div>Nisha</div></body></html>";
                    message.IsBodyHtml = true;
                    client.EnableSsl = true;
                    client.Send(message);
                };
            };
        }
    }
    
Toby Speight
  • 27,591
  • 48
  • 66
  • 103
Rakyir
  • 59
  • 1
  • 2
  • 8
  • in your code `EmailFrom` is `xyz@gmail.com` so why do you want to send it to the same `xyz@gmail.com` – jamiedanq Apr 27 '16 at 11:16
  • I think you should be clear as to what you want to do. Which ones are `EmailFrom` and which ones you sending to `Email`. If you clarify that it is easier to know how to send your email. Also if you want other `Email`s copied into the **email** you are sending indicate it clearly – jamiedanq Apr 27 '16 at 11:18
  • Rename your variable `EmailFrom` to `EmailTo` for easier understanding and for best practice i suggest you comment your code to make it easier to read – jamiedanq Apr 27 '16 at 11:32
  • does your code even run? if it does what happens when it runs? – jamiedanq Apr 27 '16 at 11:50
  • for the meantime look at this it should help http://stackoverflow.com/questions/7498968/how-to-send-email-to-multiple-address-using-system-net-mail – jamiedanq Apr 27 '16 at 11:50

1 Answers1

0

You could use a for loop between two usings.

string[] Emails = { Email,"abcd@gmail.com", "xyz@gmail.com" }

for(var i = 0; i < 3; i++) 
{
    using (var message = new MailMessage(EmailFrom, Emails[i]))
    {   
        message.Subject = "Successful";
        message.Body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + UserName +
        ", </p> <p>Thankyou for Registering</p>"
        + "</a></p><div>Best regards,</div><div>Nisha</div></body></html>";
        message.IsBodyHtml = true;
        client.EnableSsl = true;
        client.Send(message);
    };
}

Variable Email comes from void Enquiry, others are hard coded

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459