0

Sending an email seems to be a common problem :-) But the other Q&As do not fit my situation, or I am stupid :D

My class:

    public class Sender
    {

        private Setting _setting;
        public Sender(Setting setting)
        {
            _setting = setting;
        }

        public void Send(string receiverAddress, string message) => Send(new MailAddress(receiverAddress), message);
        public void Send(MailAddress receiverAddress, string message)
        {
            using(MailMessage mail = new MailMessage())
            {
                MailAddress senderAddress = new MailAddress(_setting.SenderAddress);

                mail.From = senderAddress;
                mail.To.Add(receiverAddress);

                mail.Subject = "Integration Test";
                mail.SubjectEncoding = System.Text.Encoding.UTF8;
                mail.Body = "Hello, World!";
                mail.BodyEncoding = System.Text.Encoding.UTF8;

                using(SmtpClient smtpClient = new SmtpClient(_setting.SmtpServer, _setting.SmtpPort)){
                    smtpClient.EnableSsl = true;
                    smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
                    // smtpClient.UseDefaultCredentials= false;
                    smtpClient.Credentials = new NetworkCredential(_setting.SenderAddress, _setting.Password);
                    smtpClient.Timeout = 20000;
                    smtpClient.Send(mail);
                }
            }
        }
    }

My Test

      [Test]
        public void SendEmailGmail()
        {
            Setting setting = new Setting();
            setting.SmtpServer = "smtp.google.com";
            setting.SmtpPort = 587; // also tried 465
            setting.SenderAddress = "my@googlemail.com";
            setting.Username = "Display Name";
            setting.Password = "GoogleGeneratedAppPassword";

            Sender sender = new Sender(setting);

            sender.Send("i@dont.tell", "Hello, World!");

        }

When I execute the test, the test doesn't finish. It even exceeds the timeout of 20 seconds.

I have no idea what I have done wrong; please help me I am thankfully for your time :-)

Dear peni4142

peni4142
  • 426
  • 1
  • 7
  • 26
  • 1
    Google has it's own security process, you should consider a different email provider if possible. Otherwise you need to make account changes ~ https://stackoverflow.com/a/47847415/1462295 or https://www.emailarchitect.net/easendmail/ex/c/4.aspx – BurnsBA Sep 25 '22 at 13:32
  • I don't think Google mail will act as a `relay` which is what it is when you send through your server. – Poul Bak Sep 25 '22 at 14:19
  • @BurnsBA yeah I already set up Gmail for that(Generated App Password). I have also tried for Strato, the same Code, But it doesn’t work. – peni4142 Sep 25 '22 at 15:04

1 Answers1

0

A way I made those king of code work with Gmail was to install postfix smtp server localy and replay your test Do not use Gmail SMTP directly

In postfix, set in /etc/postfix/main.cf :

relayhost = [smtp.google.fr]

And restart postfix :

systemctl restart postfix

In your google account, allow the public IP of your postfix server to login without credential on smtp.google.com for your gmail managed domain (your sender domain)

Next, change your code to use the local IP of your Postfix server in replacement of smtp.google.fr

setting.SmtpServer = "local.ip.address.of.your.postfix.server";
setting.SmtpPort = 25;

Postfix will have no troubles to deal with SMTP transactions, TLS, and whatever security google ask when connecting to smtp.google.fr

nbanba
  • 51
  • 4