I am using InfoPath 2007 with SharePoint 2007. I've created a submit button that reads the next List ID* from a SharePoint List and places it the "fileName" field and submits the form to a SharePoint form library. It works great. When I add C# code behind the same Submit button to send an email, the email does not send. When I place the same code in another button, the email sends just fine. So it seems I can't put rules and code on the same button??
*I used Clayton Cobb's tutorial for autonumering fields: http://claytoncobb.wordpress.com/2009/06/15/auto-numbering-infopath-forms/
I don't want the user to have to take an additional step to send an email to all interested parties. I want the email that is sent to be transparent to the user. Also I don't want to use the buit-in email wizard because I don't want the user to have to deal with the message box window. As it is they will have to click yes or no after they click submit button. Must I create a workflow to achieve this instead?
Here's my email code:
//Submit button
try
{
SmtpClient client = new SmtpClient(@"server.domain "); MailAddress from = new MailAddress("[email protected]", "Tommy Test");
MailAddress to = new MailAddress("[email protected]", "Testing to Dtest");MailAddress to2 = new MailAddress("[email protected]", "Sally Test");
MailAddress to3 = new MailAddress("[email protected]", "Tommy Test");MailAddress copy = new MailAddress("[email protected]", "Sally Test");
System.Net.Mail.MailMessage eMailMsg = new System.Net.Mail.MailMessage();
eMailMsg.Body =
@"System user: " + System.Environment.UserName + " has entered a new *TEST* Incident Report. View it here: http://server ";eMailMsg.Subject = "InfoPath Test Email generated from code";
eMailMsg.From = from;
eMailMsg.To.Add(to);
eMailMsg.To.Add(to2);
eMailMsg.To.Add(to3);
eMailMsg.CC.Add(copy);
client.Send(eMailMsg);
//MessageBox.Show("Your Email Message has been sent to " + to);
//clean up
eMailMsg.Dispose();
}
catch (ArgumentException exc)
{
MessageBox.Show(exc.Message, exc.Source);
}
catch (SmtpException ex)
{
MessageBox.Show(ex.ToString());
}