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#:
Un saludo.
Siempre con el inconveniente de tener que usar FrameWork...
Para evitar este inconveniente puedes hacerlo en C++:
Siempre puedes probar a hacerlo por consola, para que parezca Batch...
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.
http://foro.elhacker.net/net/libreria_de_snippets_posteen_aqui_sus_snippets-t378770.0.html;msg1873577#msg1873577
O por ejemplo en C#:
Código (csharp) [Seleccionar]
//Enviar un email:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Net.Mail;
public class Emaileitor
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)
{
MailMessage Email = new MailMessage();
try {
SmtpClient SMTPServer = new SmtpClient();
foreach (string Attachment in Attachments) {
Email.Attachments.Add(new Attachment(Attachment));
}
Email.From = new MailAddress(FromAddress);
foreach (string Recipient in Recipients) {
Email.To.Add(Recipient);
}
Email.Subject = Subject;
Email.Body = Body;
SMTPServer.Host = Server;
SMTPServer.Port = Port;
SMTPServer.Credentials = new System.Net.NetworkCredential(UserName, Password);
SMTPServer.EnableSsl = true;
SMTPServer.Send(Email);
Email.Dispose();
return "Email to " + Recipients[0] + " from " + FromAddress + " was sent.";
} catch (SmtpException ex) {
Email.Dispose();
return "Sending Email Failed. Smtp Error. " + ex.Message;
} catch (ArgumentOutOfRangeException ex) {
Email.Dispose();
return "Sending Email Failed. Check Port Number.";
} catch (InvalidOperationException Ex) {
Email.Dispose();
return "Sending Email Failed. Check Port Number.";
}
}
//Ejemplos:
public class Form1 {
private void Button1_Click(object sender, EventArgs e)
{
List<string> Recipients = new List<string>();
Recipients.Add("<Email1>", "<Email2>");
string FromEmailAddress = Recipients(0);
string Subject = "<Asunto del Mensaje>";
string Body = "<Cuerpo del Mensaje>";
string UserName = "<Tu Email>";
string Password = "<Contraseña>";
int Port = 587;
string Server = "smtp.live.com"; //Puede ser también smtp.gmail.com
List<string> Attachments = new List<string>();
Emaileitor.SendEmail(Recipients, FromEmailAddress, Subject, Body, UserName, Password, Server, Port, Attachments);
}
}
Un saludo.
Siempre con el inconveniente de tener que usar FrameWork...
Para evitar este inconveniente puedes hacerlo en C++:
Código (cpp) [Seleccionar]
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <fstream.h>
#include <iostream.h>
#include <windows.h>
#include <winsock2.h>
#pragma comment(lib, "ws2_32.lib")
// Insist on at least Winsock v1.1
const VERSION_MAJOR = 1;
const VERSION_MINOR = 1;
#define CRLF "\r\n" // carriage-return/line feed pair
void ShowUsage(void)
{
cout << "Usage: SENDMAIL mailserv to_addr from_addr messagefile" << endl
<< "Example: SENDMAIL smtp.myisp.com rcvr@elsewhere.com my_id@mydomain.com message.txt" << endl;
exit(1);
}
// Basic error checking for send() and recv() functions
void Check(int iStatus, char *szFunction)
{
if((iStatus != SOCKET_ERROR) && (iStatus))
return;
cerr << "Error during call to " << szFunction << ": " << iStatus << " - " << GetLastError() << endl;
}
int main(int argc, char *argv[])
{
int iProtocolPort = 0;
char szSmtpServerName[64] = "";
char szToAddr[64] = "";
char szFromAddr[64] = "";
char szBuffer[4096] = "";
char szLine[255] = "";
char szMsgLine[255] = "";
SOCKET hServer;
WSADATA WSData;
LPHOSTENT lpHostEntry;
LPSERVENT lpServEntry;
SOCKADDR_IN SockAddr;
// Check for four command-line args
if(argc != 5)
ShowUsage();
// Load command-line args
lstrcpy(szSmtpServerName, argv[1]);
lstrcpy(szToAddr, argv[2]);
lstrcpy(szFromAddr, argv[3]);
// Create input stream for reading email message file
ifstream MsgFile(argv[4]);
// Attempt to intialize WinSock (1.1 or later)
if(WSAStartup(MAKEWORD(VERSION_MAJOR, VERSION_MINOR), &WSData))
{
cout << "Cannot find Winsock v" << VERSION_MAJOR << "." << VERSION_MINOR << " or later!" << endl;
return 1;
}
// Lookup email server's IP address.
lpHostEntry = gethostbyname(szSmtpServerName);
if(!lpHostEntry)
{
cout << "Cannot find SMTP mail server " << szSmtpServerName << endl;
return 1;
}
// Create a TCP/IP socket, no specific protocol
hServer = socket(PF_INET, SOCK_STREAM, 0);
if(hServer == INVALID_SOCKET)
{
cout << "Cannot open mail server socket" << endl;
return 1;
}
// Get the mail service port
lpServEntry = getservbyname("mail", 0);
// Use the SMTP default port if no other port is specified
if(!lpServEntry)
iProtocolPort = htons(IPPORT_SMTP);
else
iProtocolPort = lpServEntry->s_port;
// Setup a Socket Address structure
SockAddr.sin_family = AF_INET;
SockAddr.sin_port = iProtocolPort;
SockAddr.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
// Connect the Socket
if(connect(hServer, (PSOCKADDR) &SockAddr, sizeof(SockAddr)))
{
cout << "Error connecting to Server socket" << endl;
return 1;
}
// Receive initial response from SMTP server
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() Reply");
// Send HELO server.com
sprintf(szMsgLine, "HELO %s%s", szSmtpServerName, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() HELO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() HELO");
// Send MAIL FROM: <sender@mydomain.com>
sprintf(szMsgLine, "MAIL FROM:<%s>%s", szFromAddr, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() MAIL FROM");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() MAIL FROM");
// Send RCPT TO: <receiver@domain.com>
sprintf(szMsgLine, "RCPT TO:<%s>%s", szToAddr, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() RCPT TO");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() RCPT TO");
// Send DATA
sprintf(szMsgLine, "DATA%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() DATA");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() DATA");
// Send all lines of message body (using supplied text file)
MsgFile.getline(szLine, sizeof(szLine)); // Get first line
do // for each line of message text...
{
sprintf(szMsgLine, "%s%s", szLine, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() message-line");
MsgFile.getline(szLine, sizeof(szLine)); // get next line.
} while(MsgFile.good());
// Send blank line and a period
sprintf(szMsgLine, "%s.%s", CRLF, CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() end-message");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() end-message");
// Send QUIT
sprintf(szMsgLine, "QUIT%s", CRLF);
Check(send(hServer, szMsgLine, strlen(szMsgLine), 0), "send() QUIT");
Check(recv(hServer, szBuffer, sizeof(szBuffer), 0), "recv() QUIT");
// Report message has been sent
cout << "Sent " << argv[4] << " as email message to " << szToAddr << endl;
// Close server socket and prepare to exit.
closesocket(hServer);
WSACleanup();
return 0;
}
Siempre puedes probar a hacerlo por consola, para que parezca Batch...
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.