1
string smtpServer = "mail.occtbangalore.org";
string smtpAuthentication = (string)Globals.HostSettings["SMTPAuthentication"];
string smtpUsername = (string)Globals.HostSettings["SMTPUsername"]; 
string smtpPassword = (string)Globals.HostSettings["SMTPPassword"];

MailMessage mail = new MailMessage();

mail.From = new MailAddress("info@myhost.c);
mail.To.Add(txtEmail.Text.Trim());
mail.Subject = "OCCT BANGALORE";
string html = "<img src=\"cid:Logo\" />";
AlternateView av2 = AlternateView.CreateAlternateViewFromString(html,null,text/html");   
string logoFile = MapPath(PortalSettings.HomeDirectory + PortalSettings.LogoFile);
if (File.Exists(logoFile))
{
     LinkedResource linkedResource = new LinkedResource(logoFile);
     linkedResource.ContentId = "Logo";
     linkedResource.ContentType.Name = logoFile;
     linkedResource.ContentType.MediaType = "image/jpeg";
     av2.LinkedResources.Add(linkedResource);
}
mail.AlternateViews.Add(av2);
SmtpClient emailClient = new SmtpClient(smtpServer);
if (smtpAuthentication == "1")
{
    NetworkCredential SMTPUserInfo = new NetworkCredential(smtpUsername,smtpPassword);    
    emailClient.UseDefaultCredentials = false;
    emailClient.Credentials = SMTPUserInfo;
}
emailClient.Send(mail);

当我运行此代码时,我收到此错误:

SMTP 服务器需要安全连接或客户端未通过身份验证。服务器响应是:需要 SMTP 身份验证。

我哪里出错了?

4

2 回答 2

1

我看到您正在使用 dotnetnuke 标签。为什么不使用 dotnetnuke.service.mail?

这是用于发送邮件的 dontnetuke 程序集。

在主机设置选项卡高级设置下,您可以添加要使用的 SMTP 服务器。

然后很容易使用即下一个类:

 SendEmail(string fromAddress, string senderAddress, string toAddress, string subject, string body, List<System.Net.Mail.Attachment> attachments);
于 2013-09-20T10:34:12.490 回答
0

尝试将 EnableSSL 设置为 tru 和端口号,如下所示。

SmtpClient emailClient = new SmtpClient(smtpServer);
emailClient.EnableSsl = true;
emailClient.Port = 587
if (smtpAuthentication == "1")
{
      NetworkCredential SMTPUserInfo = new NetworkCredential(smtpUsername, smtpPassword);
      emailClient.UseDefaultCredentials = false;
      emailClient.Credentials = SMTPUserInfo;
}
emailClient.Send(mail);

如果这在这里不起作用,这是我多次使用的功能齐全的示例

private static void SendEmail(string to, string cc, string bcc, string subject, string body, bool isHtml)
{
    SmtpClient mailClient = new SmtpClient(Config.SmptSettings.Server);
    mailClient.Credentials = new NetworkCredential(Config.SmptSettings.UserName, Config.SmptSettings.Password);
    mailClient.Port = Config.SmptSettings.Port;                

    MailMessage message = new MailMessage();
    message.From = new MailAddress(Config.SUPPORT_EMAIL, Config.SUPPORT_EMAIL_NAME);

    message.To.Add(new MailAddress(to));

    if (!string.IsNullOrEmpty(cc))
    {
        message.CC.Add(cc);
    }

    if (!string.IsNullOrEmpty(bcc))
    {
        message.Bcc.Add(bcc);
    }

    message.Subject = subject;
    message.Body = body;
    message.IsBodyHtml = isHtml;

    mailClient.EnableSsl = Config.SmptSettings.SSL;
    mailClient.Send(message); 
}
于 2013-09-20T14:11:26.457 回答