作为C#的新手,我尝试使用c#生成HTML电子邮件。我做对了吗?我敢肯定有很多错误。我非常感谢您的帮助。
foreach my $key (sort keys %KEY_VALUE) {
my @value = @{$KEY_VALUE{$key}};
my @indices = map { my $e = $_; first_index { $_ eq $e } @value } (uniq (sort @value));
print "$key: " . (join ', ', @indices) . "\n";
}
错误:
当前不存在名称“ toname”,“ toemail”,“ from”,“ ccname”,“ body” 上下文。
'CreateEmail'不包含对'Run'的定义,找不到扩展方法和接受第一个类型实参的扩展方法(是否缺少指令或程序集引用。
这是创建电子邮件的类。
class Program // Main class is giving me errors.
{
String user = "xyz@abc.com";
String firstName = "John";
String lastName = "Doe";
CreateEmail createEmail = new CreateEmail(toname, toemail, from, ccname, ccemail, body);
createEmail.Run();
答案 0 :(得分:0)
我建议在使用后处置您的MailMessage。然后像Ivien指出的那样,将body = XXX放在作业之前。
try
{
using (MailMessage mail = new MailMessage())
{
mail.To.Add(toemail);
mail.To.Add(ccname);
mail.From = new MailAddress(from);
mail.Subject = "test email";
body = "<div> Hello, this is the body content of the email.</div>";
mail.Body = body;
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient("localhost");
client.Send(mail);
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in Creating test email {0}", ex.ToString());
}
答案 1 :(得分:0)
第一个错误是因为您没有在CreateEmail构造函数中传递必需的数据。 在您的示例情况下,您需要创建字符串变量,并在创建CreateEmail时传递它们。
String toemail = "xyz@abc.com";
String toname = "John";
String from = "Emamr";
String ccname = "some cc name";
String from = "some cc email";
String body = "Some text for mail body";
CreateEmail createEmail = new CreateEmail(toname, toemail, from, ccname, ccemail, body);
第二个错误是因为您正在调用方法Run(),该方法在CreateEmail类的任何地方都没有定义。您有方法Execute()
您需要这样称呼它:createEmail.Execute();