Click here to Skip to main content
15,887,283 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi, I'm trying to setup email options for a web form.However if i don't put any condition and try to send a general email, it works, but if at all i try to put any condition as to send it different recipents depending on the report type.The page loads up again but no email is sent.Here is the piece of code for sending the email.It would really helpful if you could help me out with this.

C#
foreach (GridViewRow row in reportTable.Rows)
            {
                foreach (dbTypes.report report in reports)
                {

                    CheckBox cb = (CheckBox)row.Cells[8].FindControl("cb");
                    cb = (CheckBox)reportTable.Rows[i].Cells[8].FindControl("cb");
                    testBench b = new testBench();

                    if (cb.Checked && b.costPos == "123")
                    {
                        MailMessage message = new MailMessage("no-reply@gmail.com",
               "tony.romo@gmail.com", "Kostenstelle  : 123",
               "Kostenstelle : 123");


                        /* create SMTP Client and add credentials */
                        SmtpClient smtpClient = new SmtpClient("SERVER");
                        smtpClient.UseDefaultCredentials = true;
                        /* Email with Authentication */
                        //smtpClient.Credentials = new NetworkCredential("userID",
                        //"password", "domainName");

                        /*Send the message */
                        smtpClient.Send(message);
                    }
Posted
Updated 20-Aug-12 21:54pm
v3
Comments
Mario Majčica 21-Aug-12 4:09am    
Are you sure that the condition is met? If you set a break point on smtpClient.Send(message); does it hit?
Member 9196002 21-Aug-12 4:31am    
you were right..the condition is not met.

Hi,

Possible issues:

1) Exception occur at cb.Checked if checkbox not found.
2) Your condition is not met and it is not able to execute if code.

Resolution:

1) Add try ...catch block and check if any exception occur.
2) add else condition and prompt appropriate message.

Thanks
-Amit Gajjar
 
Share this answer
 
Comments
Member 9196002 21-Aug-12 4:31am    
the problem is that the condition is not meeting
AmitGajjar 21-Aug-12 4:33am    
ok then you need to check two things,
1) is b.costPos value is 123 ?
2) is cb is not null and cb is checked ?
Member 9196002 21-Aug-12 4:40am    
Oh ok.Does it make a difference if you are referencing b.costPos to a value from a datatable? 123 is the value stored in the document.
and cb is not null..i made sure its checked
AmitGajjar 21-Aug-12 4:42am    
just your b.costPos value should match your datatable value. and add breakpoint inside your if condition and check if you got hit there ? if not then your b.costPos must be different value then 123.
Member 9196002 21-Aug-12 4:46am    
ok!!i shall check that..i guess it should work!! thanks a lot!! :)
 
Share this answer
 
Well, what I see, you didn't provide any user name/password, if your SMTP server supports relay.

Check the code,
smtpClient = new SmtpClient();
System.Net.NetworkCredential credentials = null;
smtpClient.Port = int_port;
smtpClient.Host = str_server;
if (str_ssl == "SSL")
{
    smtpClient.EnableSsl = true;
}
else
{
    smtpClient.EnableSsl = false;
}
if (str_isauthenticate == "Yes")
{
    smtpClient.UseDefaultCredentials = false;
    credentials =
            new System.Net.NetworkCredential(str_user, str_password);

    smtpClient.Credentials = credentials;
}
else
{
    smtpClient.UseDefaultCredentials = true;
    credentials =
            new System.Net.NetworkCredential();
    smtpClient.Credentials = credentials;

}


Hope this helps.
cheers
 
Share this answer
 

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