如何使用dotnetnuke发送电子邮件?

时间:2013-09-20 04:45:21

标签: asp.net c#-4.0 dotnetnuke

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身份验证。

我哪里出错?

2 个答案:

答案 0 :(得分:1)

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

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

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

然后就可以轻松使用,即下一堂课:

 SendEmail(string fromAddress, string senderAddress, string toAddress, string subject, string body, List<System.Net.Mail.Attachment> attachments);

答案 1 :(得分: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); 
}