Click here to Skip to main content
15,895,557 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C#
try
       {
  foreach (GridViewRow gv in GDStudents.Rows)
           {
       if (gv.Cells[5].Text != "";)
                {
                EmailChange:
                string fromEmailid = " ";
                   if (Email_NO == 1)
                       fromEmailid = "vk@gmail.com";
                   else if (Email_NO == 2)
                       fromEmailid ="rk@gmail.com";
                   else
                       fromEmailid ="";

                MailMessage mailmess = new MailMessage();
                SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
                smtpserver.Credentials = new                 NetworkCredential("vk@hgmail.com","1234");
                smtpserver.Host = "smtp.gmail.com";
                smtpserver.Port = 587;
                smtpserver.EnableSsl = true;
                mailmess.From = new MailAddress("vk@gmail.com","Confirmation");
                mailmess.IsBodyHtml = true;
                mailmess.Subject = "ConfirmationMail";

                 string body2 = "<table>";
                 body2 = body2 + "<tr><td><b>Dear <font color='Maroon'><b>" + gv.Cells[1].Text.ToString().Trim() + ",</font></td></tr>";
                    body2 = body2 + "<tr><td><br/>&nbsp;&nbsp;&nbsp;<font face='Arial' size='2' color='Navy'><b> " + TxtMessage.Text.ToString().Trim() + " </font></td></tr>";
                    body2 = body2 + "</table>";
                    mailmess.Body = body2;

     try
     {
foreach (GridViewRow gv in GDStudents.Rows)
         {
     if (gv.Cells[5].Text != "&nbsp;")
              {
              EmailChange:
              string fromEmailid = "";
                 if (Email_NO == 1)
                     fromEmailid = "vk@gmail.com";
                 else if (Email_NO == 2)
                     fromEmailid ="rk@gmail.com";
                 else
                     fromEmailid ="";

              MailMessage mailmess = new MailMessage();
              SmtpClient smtpserver = new SmtpClient("smtp.gmail.com");
              smtpserver.Credentials = new                 NetworkCredential("vk@hgmail.com","1234");
              smtpserver.Host = "smtp.gmail.com";
              smtpserver.Port = 587;
              smtpserver.EnableSsl = true;
              mailmess.From = new MailAddress("vk@gmail.com","Confirmation");
              mailmess.IsBodyHtml = true;
              mailmess.Subject = "Confirmation Mail"

              try
                  {
                      mailmess.To.Add(gv.Cells[5].Text.ToString().Trim());
                  }
            catch
                  {

                  }

                string body2 = "<table>";
                  body2 = body2 + "<tr><td><b>Dear <font color='Maroon'><b>" + gv.Cells[1].Text.ToString().Trim() + ",</font></td></tr>";
                  body2 = body2 + "<tr><td><br/>&nbsp;&nbsp;&nbsp;<font face='Arial' size='2' color='Navy'><b> " + TxtMessage.Text.ToString().Trim() + " </font></td></tr>";
                  body2 = body2 + "</table>";
                  mailmess.Body = body2;

                  
               mailmess.To.Clear();
                mailmess.To.Add(new MailAddress(gv.Cells[5].Text.ToString().Trim()));
                Thread threademail = new Thread(delegate()
                {
                    sc.Send(mm);
                });
                threademail.IsBackground = true;
                threademail.Start();
            }
        }
        catch (Exception e11)
        { }



Run mode as follows

txtmessage.text = "Dear Student, Thanks for booking the course.";

Studname Emailid
Rakesh rakesh@gmail.com
Magesh magesh@gmail.com

I want to send the above txtmessage to below two mail id.

for one email id rakesh@gmail.com i want to send the message using one email id vk@gmail.com and another email id magesh@gmailid i want to send the message using another email id rk@gmail.com.

for that i how can i do using my above code what i sent to you.
Posted
Updated 19-Jan-16 23:06pm
v2
Comments
Sergey Alexandrovich Kryukov 19-Jan-16 22:11pm    
What are you talking about? This is not even a question.
Your code is pain to see. Write code in a neat way and you will get much less problems.
—SA

your are assigning
C#
string fromEmailid = "";
                 if (Email_NO == 1)
                     fromEmailid = "vk@gmail.com";
                 else if (Email_NO == 2)
                     fromEmailid ="rk@gmail.com";
                 else
                     fromEmailid ="";


but where are you using this?


see if u gonna pass different email id for diffrent users then there must be specific conditions for them, if u write inline conditions, then it means your code is not generic.

U need to have a unique id for sending two mails

Add a column FromEmailIdConfigruation column in db or grid:

Name EmailId FromEmailId
Rakesh Rakesh@gmail.com rk@gmail.com


then use this from email id in your code.
 
Share this answer
 
v2
Gmail doesn't support changing the "from" address. To send "from" an account you have to use that account's credentials. This is just one of the many problems you'll have using gmail to send emails, so just don't use gmail, use the SMTP server provided by your ISP or webhost or local network, whichever is appropriate.

Things you shouldn&#39;t spend time doing | The ASP.NET Forums[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900