无法发送电子邮件

本文关键字:电子邮件 | 更新日期: 2023-09-27 17:58:52

string from = "myemail@gmail.com";
string to = "other.mail.123@gmail.com";
System.Net.Mail.MailMessage mail = new System.Net.Mail.MailMessage();
mail.To.Add(to);
mail.From = new System.Net.Mail.MailAddress(from, "One Ghost", System.Text.Encoding.UTF8);
mail.Subject = "This is a test mail";
mail.SubjectEncoding = System.Text.Encoding.UTF8;
mail.Body = "This is Email Body Text";
mail.BodyEncoding = System.Text.Encoding.UTF8;
mail.IsBodyHtml = true;
mail.Priority = System.Net.Mail.MailPriority.High;
System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
client.Credentials = new System.Net.NetworkCredential(from, "XXXXX");
client.Port = 587; 
client.Host = "smtp.gmail.com";
client.EnableSsl = true; 
try
{
    client.Send(mail);
}
catch (Exception ex)
{
    Exception ex2 = ex;
    string errorMessage = string.Empty;
    HttpContext.Current.Response.Write("errroorr " + ex.Message.ToString());
}

它在client.send((方法处引发异常。消息发送失败。可能出了什么问题内部例外是这个

内部异常表示System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: No connection could be made because the target machine actively refused it 74.125.141.109:587 at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress) at System.Net.Sockets.Socket.InternalConnect(EndPoint remoteEP) at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Int32 timeout, Exception& exception) --- End of inner exception stack trace --- at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6, Int32 timeout) at System.Net.PooledStream.Activate(Object owningObject, Boolean async, Int32 timeout, GeneralAsyncDelegate asyncCallback) at System.Net.PooledStream.Activate(Object owningObject, GeneralAsyncDelegate asyncCallback) at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout) at System.Net.Mail.SmtpConnection.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpTransport.GetConnection(String host, Int32 port) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)

无法发送电子邮件

尝试添加这两个设置

client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;

还要检查您的服务器上是否有阻止发送电子邮件请求的防病毒软件

你可以这样使用它在下面

public static void SendMail(string Message, string Subject)
    {
        bool retVal;
        SmtpClient smtpClient = new SmtpClient();
        MailMessage message = new MailMessage();
        MailAddress fromAddres = new MailAddress(FromMail, "Test");
        message.From = fromAddres;
        // To address collection of MailAddress
        message.To.Add(ToMail);
        message.Subject = Subject;
        smtpClient.Host = HotName;
        smtpClient.UseDefaultCredentials = true;
        smtpClient.Credentials = new System.Net.NetworkCredential(SmtpUser, SmtpPassword);
        message.IsBodyHtml = true;
        // Message body content
        message.Body = Message;    
        smtpClient.Send(message);
        retVal = true;
        message.Dispose();
    }

我想这会对你有所帮助。。。。。。。。

很抱歉把它作为答案发布,我打算把它作为对原始帖子的回复。

SMTP将需要凭据,我找不到提供的凭据。尝试telnet连接到服务器。如果端口是可访问的,并且您具有SMTP凭据,则可以从命令行本身发送电子邮件。

试试这个,让我知道这对你是否有效。