0

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

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

我也没有使用与公司访问互联网分开的私人互联网连接

这是我的代码:

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 中发现的错误:

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
4

2 回答 2

0

尝试使用另一个端口。

client.Port = 465;
于 2013-05-23T09:00:36.487 回答
0

尝试 ping 邮件服务器:

ping 203.82.36.171

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

于 2013-05-23T08:34:37.420 回答