Email button?
- 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
- 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); }- Proposed As Answer byPieter Joost van de SandeMVPThursday, October 29, 2009 6:54 AM
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)
{
}
}
}
- 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,"")
} - 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. :-(
- Please send the code how you call SendMail method.
- 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. - 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); } } }
- Proposed As Answer byHarry ZhuMSFT, ModeratorWednesday, November 04, 2009 2:03 AM
- 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. :-(
- 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.


