首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在C#中发送电子邮件远程主机没有响应

问在C#中发送电子邮件远程主机没有响应
EN

Stack Overflow用户
提问于 2013-05-23 16:29:57
回答 2查看 2.3K关注 0票数 0

我正在尝试通过C#创建一个电子邮件客户端,但我一直有问题,我不知道如何继续。当我使用gmail和yahoo作为smtp主机时,我没有遇到任何问题。当我使用google向公司邮箱发送邮件时,我也没有遇到问题。

每当我使用公司的电子邮件地址发送电子邮件时,问题就会出现。您可以在浏览器上填写电子邮件的地址。

我也没有使用独立于公司访问互联网的私人互联网连接。

下面是我的代码:

代码语言:javascript
复制
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Mail;
using System.Net;

namespace email
{
    public partial class Form1 : Form
    {
        SmtpClient client = new SmtpClient();
        MailMessage message = new MailMessage();

        public Form1()
        {
            InitializeComponent();
        }

        private void send_Click(object sender, EventArgs e)
        {

            System.Net.NetworkCredential networkcreds = new System.Net.NetworkCredential("username", "password");

            try
            {
                //setup for smtp host
                client.Host = "mail.company.com";
                client.Port = 25;
                client.UseDefaultCredentials = false;
                client.Credentials = networkcreds;
                client.EnableSsl = true;

                //convert strings to mailaddress
                MailAddress to = new MailAddress("username@company.com");
                MailAddress froms = new MailAddress("username@company.com");

                //set up message settings
                message.Subject = "TEST SUBJECT";
                message.Body = "TEST BODY";
                message.To.Add(to);
                message.From = froms;

                client.Send(message);
                MessageBox.Show("Message Sent");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
                MessageBox.Show("Message Not sent");
            }
        }
    }
}

下面是我在try catch中发现的错误:

代码语言:javascript
复制
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 respond 203.82.36.171:25
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, 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)
   at System.Net.PooledStream.Activate(Object owningObject, Boolean async, 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(ServicePoint servicePoint)
   at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
   at System.Net.Mail.SmtpClient.GetConnection()
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   --- End of inner exception stack trace ---
   at System.Net.Mail.SmtpClient.Send(MailMessage message)
   at email.Form1.send_Click(Object sender, EventArgs e) in C:\Users\Jake Andrew\documents\visual studio 2010\Projects\email\email\Form1.cs:line 50
EN

回答 2

Stack Overflow用户

发布于 2013-05-23 16:34:37

尝试ping邮件服务器:

代码语言:javascript
复制
ping 203.82.36.171

如果这样做有效,还有其他几个选项需要检查。例如,我的公司不允许每个客户使用SMTP协议发送邮件-询问您是否有权从您的机器通过SMTP发送邮件。

票数 0
EN

Stack Overflow用户

发布于 2013-05-23 17:00:36

尝试使用另一个端口。

代码语言:javascript
复制
client.Port = 465;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16709122

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档