Это код, который я делал, и я только что проверил, чтобы проверить, работает ли он, но с сообщением об ошибке, указывающим «System.FormatException»
Я не могу понять, что у меня такая проблема.Пожалуйста, помогите
public Mail(string sendMail)
{
this.sendAddress = new MailAddress(sendMail); // exception here
}
public void SetToAddress(string toMail)
{
this.toAddress = new MailAddress(toMail);
}
public string SendEmail(string subject, string body)
{
SmtpClient smtp = null;
MailMessage message = null;
try
{
smtp = new SmtpClient
{
Host = "smtp.gmail.com",
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
Credentials = new NetworkCredential(sendAddress.Address, sendPassword),
Timeout = 20000
};
message = new MailMessage(sendAddress, toAddress)
{
Subject = subject,
Body = body
};
smtp.Send(message);
return "send mail ok";
}
catch (Exception e)
{
return "send mail fail";
}
finally
{
if (smtp != null) { smtp.Dispose(); }
if (message != null) { message.Dispose(); }
}
}
}
namespace WindowsFormsApp12
{
public partial class Form1 : Form
{
Mail mail = new Mail("email address type");
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string toAddress = this.textBox2.Text;
string subject = this.textBox1.Text;
string body = this.textBox3.Text;
if (toAddress == "")
{
MessageBox.Show("type in email address");
}
if (subject == "")
{
MessageBox.Show("type in email title");
}
if (body == "")
{
MessageBox.Show("type in email contents");
}
mail.SetToAddress(toAddress);
MessageBox.Show(mail.SendEmail(subject, body));
}
}
}