使用c#发送电子邮件

时间:2014-11-20 05:40:57

标签: c# smtp

无法理解为什么此代码无效。错误消息操作已超时

SmtpClient client = new SmtpClient("smtp.ipage.com", 465);
client.EnableSsl = true;

client.Timeout = 50000;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(email, password);
MailMessage msg = new MailMessage();
msg.To.Add(txtTo.Text.Trim());
msg.From = new MailAddress(email);
msg.Subject = txtSubject.Text.Trim();
msg.Body = txtMsg.Text.Trim();
//txtAttachment.Text = fileName.ToString();
Attachment attachment = new Attachment(txtAttachment.Text);
msg.Attachments.Add(attachment);
client.Send(msg);
MessageBox.Show("Successfuly sent Message.");
clear();

1 个答案:

答案 0 :(得分:0)

添加“收件人”电子邮件地址时,您可能会收到Null引用异常。您可能需要初始化“To”MailAddressCollection。

msg.To = new MailAddressCollection();
msg.To.Add(txtTo.Text.Trim());

aslo在stackoverflow上有一些有用的信息here关于使用某些端口发送SmtpClient.EnableSSL = true消息。