使用Visual Studio 2010 c#和外部SMTP服务器发送电子邮件
我有一个VS.STUDIO 2010 C#项目,我试图使用SMTP远程服务器发送电子邮件 我总是有一个超时并且没有发送电子邮件,查看SMTP日志没有尝试从我的IP访问,这是我的代码:使用Visual Studio 2010 c#和外部SMTP服务器发送电子邮件,c#,windows,forms,visual-studio-2010,C#,Windows,Forms,Visual Studio 2010,我有一个VS.STUDIO 2010 C#项目,我试图使用SMTP远程服务器发送电子邮件 我总是有一个超时并且没有发送电子邮件,查看SMTP日志没有尝试从我的IP访问,这是我的代码: using System.IO; using System.Net; using System.Net.Mail; private void EmailButton_Click(object sender, EventArgs e) { if (!String.IsNullOrEmpty(
using System.IO;
using System.Net;
using System.Net.Mail;
private void EmailButton_Click(object sender, EventArgs e)
{
if (!String.IsNullOrEmpty(CompanySelected))
{
MessageBox.Show("Invio email in corso per: " + CompanySelected, "Avviso", MessageBoxButtons.OK, MessageBoxIcon.Information);
SettingsManager SettMngr = new SettingsManager();
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient
{
Host = SettMngr.SmtpHost.Trim(),
Port = Convert.ToInt16(SettMngr.SmtpPort),
DeliveryMethod = SmtpDeliveryMethod.Network,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential(SettMngr.SmtpUser, SettMngr.SmtpPassword),
Timeout = 50000
};
mail.From = new MailAddress("server@ashnet.it");
mail.To.Add("info@ashnet.it");
mail.Subject = "Test Mail - SERVICE 2020";
mail.Body = "mail with attachment";
string file = "c:\\service_tmp\\q_attuali Natana_Jolly.xlsx";
mail.Attachments.Add(new Attachment(file));
try
{
SmtpServer.Send(mail);
}
catch (SmtpFailedRecipientsException ex)
{
for (int i = 0; i < ex.InnerExceptions.Length; i++)
{
SmtpStatusCode status = ex.InnerExceptions[i].StatusCode;
if (status == SmtpStatusCode.MailboxBusy ||
status == SmtpStatusCode.MailboxUnavailable)
{
Console.WriteLine("Delivery failed - retrying in 5 seconds.");
System.Threading.Thread.Sleep(5000);
SmtpServer.Send(mail);
}
else
{
Console.WriteLine("Failed to deliver message to {0}",
ex.InnerExceptions[i].FailedRecipient);
}
}
}
catch (Exception ex)
{
Console.WriteLine("Exception caught in RetryIfBusy(): {0}",
ex.ToString());
}
}
使用System.IO;
Net系统;
使用System.Net.Mail;
私有无效电子邮件按钮\u单击(对象发件人,事件参数e)
{
如果(!String.IsNullOrEmpty(已选择公司))
{
MessageBox.Show(“corso中的Invio电子邮件per:+选中公司,“Avviso”,MessageBoxButtons.OK,MessageBoxIcon.Information);
SettingsManager SettMngr=新的SettingsManager();
MailMessage mail=新的MailMessage();
SmtpClient SmtpServer=新SmtpClient
{
Host=SettMngr.SmtpHost.Trim(),
端口=转换为16(设置TMNGR.SmtpPort),
DeliveryMethod=SmtpDeliveryMethod.Network,
EnableSsl=true,
UseDefaultCredentials=false,
凭据=新系统.Net.NetworkCredential(SettMngr.SmtpUser,SettMngr.SmtpPassword),
超时=50000
};
mail.From=新邮件地址(“server@ashnet.it");
mail.To.Add(“info@ashnet.it");
mail.Subject=“测试邮件-服务2020”;
mail.Body=“带附件的邮件”;
string file=“c:\\service\u tmp\\q\u attuali Natana\u Jolly.xlsx”;
mail.Attachments.Add(新附件(文件));
尝试
{
发送(邮件);
}
捕获(SmtpFailedRecipientsException ex)
{
for(int i=0;i
我运行的代码和所有它的确定,直到发送(邮件),所有smtp参数都是正确的
发送(邮件)等待一段时间,然后退出并出现超时错误
同时,增加超时值并不能解决问题
谢谢检查邮件服务器网页中的SMTP设置。端口号可能错误。凭据和发件人地址需要使用相同的电子邮件帐户。此外,如果您位于具有outlook电子邮件服务器的公司网络中,SMTP将不适用于外部电子邮件服务器(如gmail)。因此,请从您的家用电脑上尝试。SMTP是我的服务呃,所有参数都是正确的:(当与SMTP服务器在同一台计算机上时,代码是否工作?是否有防火墙或病毒检查器阻止该端口?网络上是否有outlook服务器?当outlook服务器在网络上时,所有端口587都会通过防火墙转发到代理服务器,然后在发送到本地outlook服务器之前验证凭据。My VS app i我的SMTP服务器是一个公共服务器,我的1k用户都在其中。您的第一个响应是错误的。我说“检查邮件服务器网页的SMTP设置”。公共服务器是像gmail一样的电子邮件服务还是您是管理员?