如何从发送到我的电子邮件的文本框中获取信息? (C#,Visual studio 2013)
How do I get information from a text box sent to my email? (C#, Visual studio 2013)
是的,我知道它非常广泛,但我是 C# 编程的新手。在我的系统中有几个文本框,当用户按下我程序上的按钮时,我希望将信息发送到我的电子邮件。
文本框名称:
textbox1
textbox2
按钮名称:
btnsend
到目前为止的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsFormsApplication1
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage("myemail", "myemail2");
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail");
mail.To.Add("myemail2");
mail.Subject = textBox1.Text;
mail.Body = textBox2.Text;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail","myemailpassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Email sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
代码现在正在尝试执行预期的操作。现在,当我按下 button1 时出现此错误:
System.Net.Mail.SmtpException: SMTP 服务器要求安全连接或客户端未通过身份验证服务器响应为 5.5.1 需要身份验证。
要通过 C# 发送电子邮件,您需要更多练习 - 这里有一些 link-
- Send Email in c#
- C#.Net How To: Send email from gmail using c#
- Send Email via SMTP(Stack Overflow)
这是post-
的摘录
现在您只需要做的就是,从您的文本框中获取值,并按照您的要求进行输入。假设,Subject 的文本框是 textbox1,body 是 textbox2。
你可以这样写代码-
// Attach the btnSend click event where it is created or before it suites better in you code.
btnsend.Click += btnsend_Click;
void btnsend_Click(object sender, RoutedEventArgs e)
{
MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.To.Add("reciever@example.com");
mail.Subject = textbox1.Text;
mail.Body = textbox2.Text;
client.Send(mail);
}
是的,我知道它非常广泛,但我是 C# 编程的新手。在我的系统中有几个文本框,当用户按下我程序上的按钮时,我希望将信息发送到我的电子邮件。
文本框名称:
textbox1
textbox2
按钮名称:
btnsend
到目前为止的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net.Mail;
namespace WindowsFormsApplication1
{
public partial class form1 : Form
{
public form1()
{
InitializeComponent();
}
button1.Click += button1_Click;
private void button1_Click(object sender, EventArgs e)
{
try
{
MailMessage mail = new MailMessage("myemail", "myemail2");
SmtpClient SmtpServer = new SmtpClient("smtp.gmail.com");
mail.From = new MailAddress("myemail");
mail.To.Add("myemail2");
mail.Subject = textBox1.Text;
mail.Body = textBox2.Text;
SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential("myemail","myemailpassword");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Email sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
}
}
代码现在正在尝试执行预期的操作。现在,当我按下 button1 时出现此错误: System.Net.Mail.SmtpException: SMTP 服务器要求安全连接或客户端未通过身份验证服务器响应为 5.5.1 需要身份验证。
要通过 C# 发送电子邮件,您需要更多练习 - 这里有一些 link-
- Send Email in c#
- C#.Net How To: Send email from gmail using c#
- Send Email via SMTP(Stack Overflow)
这是post-
的摘录现在您只需要做的就是,从您的文本框中获取值,并按照您的要求进行输入。假设,Subject 的文本框是 textbox1,body 是 textbox2。
你可以这样写代码-
// Attach the btnSend click event where it is created or before it suites better in you code.
btnsend.Click += btnsend_Click;
void btnsend_Click(object sender, RoutedEventArgs e)
{
MailMessage mail = new MailMessage("you@yourcompany.com", "user@hotmail.com");
SmtpClient client = new SmtpClient();
client.Port = 25;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "smtp.google.com";
mail.To.Add("reciever@example.com");
mail.Subject = textbox1.Text;
mail.Body = textbox2.Text;
client.Send(mail);
}