Hmailserver C# SMTP
Asked Answered
T

5

8

I'm just trying to get my hmailserver to send mail from my C# program. The part that's killing me is the SSL part.

I originally got this error: The SMTP server requires a secure connection or the client was not authenticated. The server response was: SMTP authentication is required.

So I added: smtp.EnableSsl = true; and now I get Server does not support secure connections.

Here is my code, this is driving me nuts. Do I have to create my own SSL or is there a way to disable SSL on hmailserver side?

MailMessage mail = new MailMessage("[email protected]", "com", "NEW Item", emailBody);
SmtpClient smtp = new SmtpClient("1.1.1.250");
smtp.Port = 25;
NetworkCredential login = new NetworkCredential("[email protected]", "dg");
smtp.Credentials = login;
smtp.EnableSsl = true;
smtp.UseDefaultCredentials = false;
smtp.Send(mail);
Toadstool answered 11/2, 2013 at 0:34 Comment(2)
Maybe playing with hmailserver's options? I use hmailserver since years without ssl from my c# programs.Crispy
@UweKeim okay which options? I dont want SSL I just want to send basic mail to another accountToadstool
T
7

Ahh okay what you have to do is in HMailServer go to advanced- ip ranges. Create a new IP range for example if you 192.168.1.2, you have to make the range 192.168.1.1-192.168.1.3, then at bottom uncheck all the required smtp authentication boxes.

Annoying...

Toadstool answered 11/2, 2013 at 0:59 Comment(1)
I know it's an old thread, but for future googlers I just want to add that you also need to take care of the priority of the IP-ranges. If multiple ranges match, the one with highest priority is used.Nipa
K
1

i was having this issue, what i did was used localhost ip and EnableSsl to false

SmtpClient smtpClient = new SmtpClient();
            smtpClient.Host = "127.0.0.1";
            smtpClient.Credentials = new NetworkCredential("[email protected]", "pass123");
            smtpClient.EnableSsl = false;

         // then your other statements like: from, to, body, to send mail

this guide will help you setup custom NetworkCredentials in HMailServer as used above, hope helps someone.

Kowatch answered 1/3, 2015 at 10:40 Comment(0)
H
0

To enable secure connection to send email throught your email provider, you have to change the port number.

       MailMessage mail = new MailMessage("[email protected]", "com", "NEW Item", emailBody);
        SmtpClient smtp = new SmtpClient("1.1.1.250");
        //smtp.Port =25;
        smtp.Port =587;
        NetworkCredential login = new NetworkCredential("[email protected]", "dg");
        smtp.Credentials = login;
        smtp.EnableSsl = true;
        smtp.UseDefaultCredentials = false;
        smtp.Send(mail);   
Hyperspace answered 11/2, 2013 at 1:12 Comment(0)
U
0

I have stumbled on this question when trying to configure hMailServer to work to e-mail sending from C#. I have tried the following:

  • C# SmtpClient - does not work with implicit SSL - see this question and answers
  • AegisImplicitMail from here - could not make it work with UTF-8 strings (I have diacritics in my strings)
  • MailKit from here - very powerful and mature, no problems using it

I aimed for the following:

  • decent security
  • being able to send e-mails to mainstream e-mail providers (e.g. Google, Yahoo) and reach Inbox
  • being able to receive e-mails from mainstream e-mail providers

C# code

    public void MailKitSend(string senderEmail, string senderName, string subject, string bodyText, string receivers, string receiversCc)
    {
        // no receivers, no e-mail is sent
        if (string.IsNullOrEmpty(receivers))
            return;

        var msg = new MimeMessage();
        msg.From.Add(new MailboxAddress(Encoding.UTF8, senderName, senderEmail));
        msg.Subject = subject;
        var bb = new BodyBuilder {HtmlBody = bodyText};
        msg.Body = bb.ToMessageBody();

        IList<string> receiversEmails = receivers.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
        foreach (string receiver in receiversEmails)
            msg.To.Add(new MailboxAddress(Encoding.UTF8, "", receiver));

        if (!string.IsNullOrEmpty(receiversCc))
        {
            IList<string> receiversEmailsCc = receiversCc.Split(new[] { ";" }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (string receiverCc in receiversEmailsCc)
                msg.Cc.Add(new MailboxAddress(Encoding.UTF8, "", receiverCc));
        }

        try
        {
            var sc = new MailKit.Net.Smtp.SmtpClient();
            if (!string.IsNullOrWhiteSpace(SmtpUser) && !string.IsNullOrWhiteSpace(SmtpPassword))
            {
                sc.Connect(SmtpServer, 465);
                sc.Authenticate(SmtpUser, SmtpPassword);
            }

            sc.Send(msg);
            sc.Disconnect(true);
        }
        catch (Exception exc)
        {
            string err = $"Error sending e-mail from {senderEmail} ({senderName}) to {receivers}: {exc}";
            throw new ApplicationException(err);
        }
    }

hMailServer configuration

1) Opened ports - 25, 143, 465, 995 are opened to ensure that you can send and receive e-mail

2) TCP/IP ports configuration

SMTP / 0.0.0.0 / port 25 / no security (allow receiving start process)
SMTP / 0.0.0.0 / port 465 / SSL/TLS security (must define a SSL certificate)
POP3 / 0.0.0.0 / port 995 / SSL/TLS security (use the same SSL certificate)

3) pre C# testing

Run Diagnostics from hMailServer Administrator

Use an e-mail client that allows manual configuration of various settings such as ports for each protocol, security. I have used Thunderbird. Include sending of e-mails to external providers and receiving e-mails from them (I have tried with Gmail).

I made no changes in IP ranges and left the implicit ones (My computer and the Internet).

Umbria answered 24/12, 2016 at 7:54 Comment(0)
M
0

Although it's 7 years passed since the accepted answer was posted - I also upvoted it in the beginning - I want to emphasize that the suggested solution disables the whole authentication process which is unnecessary. The problem is the line with :

smtp.UseDefaultCredentials = false;

Just remove that line and it should work.

I post here the working solution for me (note that I'm not using SSL):

    MailMessage mail = new MailMessage("[email protected]", "[email protected]");
    SmtpClient client = new SmtpClient();
    client.Credentials = new NetworkCredential("[email protected]", "test");
    client.Port = 25;
    client.EnableSsl = false;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.UseDefaultCredentials = false;
    client.Host = "...IPv4 Address from ipconfig...";
    mail.Subject = "this is a test email.";
    mail.Body = "this is my test email body";
    client.Send(mail);
Marabout answered 15/3, 2018 at 11:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.