Reputation: 11
I have a .NET Core project from which I need to send e-mails.
I use the Gmail server, I enabled the password per application and this is my code:
using (var message = new MailMessage())
{
message.From = new MailAddress(mailSender);
message.To.Add(toAddress);
message.Subject = subject;
message.Body = body;
using (var smtpClient = new SmtpClient(smtp))
{
smtpClient.Port = 587;
smtpClient.Credentials = new NetworkCredential(mailSender, passwordSender);
smtpClient.EnableSsl = true;
var pdfAttachment = new Attachment(new MemoryStream(pdfBytes), fileName);
pdfAttachment.ContentType = new System.Net.Mime.ContentType("application/pdf");
message.Attachments.Add(pdfAttachment);
await smtpClient.SendMailAsync(message);
}
}
Locally everything works perfectly.
On the server, emails are never sent without obtaining useful information about them.
I tried to see the status of port 587 with this command from PowerShell:
Test-NetConnection -ComputerName smtp.gmail.com -Port 587
And I get:
AVVISO: TCP connect to (IP : 587) failed
ComputerName : smtp.gmail.com
RemoteAddress : IP
RemotePort : 587
InterfaceAlias : Ethernet 3
SourceAddress : IP
PingSucceeded : True
PingReplyDetails (RTT) : 49 ms
TcpTestSucceeded : False
Unlike the 465 which passes the test.
However, by changing the code to port 465, sending emails still does not work.
Thanks in advance to anyone who will try to help me
Upvotes: 0
Views: 71