【问题标题】:Cannot pass variable from text box无法从文本框中传递变量
【发布时间】:2013-04-13 19:14:41
【问题描述】:

上线

const string subject = "hello" + textBox1.Text;

它不会打印 textBox1.Text 的值,但我会打印单词“textBox1.Text”。我该如何解决?

var fromAddress = new MailAddress("samuimark@gmail.com", "samui mail");
var toAddress = new MailAddress("teeramail@gmail.com", "To Name");
const string fromPassword = "t429";
const string subject = "hello" + textBox1.Text  ;
const string body = "you have a mail";

var smtp = new SmtpClient
{
    Host = "smtp.gmail.com",
    Port = 587,
    EnableSsl = true,
    DeliveryMethod = SmtpDeliveryMethod.Network,
    UseDefaultCredentials = false,
    Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
    Subject = subject,
    Body = body
})
{
    smtp.Send(message);
}

【问题讨论】:

  • 试过删除 const 吗?字符串主题 = "你好" + textBox1.Text ;

标签: c# email smtp gmail


【解决方案1】:

代替

const string subject = "hello" + textBox1.Text  ;

使用

string subject = "hello" + textBox1.Text;

【讨论】:

  • 其他人在技术上也说了同样的话。但是根据问题的类型,我在这里意识到复制/粘贴很重要。很高兴它对你有用。
【解决方案2】:

textBox1.Text 不是常量表达式。将其值赋值给字符串而不是 const 字符串

string subject = "hello" + textBox1.Text;

【讨论】:

    【解决方案3】:

    习惯使用

    Subject = subject,
    Body = body
    

    我建议你也改一下

    Subject = subject.Text,
    Body = body.Text
    

    只需将“subject.Text”和“body.Text”更改为您的文本框的名称 但你必须保留 .Text

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 1970-01-01
      • 2014-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多