我无法使用以下代码连接到前端邮件服务器。我收到消息"无法连接到远程服务器"。我在本地计算机上使用C#运行程序。
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient("smtp.frontier.com");
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx@frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(thisimage);
mail.Attachments.Add(attachment);
SmtpServer.Port = 25;
SmtpServer.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
SmtpServer.EnableSsl = true;
SmtpServer.Send(mail);
MessageBox.Show("Mail sent");
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
有人能说出我是否有正确的Frontier邮件参数?我知道他们使用雅虎,但我尝试过也没有成功。是不是可以从我的本地机器运行邮件服务器?任何帮助表示赞赏。
答案 0 :(得分:0)
尝试删除此代码SmtpServer.EnableSsl = true;
答案 1 :(得分:0)
您的ISP是否阻止SMTP流量? (这通常是非商业账户的情况)。
如果没有......重写你的代码:
try
{
using (var attachment = new Attachment(thisimage))
using (var mail = new MailMessage())
{
mail.From = new MailAddress(emailaddress);
mail.To.Add("xxxx@frontier.com");
mail.Subject = thistitle;
mail.Body = thisdescription;
mail.Attachments.Add(attachment);
using (var client = new SmtpClient("smtp.frontier.com"))
{
client.Port = 25;
client.Credentials = new System.Net.NetworkCredential("username", "xxxxxxx");
client.EnableSsl = true;
client.Send(mail);
}
}
MessageBox.Show("Mail sent");
}
catch (SmtpException ex)
{
// go read through https://msdn.microsoft.com/en-us/library/swas0fwc(v=vs.110).aspx
// go read through https://msdn.microsoft.com/en-us/library/system.net.mail.smtpexception(v=vs.110).aspx
}
#if DEBUG
catch (Exception ex)
{
MessageBox.Show(ex.ToString(), "Email Error Message");
}
#endif
}
...并在调试器中运行它并查看SmtpException报告的内容。您可能无法连接的原因有很多。
答案 2 :(得分:0)
我无法发表评论所以我会输入我的评论作为答案。您是否可以使用ImapClient而不是SmtpClient?使用Imap,您可以执行一些身份验证过程。可能是问题,它只是看起来你正在登录。对于Imap,我这样做:
using (var clientTest = new ImapClient())
{
clientTest.Connect("xxxx@frontier.com");
clientTest.AuthenticationMechanisms.Remove("XOAUTH");
clientTest.Authenticate(eMail, pw);
bIsConnected = clientTest.IsConnected;
if (bIsConnected == true)
{
/// Insert Code here
}
}