Thread: How do I send mail ?

  1. #1
    Registered User gilit2's Avatar
    Join Date
    Jan 2011
    Location
    Israel
    Posts
    10

    How do I send mail ?

    How do I send an email (files) to mail recipient
    from my C/C++ program code :

    I need to send existing files .
    What is the code for it ?
    I use windows XP VisualStudio C++

    How do I do it in C# ?

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    At minimum, you need a TCP/IP client. You might need a TCP/IP server. You could then learn enough of the SMTP protocol to do what you want, it is probably not that complicated. There may some libraries around to cover that, altho a quick google didn't turn up too much WRT C/C++.

    I would not rely on sending general mail directly from an individual computer; many large ISPs will just chuck whatever you send them without looking at it. The extent and consequences of that are debatable:

    http://cboard.cprogramming.com/proje...ml#post1081568

    So consider yourself warned, but don't take my word for it if you don't want to.

    There may also be some open source mail server that you can plug into, I dunno, but the same whitelist problem still applies. Your best bet is probably to use your real email address and send to your ISP's mailserver, the same way your regular email software does.
    Last edited by MK27; 01-24-2012 at 01:41 PM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User gilit2's Avatar
    Join Date
    Jan 2011
    Location
    Israel
    Posts
    10

    I only need to send one mail -

    Thanks
    I don't need a mail server or client.
    I only want to send one file from my application,
    when the user clicks on a button.
    and only for Windows.
    I think that in C# (windows), they have a class
    and It requires only to call it like :
    files: MyFilesObject
    and then MyFilesObject.sendFile (... filename .. )

    So How would I do it in C++ ?

  4. #4
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    Quote Originally Posted by gilit2 View Post
    Thanks
    I don't need a mail server or client.

    So How would I do it in C++ ?
    As the guy above said - you either need to write it or find a library of someone that's already written it. It's *NOT* as simple as just a one-liner in C++. For a start, it differs depending on what system you're using and how it's configured and whether you send directly to the recipient or to your ISP's mailserver, etc. In C# (which only works on Windows), they give you a nice one-liner. C++ does not, and will not, ever have one because you can't make it work the same across all systems.

    I advise you write your spammy application in another language, find a pre-written C++ library, or learn how to talk SMTP.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by gilit2 View Post
    Thanks
    I don't need a mail server or client.
    Yes you do, if you want to try and send email.

    I think that in C# (windows), they have a class
    and It requires only to call it like :
    files: MyFilesObject
    and then MyFilesObject.sendFile (... filename .. )
    I don't know C#, but the "I think" looks a little sketchy to me. Have you actually used this function to send email to another computer?

    If not, most likely you are a little confused about what's going on, etc.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User gilit2's Avatar
    Join Date
    Jan 2011
    Location
    Israel
    Posts
    10

    ok - so , I will try to connect my c++ application to C#

    In C# (which only works on Windows), they give you a nice one-liner. C++ does not, and will not, ever have one because you can't make it work the same across all systems.
    OK , I understand.
    So I will try another way:
    I want it only windows. It is enough for my very specific case.
    So I would like to call a C# method (with no parameter! )
    And it will call the "files" class and method to send a file to a specific mail recipient.
    No Parameters. Assume the C# knows the file name. assume it is hard coded.
    assume everything is hard-coded:
    the mail recipient address , say "[email protected]",
    and also the local file name, say "MyTeachers.txt"
    So How do I connect them ?

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    I will reiterate my advice:

    "I advise you write your spammy application in another language, find a pre-written C++ library, or learn how to talk SMTP."

    Writing the code to send an email message direct from a C++ program requires either a library that has written the code for you already (google "C++ library smtp", for instance), or you WRITING one of those yourself (effectively). I'd be impressed if you could do it reliably in under a few hundred lines of codes, just for the most basic of cases. It's not something someone is just going to knock up for you on these forums.

    MIME encoding, UUencoding, SMTP, possibly TLS, certainly lots of dealing with SMTP commands that could crop up with the various servers you're sending email to, etc. You can't just "bodge" this. Find a library that does it properly, or learn how to program ALL of those things. Just a hint, the RFC for SMTP alone is about 68 pages - without a single line of code - even for a hideously out-of-date version of it, and the VMime library - the first I found on Google - is about 7 years of work and hundreds of thousands of lines of code. All that just to send an email. Now how complicated do you think that's going to be to do if you can't even google it yourself or even know what SMTP is before you started?

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. mail
    By masum0009 in forum C Programming
    Replies: 3
    Last Post: 05-21-2004, 10:25 AM
  2. Please someone, how do you send an e-mail in c++?
    By RobertK in forum C++ Programming
    Replies: 8
    Last Post: 05-21-2003, 06:32 AM
  3. how to send e-mail
    By metamp in forum Windows Programming
    Replies: 7
    Last Post: 02-17-2002, 05:24 AM
  4. A send mail application with source code
    By Rizwan Rafique in forum Windows Programming
    Replies: 8
    Last Post: 01-11-2002, 10:22 AM
  5. Send Mail
    By Unregistered in forum Windows Programming
    Replies: 0
    Last Post: 11-26-2001, 06:59 PM

Tags for this Thread