Gopinath Guru
Gopinath Guru

Reputation: 77

How to send email using 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("[email protected]);
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);

When I run this code, I get this error:

The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.

Where have I gone wrong?

Upvotes: 1

Views: 2571

Answers (2)

John Moore
John Moore

Reputation: 1331

Try setting EnableSSL to tru and port number like shown below.

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);

If this doesn’t work here is a fully functional example I used many times

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); 
}

Upvotes: 0

JK84
JK84

Reputation: 355

I see your are using the dotnetnuke tag. Why don't you use the dotnetnuke.service.mail?

This is the dontnetuke assembly for sending mail.

Under the Host settings tab Advanced Setting you can add the SMTP server you want to use.

Then it's easy just use i.e. the next class:

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

Upvotes: 1

Related Questions