Como Enviar Correos Desde Visual Basic
-
Buenas amigos del foro necesito que me ayuden, necesito enviar E-Mails desde mi programa creado en Visual Basic y no se como realizar esta acción creo que se puede conectar con el Outlook.
Favor AYUDA es urgente, de ante mano muchas gracias.
PD: SI ES POSIBLE ENVIEN CODIGOS CON LA AYUDA PARA GUIARME. -
A ver, buscando en Dios Google, encontré este código. Prueben y dps cuentan si funca, yo no lo probé.
Saludos!Fuente: http://www.vbip.com/winsock/winsock_simple_sender.aspCódigo PHP:Private Enum SMTP_State
MAIL_CONNECT
MAIL_HELO
MAIL_FROM
MAIL_RCPTTO
MAIL_DATA
MAIL_DOT
MAIL_QUIT
End Enum
Private m_State As SMTP_State
'
Private Sub cmdClose_Click()
Unload Me
End Sub
Private Sub cmdNew_Click()
txtRecipient = ""
txtSubject = ""
txtMessage = ""
End Sub
Private Sub cmdSend_Click()
Winsock1.Connect Trim$(txtHost), 25
m_State = MAIL_CONNECT
End Sub
Private Sub Form_Load()
'
'clear all textboxes
'
For Each ctl In Me.Controls
If TypeOf ctl Is TextBox Then
ctl.Text = ""
End If
Next
'
End Sub
Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
Dim strServerResponse As String
Dim strResponseCode As String
Dim strDataToSend As String
'
'Retrive data from winsock buffer
'
Winsock1.GetData strServerResponse
'
Debug.Print strServerResponse
'
'Get server response code (first three symbols)
'
strResponseCode = Left(strServerResponse, 3)
'
'Only these three codes tell us that previous
'command accepted successfully and we can go on
'
If strResponseCode = "250" Or _
strResponseCode = "220" Or _
strResponseCode = "354" Then
Select Case m_State
Case MAIL_CONNECT
'Change current state of the session
m_State = MAIL_HELO
'
'Remove blank spaces
strDataToSend = Trim$(txtSender)
'
'Retrieve mailbox name from e-mail address
strDataToSend = Left$(strDataToSend, _
InStr(1, strDataToSend, "@") - 1)
'Send HELO command to the server
Winsock1.SendData "HELO " & strDataToSend & vbCrLf
'
Debug.Print "HELO " & strDataToSend
'
Case MAIL_HELO
'
'Change current state of the session
m_State = MAIL_FROM
'
'Send MAIL FROM command to the server
Winsock1.SendData "MAIL FROM:" & Trim$(txtSender) & vbCrLf
'
Debug.Print "MAIL FROM:" & Trim$(txtSender)
'
Case MAIL_FROM
'
'Change current state of the session
m_State = MAIL_RCPTTO
'
'Send RCPT TO command to the server
Winsock1.SendData "RCPT TO:" & Trim$(txtRecipient) & vbCrLf
'
Debug.Print "RCPT TO:" & Trim$(txtRecipient)
'
Case MAIL_RCPTTO
'
'Change current state of the session
m_State = MAIL_DATA
'
'Send DATA command to the server
Winsock1.SendData "DATA" & vbCrLf
'
Debug.Print "DATA"
'
Case MAIL_DATA
'
'Change current state of the session
m_State = MAIL_DOT
'
'So now we are sending a message body
'Each line of text must be completed with
'linefeed symbol (Chr$(10) or vbLf) not with vbCrLf
'
'Send Subject line
Winsock1.SendData "Subject:" & txtSubject & vbLf
'
Debug.Print "Subject:" & txtSubject
'
Dim varLines As Variant
Dim varLine As Variant
'
'Parse message to get lines (for VB6 only)
varLines = Split(txtMessage, vbCrLf)
'
'Send each line of the message
For Each varLine In varLines
Winsock1.SendData CStr(varLine) & vbLf
'
Debug.Print CStr(varLine)
Next
'
'Send a dot symbol to inform server
'that sending of message comleted
Winsock1.SendData "." & vbCrLf
'
Debug.Print "."
'
Case MAIL_DOT
'Change current state of the session
m_State = MAIL_QUIT
'
'Send QUIT command to the server
Winsock1.SendData "QUIT" & vbCrLf
'
Debug.Print "QUIT"
Case MAIL_QUIT
'
'Close connection
Winsock1.Close
'
End Select
Else
'
'If we are here server replied with
'unacceptable respose code therefore we need
'close connection and inform user about problem
'
Winsock1.Close
'
If Not m_State = MAIL_QUIT Then
MsgBox "SMTP Error: " & strServerResponse, _
vbInformation, "SMTP Error"
Else
MsgBox "Message sent successfuly.", vbInformation
End If
'
End If
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
MsgBox "Winsock Error number " & Number & vbCrLf & _
Description, vbExclamation, "Winsock Error"
End Sub
-
.
Hola a Todos!!. Yo también necesito crear un programa en Visual Basic que mande mails por correo electronico usando un servidor SMTP. Ya probé con esta dll que enviaron y con otro programa que descarge, utiliza un control llamado MAIL.OCX, aparentemente todo funciona muy bien. Cuando ejecuto el programa me sale en la barra de estado que si hay conexion con el servidor y todo, incluso se dispara el evento Send Succesful, que se supone solo se ejecuta cuando el mail fue enviado correctamente, el problema es que el mail nunca llega a su destino. ¿como es posible?, se supone que ese evento solo se dispara cuando el mail se envió. Corri el programa como 5 veces, y solo llego el mail en 2 de esas ocasiones. ¿como lo puedo corregir???. Ayuda por favor!!!!
P.D. Si requieren el programa con el control OCX solo diganme para enviarlo.
Muchas gracias. Ojala alguien me pueda ayudar!!!! -
Podés usar las APIS de outlook u outlook express o usar winsock y conectarte a un servidor smtp y seguir el protocolo de la siguiente forma:
S: 220 Servidor ESMTP
C: HELO
S: 250 Hello, please meet you
C: MAIL FROM: yo@midominio.com
S: 250 Ok
C: RCPT TO: destinatario@sudominio.com
S: 250 Ok
C: DATA
S: 354 End data with.
C: Subject: Campo de asunto
C: From: yo@midominio.com
C: To: destinatario@sudominio.com
C:
C: Hola,
C: Esto es una prueba.
C: Adios.
C: .
S: 250 Ok: queued as 12345
C: quit
S: 221 Bye
Saludos
