SMTP server does not respond to `SmtpClient` connection attempt. How do I fix this?
本问题已经有最佳答案,请猛点这里访问。
我试图编写用.NET发送电子邮件的代码。这就是我目前为止所拥有的:
1 2 3 4 5 6 | MailMessage mail = new MailMessage("[email protected]", address); mail.Subject = subject; mail.Body = body; client.Host ="smtp.gmail.com"; client.Port = 587; client.Send(e); |
问题是我得到以下异常:
Unhandled Exception:
System.Net.Mail.SmtpException : Failure sending mail.
System.Net.WebException : Unable to connect to the remote server.
System.Net.Sockets.SocketException : A connection attempt failed because the connected party
did not properly respond after a period of time, or established connection failed because connected host has failed to respond173.194.66.109:587 号
试试这个:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 using System.Net;
using System.Net.Mail;
// ...
MailAddress maFrom = new MailAddress("","<display_name>");
MailAddress maTo = new MailAddress("","<display_name>");
const string sPassword ="<password>";
const string sSubject ="<subject>";
const string sBody ="<body>";
new SmtpClient
{
Host ="smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(maFrom.Address, sPassword)
}.Send(new MailMessage(maFrom, maTo) { Subject = sSubject, Body = sBody });