Question Send emails with attachments

jonny22

Member
Joined
May 14, 2022
Messages
23
Programming Experience
1-3
Hello everyone, I'm trying to send emails with attachments with C #, I saw that there is a special "System.Net.Mail" method, I tried to ascribe the code myself and also to use code examples that I found on the net but i can't get them to work, debugging with visual studio always returns several errors, which i am unable to fix, can someone point me to a working code example? Or a well done tutorial to be able to write working code?
 
Basically after entering this:

telnet <smtp-serer> <port>
EHLO <server>
AUTH LOGIN


I'm not getting something like:

C:\temp>telnet smtp.email.example.com 25

Line 1 220 smtp.email.example.com Microsoft ESMTP MAIL Service, Version: 8.5.9600.1
Line 2 6384 ready at Fri, 10 Mar 2017 03:10:48 -0500
Line 3 EHLO smtp.email.example.com
Line 4 250-smtp.email.example.com Hello
Line 5 250-AUTH=LOGIN
Line 6 250-AUTH LOGIN
Line 7 250-TURN
Line 8 250-SIZE 2097152
Line 9 250-ETRN
Line 10 250-PIPELINING
Line 11 250-DSN
Line 12 250-ENHANCEDSTATUSCODES
Line 13 250-8bitmime
Line 14 250-BINARYMIME
Line 15 250-CHUNKING
Line 16 250-VRFY
Line 17 250 OK
Line 18 AUTH LOGIN
Line 19 334 VXNlcm5hbWU6


But only:

220 smtpserver.com ESMTP ready
250-smtpservername.com
250 AUTH PLAIN LOGIN

However, it seems that communication is taking place, as there is:

220 smtpserver.com ESMTP ready
 
Actually, that's a good sign that shows that port 25 works.

Try your C# code again using port 25. Keep it simple. Use a real from address. (Don't get fancy with trying to use a no-reply address.) Use a real to address. Just a subject line and a plain text body. No attachments.

If that succeeds then try adding attachments.
 
I tried, port 25, SSL False, deleted the code for sending attachment, compiled and ran, here is what is reported.
 

Attachments

  • Immagine1.jpg
    Immagine1.jpg
    141.1 KB · Views: 8
What antivirus are you using? Can you disable it for a while? If you can't disable it, can you tell it to trust your application?
 
I suggest installing SMTP4DEV (or running a container with it). Try sending email to that local test server without authentication. When that is working, enable authentication on it. Once that it working, then try using with that mail host that is failing.

Testing with the local server first will help you iron out any issues that might be related to your code. Switching to the actual mail host will then just be a configuration change of server name, port, and credentials. In theory, it should just work.

If that doesn't work, then it is time to fire up WireShark or your favorite network sniffer. Unfortunately, I am out of my depth in guiding you what to look for in the packets to determine why it is closing the connection.
 
First of all, thank you for the help you are giving me!
I installed the local server you recommended.
I see the port on which it is listening but I did not understand what the name of this server is, which I then have to put in the C # code to send the email, what is the exact name of the SMTP4DEV local server?
 
It will be your machine name or localhost.
 
Either one should work. You can also use your machine name as a third option.
 
Back
Top Bottom