2
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)

4

3 回答 3

0

尝试添加这两个设置

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

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

于 2013-02-13T10:24:57.833 回答
0

你可以像这样使用它在下面

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

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

于 2013-02-13T10:46:22.420 回答
0

很抱歉将其发布为答案,我打算将其用于回复原始帖子。

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

试试这个,让我知道这是否适合你。

于 2013-02-13T10:46:56.177 回答