I had a working mail client in Visual Basic that sent email perfectly. Then one day it stopped working. When I used VB's try/catch on my method the exception it threw was Failure Sending Mail.
This doesn't help of course, because it's incredibly general. I have no clue why the mail isn't being sent. I searched the web, as well as StackOverflow, but none of the solutions provided worked for me.
I've tried Aol and Gmail and I'm sure that all passwords and usernames are correct. Also the port is correct too. What could be the problem?
Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click
Try
Dim smtpServer As New SmtpClient("smtp.aol.com")
Dim Mail As New MailMessage()
smtpServer.UseDefaultCredentials = False
smtpServer.Credentials = New Net.NetworkCredential("Email", "Pass"
smtpServer.Port = 587
smtpServer.Host = "smtp.aol.com"
smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
smtpServer.EnableSsl = True
Mail.From = New MailAddress("Email")
Mail.To.Add("Email")
smtpServer.Send(Mail)
Catch Ex As Exception
MsgBox(Ex.Message)
End Try
Related
I am trying to send an email after a click of a button. But this is the exception i get
Is something wrong with my code ? I have already tried to go to my gmail settings and turned on the "Access for less secure apps" , still problem persists.
Imports System.Net.Mail
Imports System.Net
Partial Class _Default
Inherits System.Web.UI.Page`
Protected Sub Button1_Click(sender As Object, e As System.EventArgs) Handles Button1.Click
Try
Dim mailMessage As New MailMessage("group2#gmail.com", "ndumisosizwe#gmail.com")
mailMessage.Subject = "Mail Body"
mailMessage.Body = "This is a test email"
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
Dim credentials As New NetworkCredential("group2#gmail.com", "myPasswordHere")
SmtpClient.Credentials = credentials
SmtpClient.EnableSsl = True
SmtpClient.Send(mailMessage)
MsgBox("Email sent successfully")
Catch ex As Exception
MsgBox("Email Failed ! " & ex.ToString)
End Try
End Sub
End Class
The exception is thrown on the line SmtpClient.Send(mailMessage)
The port you are using is for TLS according to this page.
The port for SSL (older version) is 465.
Perhaps EnableSsl does not support TLS. You can try with that port.
I created a contact form and have a security question for this form.
I did some research for my question already, but I want to double-check before I make any mistakes...
Basically, I created my contact form in my code behind as follows.
In there, I would have to include my Email address and my password to get connected to the smtp server.
I am afraid it's easy to find out my credentials.
Does anybody have a recommendation for how to secure this code? (Or am I dead wrong with this solution anyways?)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
End Sub
Protected Sub Button_Click(ByVal sender As Object, ByVal e As EventArgs)
Try
If Page.IsValid Then
Dim mailMessage As New MailMessage()
mailMessage.From = New MailAddress("MYEMAILADRESS")
mailMessage.To.Add("MYEMAILADRESS")
mailMessage.Subject = txtSubject.Text
mailMessage.Body = "<b>Sender Name : </b>" & txtName.Text & "<br/>" & "<b>Sender Email : </b>" & txtEmail.Text & "<br/>" & "<b>Phone </b>" & txtPhone.Text & "<br/>" & "<b>Comments : </b>" & txtComments.Text
mailMessage.IsBodyHtml = True
Dim smtpClient As New SmtpClient("smtp.gmail.com", 587)
smtpClient.EnableSsl = True
smtpClient.Credentials = New System.Net.NetworkCredential("MYEMAILADRESS", "PASSWORD")
smtpClient.Send(mailMessage)
Label.Text = "Thank you for contacting us"
txtName.Enabled = False
txtEmail.Enabled = False
txtComments.Enabled = False
txtSubject.Enabled = False
txtPhone.Enabled = False
Button.Enabled = False
End If
Catch ex As Exception
'Log - Event Viewer or table
Label.Text = "There is an unknown problem, please try later"
End Try
End Sub
As was alluded to in the comments, you'll want to move a lot of these values to a config file, including your password. Ideally, you will store the credentials in an encrypted format so that if someone steals your config file, they can't access your email.
See this answer for directions on how to encrypt it: Encrypting Web.Config
Detailing how to encrypt/decrypt the file is a bit out of scope for this question, but I think it is the right path for you to go down.
I have tried almost every thing but can't get this to work. I will appreciate any help. thanks
Imports Microsoft.VisualBasic, System.Net.Mail
Shared Sub SendMailHTML(ByVal ToAdd, ByVal FromAdd, ByVal Message, ByVal Subject)
Dim mail As New MailMessage()
Dim SmtpServer As New SmtpClient()
SmtpServer.Host = "smtp.gmail.com"
SmtpServer.Credentials = New Net.NetworkCredential("xxxx#gmail.com", "xxxxxx")
SmtpServer.UseDefaultCredentials = False
SmtpServer.Port = 465
SmtpServer.EnableSsl = True
SmtpServer.Timeout = 5000
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
Try
mail.From = New MailAddress("xxxxx#gmail.com")
mail.To.Add("xxxxxx#gmail.com")
SmtpServer.Send(Mail)
Catch ex As Exception
MsgBox(ex.ToString())
End Try
End Sub
You can try changing the port to 587 for gmail.
And also add in the try block
mail.Subject= ...
mail.Body= ...
(add suitable strings here)
also try running it without
SmtpServer.UseDefaultCredentials = False
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
I can add my code in C# which works if you need as I do not know VB
My code is adding a user to my database. I am trying to have my site send an email with a passcode to verify the email account is legit.
First I am trying to get it to send a basic test email. Then I plan on adding the passcode in a link to my site validating it.
My problem is my code doesn't send the basic test email.
Imports System.Data.SqlClient
Imports System.Net.Mail
Partial Class Account_Register
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
RegisterUser.ContinueDestinationPageUrl = Request.QueryString("ReturnUrl")
End Sub
Protected Sub RegisterUser_CreatedUser(ByVal sender As Object, ByVal e As EventArgs) Handles RegisterUser.CreatedUser
FormsAuthentication.SetAuthCookie(RegisterUser.UserName, False)
Dim MyMailMessage As New MailMessage()
' MyMailMessage.IsBodyHtml = True
MyMailMessage.From = New MailAddress("NAME#gmail.com")
MyMailMessage.To.Add("Name#yahoo.com")
MyMailMessage.Subject = "Email Confirmation"
MyMailMessage.Body = "TESTING"
'MyMailMessage.Body = "<html>" & RegisterUser.UserName & "Link: $" & "<br/> " & "</html>"
'Create the SMTPClient object and specify the SMTP GMail server
Dim SMTPServer As New SmtpClient("smtp.gmail.com")
SMTPServer.Port = 588
SMTPServer.Credentials = New System.Net.NetworkCredential("NAME#gmail.com", "Password")
SMTPServer.EnableSsl = True
Try
SMTPServer.Send(MyMailMessage)
'MessageBox.Show("Email Sent")
Catch ex As SmtpException
'MessageBox.Show(ex.Message)
End Try
Dim continueUrl As String = RegisterUser.ContinueDestinationPageUrl
If String.IsNullOrEmpty(continueUrl) Then
continueUrl = "~/"
End If
Response.Redirect(continueUrl)
End Sub
End Class
Any help?
Change the port number Gmail SMTP port is : 465
Use this
SMTPServer.Port = 465
So I ended up having to use Port = 587
Thank you for the help!
I have an asp.net 4.0 webpage which has 10 chart controls on it. I have been to email the chart controls to the current logged in user when they open the page. The chart controls will be different for each user. I have been testing this by trying to send 1 chart control but the body of the email doesnt show the chart only the image outline. I have tried several things but cant get it to work. The code i have just now is -
web.config
<add key="ChartImageHandler" value="storage=memory;deleteAfterServicing=true;"/>
webpage
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
SendMail()
End Sub
Private Sub SendMail()
Dim SB As New StringBuilder()
Dim SW As New StringWriter(SB)
Dim htmlTW As New HtmlTextWriter(SW)
'SB.Append("<td><img src=""cid:chart17""></td>")
Chart10.RenderControl(htmlTW)
Dim MyHTML As String = SB.ToString()
Dim from As String = "EMAIL ADDRESS"
Dim recip As String = "EMAIL ADDRESS"
'Dim recip As String = Membership.GetUser.Email.ToString
Dim subject As String = "Test Email"
'Create message object and populate w/ data from form
Dim message As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
message.From = New System.Net.Mail.MailAddress(from.Trim())
message.To.Add(recip.Trim())
message.Subject = subject.Trim()
message.IsBodyHtml = True
message.Body = MyHTML
'Setup SmtpClient to send email. Uses web.config settings.
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient()
'Error handling for sending message
Try
smtpClient.Send(message)
'Exception contains information on each failed receipient
Catch recExc As System.Net.Mail.SmtpFailedRecipientsException
For recipient = 0 To recExc.InnerExceptions.Length - 1
Dim statusCode As System.Net.Mail.SmtpStatusCode
'Each InnerException is an System.Net.Mail.SmtpFailed RecipientException
statusCode = recExc.InnerExceptions(recipient).StatusCode
If (statusCode = Net.Mail.SmtpStatusCode.MailboxBusy) Or (statusCode = Net.Mail.SmtpStatusCode.MailboxUnavailable) Then
'Log this to event log: recExc.InnerExceptions(recipient).FailedRecipient
System.Threading.Thread.Sleep(5000)
smtpClient.Send(message)
Else
'Log error to event log.
'recExc.InnerExceptions(recipient).StatusCode or use statusCode
End If
Next
'General SMTP execptions
Catch smtpExc As System.Net.Mail.SmtpException
'Log error to event log using StatusCode information in
'smtpExc.StatusCode
Catch ex As Exception
'Log error to event log.
End Try
End Sub
As you can see i have tried some examples on forums like "SB.Append" and "chart10.rendercontrol(htmlTW) but both do not work for me.
Any helpo would be greatful.
You are not attaching the image to the e-mail.
''I am not sure how to handle memory streams in vb but it should be something like so.
Dim ms as MemoryStream = new MemoryStream()
Chart10.SaveImage(ms, ChartImageFormat.Png)
Dim A As System.Net.Mail.Attachment = New System.Net.Mail.Attachment(ms, "MyChart.png")
A.ContentId = "chart17"
A.ContentDisposition.Inline = True
message.Attachments.Add(A)