?
Solved

Sending an e-mail via set template

Posted on 2008-06-16
31
Medium Priority
?
232 Views
Last Modified: 2013-11-26
I am using MS Visual Studio 2005 .Net C#. I am trying to create a sample e-mail setup. i.e.

I would like an e-mail set up when a user clicks on the button will create an email notification sending out to an e-mail address saying data submitted etc... but also displaying what in the email whatever data has been inserted in that particular txt box.

Thanks!

introlux
0
Comment
Question by:introlux
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 14
31 Comments
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21791888
Hi,

I have made a quick example here for you.  So say for any for you have, for the event of the submit button, either add this code or have it as the event handler.  Basically it emnails you all the information from the posted form values prefixing with the name.

Cheers

Andrew

    protected void TemplatedEmailFormData(object sender, EventArgs e)
    {
        StringBuilder sb1 = new StringBuilder();
        foreach (string s in Request.Form)
        {
            sb1.AppendLine(s + " : " + Request.Form[s]);
        }
        MailAddress FromAddress = new MailAddress("user1@email.com");
        MailAddress ToAddress = new MailAddress("user1@email.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Body = sb1.ToString();
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }

Open in new window

0
 

Author Comment

by:introlux
ID: 21791956
I would only like the data from one txt box rather than all the values enetered by the user.

introlux
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21791991
then simply this

Andrew
    protected void TemplatedEmailFormData(object sender, EventArgs e)
    {
        MailAddress FromAddress = new MailAddress("user1@email.com");
        MailAddress ToAddress = new MailAddress("user1@email.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Body = YourTextBox.Text;
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }

Open in new window

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:introlux
ID: 21792283
What about the subject of the e-mail?? where does that go??

Thanks,

introlux
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21792301
Cheers,

Andrew
   protected void TemplatedEmailFormData(object sender, EventArgs e)
    {
        MailAddress FromAddress = new MailAddress("user1@email.com");
        MailAddress ToAddress = new MailAddress("user1@email.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Body = YourTextBox.Text;
        message.Subject = "Subject";
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }

Open in new window

0
 

Author Comment

by:introlux
ID: 21792317
I am sorry as I am on the beginner level. The code i assume is added into the CS file which I have. How will I ensure that this has run after the button has been clicked.

Also in the body, I would like to inserted the text.box while in a sentence. E.g. The new project txtPName.text has been commissioned.

How will I do this??
    protected void TemplatedEmailFormData(object sender, EventArgs e)
    {
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text 'Has now been commissioned';
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }

Open in new window

0
 

Author Comment

by:introlux
ID: 21793553
??????
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21793580
OK, you are simply missing a plus sign:

Cheers

Andrew :-)
   protected void TemplatedEmailFormData(object sender, EventArgs e)
    {
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + 'Has now been commissioned';
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21793588
oh and use double quotes not single quotes.
0
 

Author Comment

by:introlux
ID: 21793607
I take it you put this code in the CS. But how will i execute this command???

thanks,

introlux
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21793722
you will have a Button, so you simple add this to its onclick event

OnClick="TemplatedEmailFormData"
0
 

Author Comment

by:introlux
ID: 21793851
What if I have a command already on the OnClick=

Is it possible to add two multiple commands???

0
 

Author Comment

by:introlux
ID: 21794669
????????
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21795070
yes, so just add this code block, either at the end or the beginning of your current function

aslong as you code between the two curly brackets you can put as much code in there as you want.  

Andrew
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + 'Has now been commissioned';
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);

Open in new window

0
 

Author Comment

by:introlux
ID: 21795105
When trying to run it, I am getting the following error message:

Compiler Error Message: CS0246: The type or namespace name 'MailAddress' could not be found (are you missing a using directive or an assembly reference?)

Source Error:

 

Line 21:        protected void TemplatedEmailFormData(object sender, EventArgs e)
Line 22:     {
Line 23:         MailAddress FromAddress = new MailAddress("project@theidfactor.com");
Line 24:         MailAddress ToAddress = new MailAddress("project@theidfactor.com");
Line 25:         MailMessage message = new MailMessage(FromAddress, ToAddress);
 
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21795426
at the top you need to inlude this line

using System.Net.Mail;
0
 

Author Comment

by:introlux
ID: 21800968
I get this error now:

Compiler Error Message: CS0149: Method name expected

Source Error:

 

Line 563:</asp:Table>
Line 564:    &nbsp;<br />
Line 565:<asp:Button id="btnLogin" runat="server" Text="Print" onClick="AddCredentials_Click, TemplatedEmailFormData" Height="55px" Width="84px" />&nbsp;
Line 566:</form>
Line 567:</td>
 
0
 

Author Comment

by:introlux
ID: 21802781
??????????????
0
 

Author Comment

by:introlux
ID: 21803014
This is a very simple error which I have looked all over the internet but no luck. I have tried every possible ways to have multiple commands being executed

Thanks
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21803132
Before you attempt this you need to read up on Basic C# syntax and event handlers, as I am struggling to jump in and fill all the gaps.  Have a read through some tutorials first.
0
 

Author Comment

by:introlux
ID: 21803233
I have read through some examples and all only explain how to execute one command. Hence I have two:

AddCredentials_Click
TemplatedEmailFormData

AddCredentials_Click = This adds data into the access database
TemplatedEmailFormData = This sends an e-mail

I want both of these to run at the same time the button 'Print' is clicked by the user.

Thanks
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21803253
check this.

I have

pubilc void DoSomethingA()
{
}

and
pubilc void DoSomethingB()
{
}

I could simply write this:

pubilc void DoSomethingA(object sender,EventArgs e)
{
DoSomethingB();
DoSomethingC();
}

OnClick="DoSomethingA"

So here I call DoSomethingA on the onclick and in turn it then calls the other two functions DoSomthingB and DoSomethingC

Cheers

Andrew :-)
0
 

Author Comment

by:introlux
ID: 21804576
I have done what you have said. I am now getting the following error message:

Compiler Error Message: CS1501: No overload for method 'CheckCredentials_Click' takes '0' arguments

Source Error:

 

Line 32:     void multipleTask(object sender,EventArgs e)
Line 33:     {
Line 34:         CheckCredentials_Click();
Line 35:         TemplatedEmailFormData();
Line 36: }
 
0
 

Author Comment

by:introlux
ID: 21813547
????????????
0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21813715
I meant fo you to get that from my example, you simply need to place the other function call inside your original function for the button.

AddCredentials_Click(object sender, EventArgs e)
{
//YOUR CODE HERE!!

//NOW MY CODE
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + 'Has now been commissioned';
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);

//NOW ANY OTHER FUNCTION IF YOU WANT
AnotherFunctionOrTwoHere();

//NOW CLOSE THE FUNCTION BLOCK
}


0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21813732
p.s. make sure you know and insert the email provider host you have. replace the 127.0.0.1 with the actual email host address if it is not the same as that.  i.e. a dedictaed server
0
 

Author Comment

by:introlux
ID: 21813831
Im still having problem with the layout you have just showed me. The mail host i will sort that out later

Thanks
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Net.Mail;
 
public partial class CreateProCom : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    void CheckCredentials_Click(Object s, EventArgs e) {
		Response.Redirect("Default.aspx");
 
        TemplatedEmailFormData();
    {
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + "Has now been commissioned";
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
    }
 
    }
}

