-2

I would like to add an extra email address for messages to be sent/forwarded to when the email form submit button is clicked, whats the easiest way to do this?

public partial class Contact : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
    string name = txtName.Text;
    string emailaddress = txtEmail.Text;
    string body = txtComment.Text;


    MailAddress From = new MailAddress(emailaddress);
    MailAddress To = new MailAddress("111@1111.com");
    MailMessage email = new MailMessage(From, To);
    email.Subject = "Comment from Website from " + name;
    email.Body = body;

    SmtpClient smtp = new SmtpClient("smtp.1111.com");
    smtp.Credentials = new System.Net.NetworkCredential("111@1111.com", "1111111111");
    smtp.Send(email);
    email.Dispose();

    ClientScript.RegisterClientScriptBlock(this.GetType(), "Email Confirm", "alert('Email Sent!');", true);

    txtComment.Text = "";
    txtEmail.Text = "";
    txtName.Text = "";


}
protected void txtEmail_TextChanged(object sender, EventArgs e)
{

}
}

We'll say that my extra email address is smtp.2222.com 222@2222.com with an authentication of 22222. Thanks for looking folks.

  • A new email address or a new SMTP client? If it's a new email address, there should be a `CC` property in your MailMessage object. If it's an SMTP client, you have to execute this twice, once for each SMTP client. – Robert Harvey Mar 18 '13 at 23:09
  • Forgive me, I'm still new to C#. The new email address would be a facebook email address, where as the original is not. – Andrew Staheli Mar 18 '13 at 23:13
  • I'm sorry, I don't understand why a new question needed to be opened for this. For example, a post here asks the same question: http://stackoverflow.com/questions/3209129/sending-an-email-to-multiple-addresses-recipients. A quick google search of "send email c# multiple recipients" brings you back hundreds of relevant results. It's OK to ask questions, just please do research on your own first. – theMayer Mar 18 '13 at 23:26
  • And theres that one guy. – Andrew Staheli Mar 19 '13 at 15:55
  • I apologize if I seemed gruff, but I'm also an engineering instructor, and sometimes I feel that being blunt is the best way to get a point across. If a student asks me a question that I know he can answer on his own, I tell him to do so. – theMayer Mar 20 '13 at 04:14

2 Answers2

4

Simple:

…
MailAddress From = new MailAddress(emailaddress);
MailMessage email = new MailMessage(from, to);
email.To.Add(new MailAddress("111@1111.com"));
email.To.Add(new MailAddress("222@2222.com"));
email.Subject = "Comment from Website from " + name;
email.Body = body;
…
Mintey
  • 118
  • 11
-1

Instead of using the To.Add method, you can pass a comma separated list of email address to MailMessage constructor which is a much better solution especially if you have more than a few email addresses to send the email to...

var from = "me@me.com";
var to = "mail1@domain.com,mail2@domain.com,mail3@domain.com";
var subject = "My Subject";
var body = "Message Body";
var message = new MailMessage(from, to, subject, body);

will work just as well...

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • Thanks, I really appreciate your input! c#, and web development in general, has really been testing my learning curve. I'd one up you both, but it seems my rep keeps getting negative hits. – Andrew Staheli Mar 19 '13 at 15:46
  • Np, I'd love to hear from the person who downvoted this because it most certainly works and it saves lines of code... – Dean Kuga Mar 20 '13 at 15:30