C# 如何将值从文本框发送到邮件?

C# 如何将值从文本框发送到邮件?,c#,C#,我有了一个做邮件寄件人的想法。我试图寻找解决方案,但没有一个真正起作用,而且解释得很糟糕,或者是否有可能做到这一点? 所以我基本上有这个if-else代码来检查它是否为空,如果不是,它会将值发送到mail using System.Net.Mail; //i think this is what i need? private void button1_Click(object sender, EventArgs e) { if(string.IsNullOrWhiteSpace(te

我有了一个做邮件寄件人的想法。我试图寻找解决方案,但没有一个真正起作用,而且解释得很糟糕,或者是否有可能做到这一点?
所以我基本上有这个
if-else
代码来检查它是否为空,如果不是,它会将值发送到mail

using System.Net.Mail; //i think this is what i need?

private void button1_Click(object sender, EventArgs e)
{
    if(string.IsNullOrWhiteSpace(textBox1.Text) && string.IsNullOrWhiteSpace(textBox2.Text))
    {
        MessageBox.Show("You're empty!");
    }
    else if(Int32.Parse(textBox1.Text) != 0)
    {
        // send text box to mail
    }
    else
    {
        MessageBox.Show("Something went wrong.");
        System.Threading.Thread.Sleep(2000);
        MessageBox.Show("Closing");
        System.Threading.Thread.Sleep(1000);
        this.Close();
    }
}

是否有人愿意为我指明正确的方向或帮助我解释如何做?

您可以将textBox1.Text作为电子邮件正文,如下所示:

mail.From = new MailAddress(emailaddress);
mail.To.Add(recipient);
mail.Subject = "Test Mail";

mail.Body = textBox1.Text; // sets the body to the text box's content

SmtpServer.Port = 587;
SmtpServer.Credentials = new System.Net.NetworkCredential(emailaddress, password);
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);

System.Windows.Forms.MessageBox.Show("Mail sent");

你为什么要对文本框进行整数解析?这样我也可以发送数字?你不需要解析它,你可以将数字作为字符串值发送..NET有一个非常详细的文档,我建议你阅读其中的示例:你能解释端口587的用途吗?端口587是一个MSA(消息提交代理)需要SMTP身份验证的端口&通常在ISP阻止端口25的情况下使用,而不是端口25。更多信息可在此处找到:
Try this :
private void button1_Click(object sender, EventArgs e)
{
sendMailToAdmin(textbox1.Text,textbox2.text);
}

protected void sendMailToAdmin(string uname, string email)
{
    MailMessage myMsg = new MailMessage();
    myMsg.From = new MailAddress("****@mail.com");
    myMsg.To.Add(email);
    myMsg.Subject = "New User Email ";
    myMsg.Body = "New User Information\n\nUser Name: " + uname + "\nEmail : " + email;

    // your remote SMTP server IP.
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 587;
    smtp.Credentials = new System.Net.NetworkCredential("****@mail.com", "pass***");
    smtp.EnableSsl = true;
    smtp.Send(myMsg);
}