enter image description herewhen I send Email from my vb .net windows form than an error occur
i think there is some authentication problem..
MailBox Unavailable.The Server Response Was:5.7.3
Requested Action Aborted;
User Not Authenticated
Try this code:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim Correo As New System.Net.Mail.MailMessage
Dim SMTP As New System.Net.Mail.SmtpClient
Correo.From = "VALID EMAIL FROM"
Correo.To.Add("VALID EMAIL TO)
Correo.Subject = "Subject text"
Correo.Priority = System.Net.Mail.MailPriority.Normal
Correo.IsBodyHtml = True
Correo.Body = "BODY OF EMAIL"
Correo.Attachments.Add("DIRECTORY FROM FILE TO ATTACH")
SMTP.Host = "VALID SMTP"
SMTP.Port = "VALID PORT"
SMTP.EnableSsl = "SSL TRUE OR FALSE"
SMTP.UseDefaultCredentials = False
SMTP.Credentials = New System.Net.NetworkCredential("user name", "password")
SMTP.DeliveryMethod = Net.Mail.SmtpDeliveryMethod.Network
Dim Token As Object = Correo
AddHandler SMTP.SendCompleted, AddressOf SmtpClient_OnCompleted
SMTP.SendAsync(Correo, Token)
' Wait response from SmtpClient_OnCompleted
End Sub
Public Sub SmtpClient_OnCompleted(ByVal sender As Object, ByVal e As System.ComponentModel.AsyncCompletedEventArgs)
End Sub
Related
I've been looking at trying to get my Access database to automatically send an email to notify me of an addition once a user has entered a new record through a form. I've tried using:
Private Sub Form_AfterInsert()
Dim oApp As Outlook.Application
Dim oMail As MailItem
Set oApp = CreateObject("Outlook.application")
Set oMail = oApp.CreateItem(olMailItem)
oMail.Body = "Body of the email"
oMail.Subject = "Test Subject"
oMail.To = "Someone#somewhere.com"
oMail.Send
Set oMail = Nothing
Set oApp = Nothing
End Sub
But it just gives me the error: Compile error: User-defined type not defined.
I want to send a mail using gmail id, my code is as here under, but an error occurred. How can I solve this?
Imports System.Net
Imports System.Net.Mail
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
MsgBox("This will send a mail...")
Try
Dim smtpserver As New SmtpClient()
Dim mail As New MailMessage()
smtpserver.Credentials = New Net.NetworkCredential("myname#gmail.com", "password")
smtpserver.Port = 465
smtpserver.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("myname#gmail.com")
mail.To.Add("to mail id")
mail.Subject = "Test by bharat"
mail.Body = "hello ooooooooooooooooooooooo"
smtpserver.Send(mail)
MsgBox("Mail Sent")
Catch ex As Exception
MsgBox(ex.ToString)
Close()
End Try
End Sub
End Class
Below is an example of sending an email with visual basic 10, First you need to build a form with fields to,from,text,and email_send button now the below code
Private Smtp_Server As New SmtpClient()
Private Sub email_send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles email_send.Click
Try
Dim e_mail As New MailMessage()
Smtp_Server.Credentials = New Net.NetworkCredential("yener.turkeli#v-basic.net", "password")
Smtp_Server.Port = 25
Smtp_Server.Host = "mail.v-basic.net"
e_mail = New MailMessage()
e_mail.From = New MailAddress(from_txt.Text)
e_mail.To.Add(to_txt.Text)
e_mail.Subject = "VB.NET Email Sending"
e_mail.Body = text_txt.Text
Smtp_Server.Send(e_mail)
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
You can make a new class called 'Mail'.
If you make a class, you don't have to type all of the code again if you want to send more than 1 email.
Imports System.Net.Mail
Public NotInheritable Class Mail
Public Property subject As String
Public Property body As String
Public Property receiver As String
Public Sub send()
Try
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("yener.turkeli#v-basic.net", "password"))
smtpServer.Port = 587
smtpServer.EnableSsl = True
smtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("yener.turkeli#v-basic.net")
mail.To.Add(receiver)
mail.Subject = subject
mail.Body = body
smtpServer.Send(mail)
Catch ex As Exception
MsgBox(ex.Message & vbNewLine & ex.StackTrace)
End Try
End Sub
End Class
And then you can make a new object in your code where and when you want to send the email:
Dim email as new Mail
email.receiver = "receiver#example.com"
email.subject = "Subject"
email.body = "Message"
email.send()
Imports System.Net.Mail
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim Smtp_Server As New SmtpClient
Dim e_mail As New MailMessage()
Smtp_Server.UseDefaultCredentials = False
Smtp_Server.Credentials = New Net.NetworkCredential("myemail#gmail.com", "Password")
Smtp_Server.Port = 587
Smtp_Server.EnableSsl = True
Smtp_Server.Host = "smtp.gmail.com"
e_mail = New MailMessage()
e_mail.From = New MailAddress(TextBox1.Text)
e_mail.To.Add(TextBox2.Text)
e_mail.Subject = "Email Sending"
e_mail.IsBodyHtml = False
e_mail.Body = txtmsg.Text
Smtp_Server.Send(e_mail)
MsgBox("Mail Sent")
Catch error_t As Exception
MsgBox(error_t.ToString)
End Try
End Sub
End Class
How to send a mail from MS Access Form. ?
Assume I have MS Access Application with a DashBoard form.
I have send mails to certain mail address
like sending a notification mail on clicking "send notification button"
How to do this with codebuilder in MS access
I found that you can send mail using this code snippet
Private Sub send_mail()
Dim olApp As Object
Dim objMail As Object
On Error Resume Next 'Keep going if there is an error
Set olApp = GetObject(, "Outlook.Application") 'See if Outlook is open
If Err Then 'Outlook is not open
Set olApp = CreateObject("Outlook.Application") 'Create a new instance
End If
'Create e-mail item
Set objMail = olApp.CreateItem(olMailItem)
With objMail
'Set body format to HTML
.BodyFormat = olFormatHTML
.To = "abc#yourmailaddress.com"
.Cc ="ccaddress#yourmailaddress.com"
.Subject = "Subject LIne"
.HTMLBody = "<htmltags>Body Content</htmltags>"
.send
End With
MsgBox "Operation completed successfully"
End Sub
Source : Thread from Access Programmer site
I found some code that worked for email based on code that I was using to open a browser from a contact's home page, worked just fine and was a lot easier for me to understand. For opening a web page, delete the '"mailto:" & '.
Private Sub cmdEmailContact_Click()
Dim sWebPath As String
Dim sFullLinkPath As String
If IsNull(Me.ContactEmail) Then
MsgBox ("Can't create email: no address listed")
Exit Sub
End If
sWebPath = "mailto:" & Me.ContactEmail
sFullLinkPath = sWebPath
Application.FollowHyperlink sFullLinkPath
End Sub
I am trying to create an App in Microsoft Access 2007. How can I silently send an email out using Outlook 2007 upon a specific event without any user interaction. How should I approach this. If you can provide some VBA some it would be extremely nice, but if not, could you guide me in how to accomplish this?
I was able to solve my problem with the following code:
Public Sub SendEmail()
Email_Bcc = "email#domain.com"
Email_Body = "Email body!!!!"
Email_Subject = "Email Subject"
On Error GoTo debugs
Set Mail_Object = CreateObject("Outlook.Application")
Set Mail_Single = Mail_Object.CreateItem(0)
With Mail_Single
.Subject = Email_Subject
.To = Email_Send_To
.cc = Email_Cc
.BCC = Email_Bcc
.Body = Email_Body
.send
End With
debugs:
If Err.Description <> "" Then MsgBox Err.Description
End Sub
First declare a couple variables in the event that you want to send the email, or in a function you'd like the event to call.
Public Started As Boolean
Public oApp As Outlook.Application
Public oItem As Outlook.MailItem
Next, open or get Outlook if it's running.
On Error Resume Next
'Get Outlook if it's running
Set oApp = GetObject(, "Outlook.Application")
If Err <> 0 Then
'Outlook wasn't running, start it from code
Set oApp = CreateObject("Outlook.Application")
Started = True
End If
Now, do what you've got to do with your email.
Set oItem = oApp.CreateItem(olMailItem)
With oItem
.To = "email#email.com"
.Subject = "Your email, sirrah."
.Body = "Please enjoy this complimentary email."
'Send the email
.Send
End With
Finally, close outlook if it wasn't running before and clean up.
Set oItem = Nothing
If Started Then
oApp.Quit
End If
You can do this "code-free" by using a macro and the "SendObject" action. Be sure to complete all the necessary arguments To, Subject, Message Text, and then set the Edit Message argument to 'No'.
You can then attach this macro to any event you wish, such as the OnClick event of a button.
I have a userform that helps different users fill in data into the spreadsheet. As soon as the data is inserted it should also be sent by email to a few recipients, depending on the options filled in the form.
This happens within a corporate environment using Exchange. I would create a new email account for this file to be able to send the email as an entity and not use the user's email account.
Is this possible? How? I have googled for it and all I can find is how to create a mail message that the user sends from his account.
I've used the code below (source) to send e-mails from Excel-VBA. I've only tested it with my own e-mail account, but I assume you could have it send mail from a different account (msgOne.from = ...), as long as the user has permission to send from that account on the Exchange server.
Dim cdoConfig
Dim msgOne
Set cdoConfig = CreateObject("CDO.Configuration")
With cdoConfig.Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServerPort) = 25 '465 ' (your port number) usually is 25
.Item(cdoSMTPServer) = "smtp.mysmtpserver.com" ' your SMTP server goes here
'.Item(cdoSendUserName) = "My Username"
'.Item(cdoSendPassword) = "myPassword"
.Update
End With
Set msgOne = CreateObject("CDO.Message")
Set msgOne.Configuration = cdoConfig
msgOne.To = "someone#somewhere.com"
msgOne.from = "me#here.com"
msgOne.subject = "Test CDO"
msgOne.TextBody = "It works just fine."
msgOne.Send
Unfortunately, I can't test this hypothesis at this time, as I'm only set up to send from one account. Let me know how it works out!
If the excel application is running on a machine with outlook, you can something along the following.
Function SendEmailWithOutlook(er As emailRecord,
recipients As String,
cc As String,
subject As String,
body As String,
attachmentPath As String) As Boolean
Dim errorMsg As String
Dim OutApp As Object
Dim OutMail As Object
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error GoTo errHandle
If (er.useTestEmail = True) Then
recipients = er.emailTest
cc = er.emailTest
End If
With OutMail
If er.emailFrom <> "" Then
.sentOnBehalfOfName = er.emailFrom
End If
.To = recipients
.cc = cc
.bcc = er.emailBcc
.subject = subject
.htmlBody = body
If attachmentPath <> "" Then
.Attachments.Add attachmentPath
End If
.Send 'or use .Display
End With
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
SendEmailWithOutlook = True
Exit Function
errHandle:
errorMsg = "Error sending mail via outlook: " & Err.Description & vbCrLf
errorMsg = errorMsg & "OnBehalfOf:" & er.emailFrom & vbCrLf
errorMsg = errorMsg & "Recipients: " & recipients & vbCrLf
errorMsg = errorMsg & "CC: " & cc & vbCrLf
errorMsg = errorMsg & "BCC: " & er.emailBcc
MsgBox errorMsg
SendEmailWithOutlook = False
End Function
Add a reference to Microsoft Outlook 14.0 Object Library
Why not use the Outlook Object Model?
You can give the current user the right to send on behalf of the specified user, then set MailItem.SentOnBehalfOfName and MailItem.ReplyRecipients (if necessary) properties before callign MailItem.Send.