メソッドに指定された値に応じて、送信者と受信者の電子メール アドレス (および件名と本文の内容) が変化する電子メールを送信しようとしています。おそらく、コード自体で最もよく説明されています。
public void EmailNotification(int emailAction)
{
MailAddress to;
MailAddress from;
string subject;
string body;
switch (emailAction)
{
case 1:
// Comment approved
to = new MailAddress("someone@theirdomain.com");
from = new MailAddress("no-reply@thisdomain.com");
subject = "Comment approved";
body = @"The comment you posted has been approved";
break;
case 2:
// Comment rejected
to = new MailAddress("someone@theirdomain.com");
from = new MailAddress("no-reply@thisdomain.com");
subject = "Comment rejected";
body = @"The comment you posted has been rejected";
break;
}
MailMessage message = new MailMessage(from, to);
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient();
try
{
client.Send(message);
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in EmailNotification: {0}", ex.ToString());
}
}
したがって、問題は、スコープがスイッチでどのように機能するかにより、to、from、subject などの値が、スイッチの外で宣言したにもかかわらず、スイッチの外では認識されないことです (おそらく間違っていますか?)。
私は .NET の初心者なので、この種のことをどのように行うべきかについてのアドバイスをいただければ幸いです。