简体   繁体   中英

Send e-mail using c#

Can't understand why this code is not working. error message is The operation has timed out

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();

You may be getting a Null Reference Exception when adding the "To" email address. You may need to initialize the "To" MailAddressCollection.

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

aslo there is some useful info here on stackoverflow about sending SmtpClient.EnableSSL = true messages using certain ports.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM