Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: crlos20 en 12 Agosto 2013, 18:40 pm



Título: como puedo enviar unos archivos de manera automatica a mi correo por Cmd
Publicado por: crlos20 en 12 Agosto 2013, 18:40 pm
Hola a todos. quisiera que alguien me explicara como puedo enviar unos archivos txt a mi correo de manera automatica por cmd y si es posible enviarlas en un horario definido. Gracias


Título: Re: como puedo enviar unos archivos de manera automatica a mi correo por Cmd
Publicado por: Eleкtro en 16 Agosto 2013, 14:48 pm
Batch no dispone de ningún comando para emails, debes recurrir a aplicaciones de terceros o en su defecto a cualquier otro lenguaje que no sea Batch.

Puedes registrarte de forma gratuita aquí y utilizar la aplicación commandline que ofrecen, para usarla en Batch: https://www.zeta-uploader.com/es , lo llevo usando años, lo bueno es que usan un server dedicado así que no es necesario enviarlo desde una cuenta de correo original y no hay limite (al menos no un limite pequeño) de tamaño.

...O puedes googlear un poco para ver ejemplos de códigos para enviar emails usando VBS, por ejemplo.

Para lo de la hora debes crear una tarea programada en tu PC -> SCHTASKS (http://msdn.microsoft.com/en-us/library/windows/desktop/bb736357%28v=vs.85%29.aspx)

Saludos


Título: Re: como puedo enviar unos archivos de manera automatica a mi correo por Cmd
Publicado por: z3nth10n en 18 Agosto 2013, 13:17 pm
Hola buenas, en Batch como bien @Elektro H@cker creo que no se puede siempre puedes usar VB.NET:

http://foro.elhacker.net/net/libreria_de_snippets_posteen_aqui_sus_snippets-t378770.0.html;msg1873577#msg1873577

O por ejemplo en C#:

Código
  1. //Enviar un email:
  2.  
  3. using Microsoft.VisualBasic;
  4. using System;
  5. using System.Collections;
  6. using System.Collections.Generic;
  7. using System.Data;
  8. using System.Diagnostics;
  9. using System.Net.Mail;
  10.  
  11. public class Emaileitor
  12.  
  13. public static object SendEmail(List<string> Recipients, string FromAddress, string Subject, string Body, string UserName, string Password, string Server = "smtp.live.com", int Port = 587, List<string> Attachments = null)
  14. {
  15.  
  16. MailMessage Email = new MailMessage();
  17.    try {
  18.      SmtpClient SMTPServer = new SmtpClient();
  19.      foreach (string Attachment in Attachments) {
  20.        Email.Attachments.Add(new Attachment(Attachment));
  21.      }
  22.      Email.From = new MailAddress(FromAddress);
  23.      foreach (string Recipient in Recipients) {
  24.        Email.To.Add(Recipient);
  25.      }
  26.      Email.Subject = Subject;
  27.      Email.Body = Body;
  28.      SMTPServer.Host = Server;
  29.      SMTPServer.Port = Port;
  30.      SMTPServer.Credentials = new System.Net.NetworkCredential(UserName, Password);
  31.      SMTPServer.EnableSsl = true;
  32.      SMTPServer.Send(Email);
  33.      Email.Dispose();
  34.      return "Email to " + Recipients[0] + " from " + FromAddress + " was sent.";
  35.    } catch (SmtpException ex) {
  36.      Email.Dispose();
  37.      return "Sending Email Failed. Smtp Error. " + ex.Message;
  38.    } catch (ArgumentOutOfRangeException ex) {
  39.      Email.Dispose();
  40.      return "Sending Email Failed. Check Port Number.";
  41.    } catch (InvalidOperationException Ex) {
  42.      Email.Dispose();
  43.      return "Sending Email Failed. Check Port Number.";
  44.    }
  45.  
  46. }
  47.  
  48. //Ejemplos:
  49.  
  50. public class Form1 {
  51.  
  52. private void Button1_Click(object sender, EventArgs e)
  53. {
  54. List<string> Recipients = new List<string>();
  55. Recipients.Add("<Email1>", "<Email2>");
  56. string FromEmailAddress = Recipients(0);
  57. string Subject = "<Asunto del Mensaje>";
  58. string Body = "<Cuerpo del Mensaje>";
  59. string UserName = "<Tu Email>";
  60. string Password = "<Contraseña>";
  61. int Port = 587;
  62. string Server = "smtp.live.com"; //Puede ser también smtp.gmail.com
  63. List<string> Attachments = new List<string>();
  64. Emaileitor.SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments);
  65. }
  66.  
  67. }

Un saludo.
Siempre con el inconveniente de tener que usar FrameWork...

Para evitar este inconveniente puedes hacerlo en C++:

