我正在尝试通过 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