Open in new window

0
 
LVL 20

Expert Comment

by:REA_ANDREW
ID: 21813917

    void CheckCredentials_Click(Object s, EventArgs e) {
		
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + "Has now been commissioned";
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
 
        Response.Redirect("Default.aspx");
    }

Open in new window

0
 

Author Comment

by:introlux
ID: 21813961
I get this error now:

Compiler Error Message: CS0122: 'CreateProCom.CheckCredentials_Click(object, System.EventArgs)' is inaccessible due to its protection level

Source Error:

 

Line 563:</asp:Table>
Line 564:    &nbsp;<br />
Line 565:<asp:Button id="btnLogin" runat="server" Text="Print" onClick="CheckCredentials_Click" Height="55px" Width="84px" />&nbsp;
Line 566:</form>
Line 567:</td>
 
0
 
LVL 20

Accepted Solution

by:
REA_ANDREW earned 2000 total points
ID: 21814132
protected void CheckCredentials_Click(Object s, EventArgs e) {
            
        MailAddress FromAddress = new MailAddress("project@theidfactor.com");
        MailAddress ToAddress = new MailAddress("project@theidfactor.com");
        MailMessage message = new MailMessage(FromAddress, ToAddress);
        message.Subject = "New Project Has been commissioned";
        message.Body = txtPName.Text + "Has now been commissioned";
        SmtpClient client = new SmtpClient("127.0.0.1", 25);
        client.Send(message);
 
        Response.Redirect("Default.aspx");
    }
0
 

Author Closing Comment

by:introlux
ID: 31467510
Thank you!
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question