C#Mail功能未连接到远程服务器

时间:2015-02-25 07:27:58

标签: c# email

我正在开发一个需要具有发送电子邮件功能的程序。我有一个简单的邮件功能设置但是我从来没有真正钻研过邮件方面的东西,我不确定我是否使用了正确的设置。

我确定我在使用SMTP做错了,我已将MailMessage主机设置为我用于从outlook发送电子邮件的外发邮件服务器(电子邮件帐户托管在共享虚拟主机上,因此我使用其提供的主机名功能)以及我通常使用的登录凭据。当我尝试发送测试邮件时,它会抛出无法连接到远程服务器的异常。我在计算机上安装了WAMPSERVER,我试图运行它,我知道它有某种SMTP功能吗?我应该使用这个还是没有理由不能使用共享虚拟主机SMTP作为主机?请参阅下面的代码 -

public void EmailTracking()
    {

        string to = "johnsmith@xxxxxxxxxxxxx.com.au";
        string body = "this is some text for body";
        string subject = "subject line";
        string fromAddress = "kelvie@xxxxxxxxxx.com.au";
        string fromDisplay = "Kelvie";
        string credentialUser = "removed";
        string credentialPassword = "removed";
        string host = "removed";

        MailMessage mail = new MailMessage();
        mail.Body = body;
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress(to));
        mail.From = new MailAddress(fromAddress, fromDisplay, Encoding.UTF8);
        mail.Subject = subject;
        mail.SubjectEncoding = Encoding.UTF8;
        mail.Priority = MailPriority.Normal;

        SmtpClient smtp = new SmtpClient();
        smtp.Credentials = new System.Net.NetworkCredential(credentialUser, credentialPassword);
        smtp.Host = host;
        smtp.Send(mail);  //fails here with unable to connect to remote server
    }

0 个答案:

没有答案