私はこれに対する解決策を探していましたが、困惑しています。C#でメールクライアントを作っています。私は C# を使いこなすことができますが、構文に追いつくことがあります。
ここに私の問題があります。メールのユーザー入力に基づいて smtpclient 設定を割り当てようとしています。53 行目でエラーが発生します (割り当てられていないローカル変数の使用)。
smtpclient smm = new smtpclient(s, p);
これが私のコードです:
private void Send_Click(object sender, EventArgs e)
{
//Set the login info for the email
NetworkCredential nc = new NetworkCredential(Euser.Text, Epass.Text);
MailMessage msg = new MailMessage();
msg.To.Add(Toemail.Text);
msg.From = new MailAddress(Euser.Text);
msg.Subject = Subemail.Text;
msg.Body = body.Text;
string s;
int p;
if (Euser.Text.Contains("@gmail.com") == true)
{
s = "smtp.gmail.com";
p = 587;
}
if (Euser.Text.Contains("@yahoo.com") == true)
{
s = "smtp.mail.yahoo.com";
p = 995;
}
if (Euser.Text.Contains("@live.com") == true)
{
s = "smtp.live.com";
p = 587;
}
SmtpClient smm = new SmtpClient(s, p);
smm.Credentials = nc;
smm.EnableSsl = true;
try
{
smm.Send(msg);
MessageBox.Show("Emails Sent Successfully");
}
catch (Exception ex)
{
MessageBox.Show("There was an error sending your emails");
}
私は正確に何を間違っていますか?このためのメソッドを作成する必要がありますか? どんな助けでも大歓迎です。