Ask a questionAsk a question
 

Proposed AnswerEmail button?

  • Thursday, October 29, 2009 4:54 AM2cool4cereal2 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Ok, I'm reletivley new to all of this editing, (actually just started yesterday) and I was curious if anyone new a code I could insert in my program, that will bascially at LEAST get the information in all of the text boxes I have added, and email them to me when the user clicks accept/send. Sorry if this is an easy answer, like I said, I'm new.

     :-)

All Replies

  • Thursday, October 29, 2009 5:31 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    Hi,

    You can use the following method to send email.

    To send values to textboxes, you can concat them into a string and pass body parameter of this method.

            public void SendMail(string smtpAddress, string from,string to, string cc, string bcc, string body, string subject, bool isHtml,string attachmentFileNames)
            {
                SmtpClient insSmtpClient = new SmtpClient(smtpAddress);
                MailMessage insMailMessage = new MailMessage();
                insMailMessage.From = new MailAddress(from);
                foreach (string strBcc in bcc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Bcc.Add(strBcc);
                }
                foreach (string strTo in to.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.To.Add(strTo);
                }
                foreach (string strCc in cc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.CC.Add(strCc);
                }
                insMailMessage.Body = body;
                insMailMessage.Subject = subject;
                insMailMessage.IsBodyHtml = isHtml;
     
                foreach (string strAtt in attachmentFileNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Attachments.Add(new Attachment(strAtt));
                }
                insSmtpClient.Send(insMailMessage);
            }
    
    
  • Thursday, October 29, 2009 6:50 AMBulldog.NET Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Additional info: http://www.systemnetmail.com/

    Hope this helps.

    Bulldog.NET (South Africa)
  • Thursday, October 29, 2009 8:51 PMcool4cereal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    Hi,

    You can use the following method to send email.

    To send values to textboxes, you can concat them into a string and pass body parameter of this method.

            public
     void
     SendMail(string
     smtpAddress, string
     from
    ,string
     to, string
     cc, string
     bcc, string
     body, string
     subject, bool
     isHtml,string
     attachmentFileNames)
            {
                SmtpClient insSmtpClient = new
     SmtpClient(smtpAddress);
                MailMessage insMailMessage = new
     MailMessage();
                insMailMessage.From = new
     MailAddress(from
    );
                foreach
     (string
     strBcc in
     bcc.Split(";"
    .ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Bcc.Add(strBcc);
                }
                foreach
     (string
     strTo in
     to.Split(";"
    .ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.To.Add(strTo);
                }
                foreach
     (string
     strCc in
     cc.Split(";"
    .ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.CC.Add(strCc);
                }
                insMailMessage.Body = body;
                insMailMessage.Subject = subject;
                insMailMessage.IsBodyHtml = isHtml;
     
                foreach
     (string
     strAtt in
     attachmentFileNames.Split(";"
    .ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Attachments.Add(new
     Attachment(strAtt));
                }
                insSmtpClient.Send(insMailMessage);
            }
    
    

    Ok, thanks, (sorry for the different usename, mine messed up) but I don't know where to insert the code, could you show me in using my code?



    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace Blizzard
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                label1.Text = "Thank You For Sending The Form!";

            }

            private void progressBar1_Click(object sender, EventArgs e)
            {

            }

            private void textBox1_TextChanged(object sender, EventArgs e)
            {

            }

            private void radioButton1_CheckedChanged(object sender, EventArgs e)
            {

            }

            private void label2_Click(object sender, EventArgs e)
            {

            }

            private void label1_Click(object sender, EventArgs e)
            {

            }

            private void radioButton2_CheckedChanged(object sender, EventArgs e)
            {

            }

            private void Form1_Load(object sender, EventArgs e)
            {

            }

            private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
            {

            }

            private void folderBrowserDialog1_HelpRequest(object sender, EventArgs e)
            {

            }

            private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
            {

            }

            private void notifyIcon1_MouseDoubleClick(object sender, MouseEventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {

            }

            private void toolStripContainer1_ContentPanel_Load(object sender, EventArgs e)
            {

            }
        }
    }
     
  • Friday, October 30, 2009 3:51 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    For example

    private void button2_Click(object sender, EventArgs e)
            {
    string body="";
    body+="Name:" + TextBox1.Text;
    ............
    SendMail(smtpserverip, from@from.com,to@to.com, cc@cc.com, bcc@bcc.com, body, "subject", true,"")

            }
  • Friday, October 30, 2009 12:38 PMcool4cereal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    private void button2_Click(object sender, EventArgs e) { string body=""; body+="Code:" + textBox1.Text; SendMail(smtpserverip, from@from.com,to@to.com, cc@cc.com, bcc@bcc.com, body, "subject", true,"") All I get is a BUNCH of errors... Filled out the info correctly too. :-(
  • Friday, October 30, 2009 12:40 PMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Please send the code how you call SendMail method.
  • Friday, October 30, 2009 9:21 PMcool4cereal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Sorry, I don't know how to send you a personal message, so I'll give you my direct code here.





    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;

    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }

            private void label1_Click(object sender, EventArgs e)
            {

            }
        }
    }


    That's my ENTIRE code, I've added quite a few things to the program, I guess this is all the code needs though.
  • Saturday, October 31, 2009 11:27 AMTamer OzMVPUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Proposed AnswerHas Code
    Hi,

    I modified your code. Try using this,

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Net.Mail;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
    
            private void label1_Click(object sender, EventArgs e)
            {
                SendMail("127.0.0.1", "from@mymail.com", "to@mail.com", "", "", "mailbodyhere", "mailsubjecthere", true, "");
            }
            public void SendMail(string smtpAddress, string from, string to, string cc, string bcc, string body, string subject, bool isHtml, string attachmentFileNames)
            {
                SmtpClient insSmtpClient = new SmtpClient(smtpAddress);
                MailMessage insMailMessage = new MailMessage();
                insMailMessage.From = new MailAddress(from);
                foreach (string strBcc in bcc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Bcc.Add(strBcc);
                }
                foreach (string strTo in to.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.To.Add(strTo);
                }
                foreach (string strCc in cc.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.CC.Add(strCc);
                }
                insMailMessage.Body = body;
                insMailMessage.Subject = subject;
                insMailMessage.IsBodyHtml = isHtml;
    
                foreach (string strAtt in attachmentFileNames.Split(";".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
                {
                    insMailMessage.Attachments.Add(new Attachment(strAtt));
                }
                insSmtpClient.Send(insMailMessage);
            }
    
    
        }
    }
    
    


  • Monday, November 02, 2009 3:11 AMcool4cereal Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    When using

    public
     partial
     class
     Form1 : Form
    I get an error saying that basically, that already exists. This whole program creating is too impossible, im about to give up. :-(
  • Wednesday, November 04, 2009 2:03 AMHarry ZhuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi,

    Then you need to remove the original class named Form1. If possible , please post code of the .cs file.

    Harry
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.