我的主要目的是简单地向自己发送带有附件的邮件。
目前,我正在使用 smtp 客户端,但它需要大约 5 分钟并且非常不可靠。
这是我当前的代码:
private SmtpClient smtp;
private void button2_Click(object sender, EventArgs e)
{
if (smtp != null)
return;
string username = "", password = "";
if (checkBox2.Checked)
username = textBox7.Text;
if (checkBox3.Checked)
password = textBox8.Text;
if (username != "" || password != "")
{
//save info
}
label17.Text = "In Progress...";
MailAddress me = new MailAddress(textBox7.Text, "Me");
smtp = new SmtpClient("smtp.gmail.com", 465);
smtp.Timeout = 300000;
smtp.EnableSsl = true;
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential(me.Address, textBox8.Text);
MailMessage message = new MailMessage(me, me);
message.Subject = page.ActiveDocument.Name;
message.Body = "r u mad tho?";
string path = "path to file";
message.Attachments.Add(new Attachment(path));
smtp.SendCompleted += Completed;
smtp.SendAsync(message, label17.Text);
}
private void Completed(object sender, EventArgs e)
{
label17.Text = "Done!";
smtp.Dispose();
smtp = null;
}
字符串路径值可以忽略不计;我确定它是正确的路径,用户名/密码也是如此。