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?
 
Show us your code. Post code as text in code tags. Also post the errors that you are getting.

As an aside that send mail method has always worked for me without issue, so I am wondering what is getting in your way.
 
I found that the sample code in documentation as a good starting point.

 
Below is the code used:

Send email with attachment with C#:
using System.Net.Mail;
using System.Net;
static void email_send()
{
    MailMessage mail = new MailMessage();
    SmtpClient SmtpServer = new SmtpClient("smtpservername");
    mail.From = new MailAddress("sendingemail");
    mail.To.Add("receivedemail");
    mail.Subject = "Test Mail";
    mail.Body = "mail with attachment";

    System.Net.Mail.Attachment attachment;
    attachment = new System.Net.Mail.Attachment("D:/attachment.txt");
    mail.Attachments.Add(attachment);

    SmtpServer.Port = 25;
    SmtpServer.Credentials = new System.Net.NetworkCredential("sendingemail", "sendingemailpassword");
    SmtpServer.EnableSsl = false;

    SmtpServer.Send(mail);

}
email_send();

;


Debugging indicates the following:
System.Net.Mail.SmtpException: 'Failure sending mail.'
Internal exception
IOException: Unable to read data from the transport connection: The connection was closed.
 
Last edited:
Some SMTP servers have "allow lists" and "block lists" of IP addresses from which they will accept connections from. (My company has "allow lists".) Have you successfully connected to that same server on port 25 using telnet?

Also a lot of modern SMTP server prefer connections over SSL and/or a custom port. (Most of the web/email hosting companies do this.) Some even require you to supply a certificate on top of passing in the correct credentials.

In all cases, have you contacted your email administrator to find out the details that you need to connect to the SMTP server?
 
When I registered for the smtp service I was given the credentials and they are the ones I entered in the code.
I tried with both SSl enabled and disabled and with ports 25, 465 and 587, but it doesn't work, the code I posted above, the one with the default credentials, should return me a message?
The console appears to me and nothing else.
 
should return me a message?
Only if a successful connection is made. The error you have above indicates that the connection is closed even before that can happen.

Again, I suggest trying to use telnet to see if you can even connect.
 
So in practice I should open a telnet connection using my pc as a client to connect to the remote smtp server that I use for the email archive to see if with the credentials I use I can establish a connection with the server?
I've never done it but I try to find out how to do it.
 
Yes.

I also forgot to mention that if you are behind a corporate firewall, a lot of businesses also block outbound connections to common email ports to prevent people from exfiltrating data through non-approved or non-monitored methods.
 
Hi, I took a look at telnet for a while, so I have to activate the client on the pc and then the syntax should be:
telnet smtpservername port
it's right?
but then how do I enter the password?
 
Depending on the telnet client you would start it different ways. It sounds like you are trying to use the version of telnet built into Windows since you are saying that you need to activate the Windows feature set that packages it. For that version, yes, that would be the syntax in a console window to start telnet.

Read about the SMTP protocol.
 
I would use the integrated version in windows, but I did not understand how to enter the password in the telnet syntax to connect to the remote smtp server.
 
I thought you were researching the SMTP protocol. *sigh* You put in the user name and password after the initial connection to the SMTP server, and the EHLO handshake. You send the AUTH LOGIN Here's a quick overview of what to expect:
 
I open the Windows command line as an administrator and type:

telnet smtpnameserver 25
EHLO smptnameserver.com
AUTH LOGIN

I get this:

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

However, if I continue with the other commands in the guide that you indicated, it tells me "unrecognized commands", however, I'll try again!
 
Last edited:
Back
Top Bottom