使用C#和GoDaddy通过smtp.secureserver.net发送电子邮件

时间:2011-09-29 03:21:44

标签: c# asp.net windows vb.net

这让我绝对疯狂。我试图通过GoDaddy服务器(smtp.secureserver.net)用C#编写的Web服务发送电子邮件,但由于某种原因它无法正常工作。这是我的代码:

public static void SendMessage(string mailFrom, string mailFromDisplayName, string[] mailTo, string[] mailCc, string subject, string body)
{
    try
    {
        using (SmtpClient client = new SmtpClient("smtpout.secureserver.net"))
        {
            client.Credentials = new NetworkCredential("myemail@mydomain.com", "mypassword");
            client.EnableSsl = true;

            //client.Credentials = CredentialCache.DefaultNetworkCredentials;
            //client.DeliveryMethod = SmtpDeliveryMethod.Network;

            string to = mailTo != null ? string.Join(",", mailTo) : null;
            string cc = mailCc != null ? string.Join(",", mailCc) : null;

            MailMessage mail = new MailMessage();
            mail.From = new MailAddress(mailFrom, mailFromDisplayName);
            mail.To.Add(to);

            if (cc != null)
            {
                mail.CC.Add(cc);
            }

            mail.Subject = subject;
            mail.Body = body.Replace(Environment.NewLine, "<BR>");
            mail.IsBodyHtml = true;

            client.Send(mail);
        }
    }
    catch (Exception ex)
    {
        // exception handling
    }
}


string[] mailTo = { "mytestaddress@gmail.com" };
SendMessage("myemail@mydomain.com", "Test Email", mailTo, null, "Secure Server Test", "Testing... Sent at: " + DateTime.Now);

2 个答案:

答案 0 :(得分:3)

GOT IT !!!需要删除“client.EnableSsl = true;”行因为godaddy不接受安全连接。

答案 1 :(得分:0)

我有类似的问题。在我的情况下,设置客户端对象的.Port公共属性的值是问题。

现在,我根本没有设置该值,即使有附件,电子邮件也很快到达。