Código
  1. #define WIN32_LEAN_AND_MEAN
  2.  
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <fstream.h>
  6. #include <iostream.h>
  7. #include <windows.h>
  8. #include <winsock2.h>
  9.  
  10. #pragma comment(lib, "ws2_32.lib")
  11.  
  12. // Insist on at least Winsock v1.1
  13. const VERSION_MAJOR = 1;
  14. const VERSION_MINOR = 1;
  15.  
  16. #define CRLF "\r\n"                 // carriage-return/line feed pair
  17.  
  18. void ShowUsage(void)
  19. {
  20.  cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
  21.       << "Example: SENDMAIL smtp.myisp.com rcvr@elsewhere.com my_id@mydomain.com message.txt" << endl;
  22.  
  23.  exit(1);
  24. }
  25.  
  26. // Basic error checking for send() and recv() functions
  27. void Check(int iStatus, char *szFunction)
  28. {
  29.  if((iStatus != SOCKET_ERROR) && (iStatus))
  30.    return;
  31.  
  32.  cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
  33. }
  34.  
  35. int main(int argc, char *argv[])
  36. {
  37.  int         iProtocolPort        = 0;
  38.  char        szSmtpServerName[64] = "";
  39.  char        szToAddr[64]         = "";
  40.  char        szFromAddr[64]       = "";
  41.  char        szBuffer[4096]       = "";
  42.  char        szLine[255]          = "";
  43.  char        szMsgLine[255]       = "";
  44.  SOCKET      hServer;
  45.  WSADATA     WSData;
  46.  LPHOSTENT   lpHostEntry;
  47.  LPSERVENT   lpServEntry;
  48.  SOCKADDR_IN SockAddr;
  49.  
  50.  // Check for four command-line args
  51.  if(argc != 5)
  52.    ShowUsage();
  53.  
  54.  // Load command-line args
  55.  lstrcpy(szSmtpServerName, argv[1]);
  56.  lstrcpy(szToAddr, argv[2]);
  57.  lstrcpy(szFromAddr, argv[3]);
  58.  
  59.  // Create input stream for reading email message file
  60.  ifstream MsgFile(argv[4]);
  61.  
  62.  // Attempt to intialize WinSock (1.1 or later)
  63.  if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
  64.  {
  65.    cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
  66.  
  67.    return 1;
  68.  }
  69.  
  70.  // Lookup email server's IP address.
  71.  lpHostEntry = gethostbyname(szSmtpServerName);
  72.  if(!lpHostEntry)
  73.  {
  74.    cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
  75.  
  76.    return 1;
  77.  }
  78.  
  79.  // Create a TCP/IP socket, no specific protocol
  80.  hServer = socket(PF_INET, SOCK_STREAM, 0);
  81.  if(hServer == INVALID_SOCKET)
  82.  {
  83.    cout << "Cannot open mail server socket" << endl;
  84.  
  85.    return 1;
  86.  }
  87.  
  88.  // Get the mail service port
  89.  lpServEntry = getservbyname("mail", 0);
  90.  
  91.  // Use the SMTP default port if no other port is specified
  92.  if(!lpServEntry)
  93.    iProtocolPort = htons(IPPORT_SMTP);
  94.  else
  95.    iProtocolPort = lpServEntry->s_port;
  96.  
  97.  // Setup a Socket Address structure
  98.  SockAddr.sin_family = AF_INET;
  99.  SockAddr.sin_port   = iProtocolPort;
  100.  SockAddr.sin_addr   = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  101.  
  102.  // Connect the Socket
  103.  if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
  104.  {
  105.    cout << "Error connecting to Server socket" << endl;
  106.  
  107.    return 1;
  108.  }
  109.  
  110.  // Receive initial response from SMTP server
  111.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
  112.  
  113.  // Send HELO server.com
  114.  sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
  115.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
  116.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
  117.  
  118.  // Send MAIL FROM: <sender@mydomain.com>
  119.  sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
  120.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
  121.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
  122.  
  123.  // Send RCPT TO: <receiver@domain.com>
  124.  sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
  125.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
  126.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
  127.  
  128.  // Send DATA
  129.  sprintf(szMsgLine, "DATA%s", CRLF);
  130.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
  131.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
  132.  
  133.  // Send all lines of message body (using supplied text file)
  134.  MsgFile.getline(szLine, sizeof(szLine));             // Get first line
  135.  
  136.  do         // for each line of message text...
  137.  {
  138.    sprintf(szMsgLine, "%s%s", szLine, CRLF);
  139.    Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
  140.    MsgFile.getline(szLine, sizeof(szLine)); // get next line.
  141.  } while(MsgFile.good());
  142.  
  143.  // Send blank line and a period
  144.  sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
  145.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
  146.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
  147.  
  148.  // Send QUIT
  149.  sprintf(szMsgLine, "QUIT%s", CRLF);
  150.  Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
  151.  Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
  152.  
  153.  // Report message has been sent
  154.  cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
  155.  
  156.  // Close server socket and prepare to exit.
  157.  closesocket(hServer);
  158.  
  159.  WSACleanup();
  160.  
  161.  return 0;
  162. }

Siempre puedes probar a hacerlo por consola, para que parezca Batch... :P



Si quieres mandar un archivo, pues con esto: (VB.NET)

http://www.freevbcode.com/ShowCode.asp?ID=5486

Si lo necesitas traducir a C#:

http://www.developerfusion.com/tools/convert/vb-to-csharp/?batchId=0f036935-fa35-4c10-8f03-81afc4e77987

Y si lo quieres en C++:

http://www.emailarchitect.net/easendmail/ex/vc/14.aspx





[MOD] @Ikillnukes, estamos en Scripting, si muestras ejemplos ten la consideración de que sean para lenguajes interpretados.

Nota: Además teniendo en Windows un lenguaje que se interpreta de forma nativa (VBS, PS) no hay necesidad de requerir el aprendizaje de un lenguaje compilado para esta tarea.



Título: Re: como puedo enviar unos archivos de manera automatica a mi correo por Cmd
Publicado por: crlos20 en 31 Octubre 2013, 21:40 pm
Muchas gracias Ikillnukes despues de un tiempo ausente ahora lo voy a probar> GRaciass