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