C#如何将带有值的变量包含到要作为电子邮件发送的电子邮件正文中
我有一个“自动生成”的随机数方法,该值存储在一个字符串中。我需要变量的值显示在C#如何将带有值的变量包含到要作为电子邮件发送的电子邮件正文中,c#,mysql,email,C#,Mysql,Email,我有一个“自动生成”的随机数方法,该值存储在一个字符串中。我需要变量的值显示在mail.Body属性中。 这是我必须生成随机数的代码: String id = " "; Random rnd = new Random(); for(int a = 0; a <8; a++){ id += rnd.Next(0,9); } 但我收到的电子邮件中只包含“您的新价值是”作为正文,没有自动生成的价值 我能做些什么来解决这个问题?谢谢 尝试string.Format string Bod
mail.Body
属性中。
这是我必须生成随机数的代码:
String id = " ";
Random rnd = new Random();
for(int a = 0; a <8; a++){
id += rnd.Next(0,9);
}
但我收到的电子邮件中只包含“您的新价值是”作为正文
,没有自动生成的价值
我能做些什么来解决这个问题?谢谢 尝试
string.Format
string Body = string.Format("Your New Value is {0}", id);
另外,您确定id
包含您期望的内容吗
在数字生成器上调用.ToString()
,可能会有帮助:
String id = string.Empty;
Random rnd = new Random();
for(int a = 0; a <8; a++){
id += rnd.Next(0,9).ToString(); {
String id=String.Empty;
随机rnd=新随机();
对于(int a=0;apublic class GenerateRandomNum)
{
私有静态字符串id=“”;
公共静态字符串RandomNum()
{
随机rnd=新随机();
对于(inta=0;a请尝试此C#代码
Random rnd=new Random();
System.Text.StringBuilder结果=新建StringBuilder();
对于(int a=0;a<8;a++)
{
追加结果(rnd.Next(0,9));
}
mail.Body=string.Format(“您的新值是{0}',result.ToString());
由于第id+=rnd.Next(0,9);
行的原因,您的代码无法工作。您尝试将int转换为字符串。
它应该使用这个id+=rnd.Next(0,9).ToString();
请不要使用“+”运算符或“+=”运算符来压缩字符串。请改用StringBuilder。是否尝试运行此示例?如果修复了编译器错误(在第1行添加分号,请在第5行将{
更改为}
)它只是按预期工作。请显示实际重现问题的工作代码。它没有足够的代码段来查找错误。
String id = string.Empty;
Random rnd = new Random();
for(int a = 0; a <8; a++){
id += rnd.Next(0,9).ToString(); {
public class GenerateRandomNum
{
private static string id = "";
public static string RandomNum()
{
Random rnd = new Random();
for(int a = 0; a <8; a++)
{
id +=(string)rnd.Next(0,9);
}
}
}
class TestRandom
{
public static void Main()
{
string Body = "Your New Value is " + GenerateRandomNum.RandomNum();
mail.Body = Body;
}
}
Random rnd = new Random();
System.Text.StringBuilder result = new StringBuilder();
for (int a = 0; a < 8; a++)
{
result.Append(rnd.Next(0,9));
}
mail.Body = string.Format("Your New Value is '{0}'", result.ToString());