This demonstrate will show you how to send e-mail from Visual Basic NET with the Easy Email ActiveX. The Easy Email ActiveX is an OCX component that can send the email using POP3 and SMTP.
You can send add attachments, display name, recipient, connect retry, connect time out, connect base, encode type, and much more.
Please note: There is no need to use any additional component (such the Winsock control) in order to use the Easy Email ActiveX. This component is independent, runs fast and easy to use.
Requirements:
- Visual Basic 2008/2010
- Easy Email ActiveX | Download
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 | Option Strict Off Option Explicit On Imports VB = Microsoft.VisualBasic Friend Class frmMain Inherits System.Windows.Forms.Form Private Sub cmdAddFile_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAddFile.Click 'Add attachments: On Error GoTo ErrcmdAddFile_Click With CommonDialog1Open .ShowDialog() If Err.Number = 0 Then If Trim(.FileName) <> "" Then lstAttachments.Items.Add (.FileName) Else Exit Sub End If End If End With Exit Sub ErrcmdAddFile_Click: End Sub Private Sub cmdRemove_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRemove.Click 'Remove an attachment: On Error Resume Next lstAttachments.Items.RemoveAt (lstAttachments.SelectedIndex) End Sub Public Sub SendEMail() 'Set the properties and send the email: Dim i As Short Dim ulimit As Short Dim m_strAttachedFiles As String Dim strTemp As String Dim c As System.Windows.Forms.Control m_strAttachedFiles = "" On Error GoTo ErrSendEMail 'Error handler If Me.txtTo.Text = "" Then MsgBox ("Please enter an E-Mail Address!") Exit Sub End If 'Get all the attachments ulimit = lstAttachments.Items.Count Select Case ulimit Case Is > 1 For i = 0 To ulimit - 1 m_strAttachedFiles = VB6.GetItemString(lstAttachments, i) & ";" & m_strAttachedFiles Next i 'Cut the ; from the rest If VB.Right(m_strAttachedFiles, 1) = ";" Then m_strAttachedFiles = VB.Left(m_strAttachedFiles, Len(m_strAttachedFiles) - 1) End If Case 1 i = 0 m_strAttachedFiles = VB6.GetItemString(lstAttachments, i) End Select ' These are optioanl properties. ' You must set them first if you decide to use them: cEmail1.SMTPPort = Val(txtPort.Text) cEmail1.SMTPHostValidation = Easy_Email_ActiveX.VALIDATE_METHOD.validate_none cEmail1.EmailAddressValidation = Easy_Email_ActiveX.VALIDATE_METHOD.VALIDATE_SYNTAX cEmail1.Delimiter = ";" ' The email properties: cEmail1.set_SMTPHost (txtHost.Text) cEmail1.From = txtFromEMail.Text cEmail1.FromDisplayName = txtFromEMail.Text cEmail1.Recipient = txtTo.Text cEmail1.Subject = Me.txtSubject.Text 'Don't forget to add the new line to the 'end of the message. cEmail1.Message = rtfMail.Text & vbCrLf cEmail1.Attachment = Trim(m_strAttachedFiles) ' More properties: cEmail1.AsHTML = False cEmail1.UseAuthentication = False cEmail1.UsePopAuthentication = True cEmail1.Username = txtUserName.Text cEmail1.Password = txtPassword.Text cEmail1.set_POP3Host (txtHost.Text) 'Send the email: cEmail1.send() Exit Sub ErrSendEMail: MsgBox("Error SendEMail" & Err.Description & "(" & Err.Number & ")", MsgBoxStyle.Information) End Sub Private Sub cmdSendMail_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdSendMail.Click SendEMail() End Sub Private Sub frmMain_Load(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles MyBase.Load 'Init the control: 'Must be call before using any 'functionality of the control. 'sKey = Your registration key. 'On trial mode use the "Trial Mode." cEmail1.Init ("Trial Mode.") End Sub Private Sub frmMain_FormClosed(ByVal eventSender As System.Object, ByVal eventArgs As System.Windows.Forms.FormClosedEventArgs) Handles Me.FormClosed 'Release the control 'from the memory: cEmail1.DeInit() End Sub ' ***************************************************************************** ' Events ' ***************************************************************************** Private Sub cEmail1_Progress(ByVal eventSender As System.Object, ByVal eventArgs As AxEasy_Email_ActiveX.__cEmail_ProgressEvent) Handles cEmail1.Progress Status.Text = CStr(eventArgs.percentComplete) End Sub Private Sub cEmail1_SendFailed(ByVal eventSender As System.Object, ByVal eventArgs As AxEasy_Email_ActiveX.__cEmail_SendFailedEvent) Handles cEmail1.SendFailed 'SendFailed Event MsgBox ("Your attempt to send mail failed for the following reason(s): " & vbCrLf & eventArgs.Explanation) Debug.Print ("Your attempt to send mail failed for the following reason(s): " & vbCrLf & eventArgs.Explanation) End Sub Private Sub cEmail1_SendSuccesful(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cEmail1.SendSuccesful 'SendSuccesful Event' MsgBox ("SendSuccesful") End Sub Private Sub cEmail1_Status(ByVal eventSender As System.Object, ByVal eventArgs As AxEasy_Email_ActiveX.__cEmail_StatusEvent) Handles cEmail1.Status 'Status Event txtStatus.Text = txtStatus.Text & TimeOfDay & ":" & eventArgs.Status & vbCrLf End Sub End Class |
Links:
- Visual Basic 2008/2010
- Easy Email ActiveX | Download
read the original story ...