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.
Related
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'm currently trying to open a PDF file on my website that is located on my company's network. I had this working previously, but now for some reason it isn't working. Here is what I have:
I am using impersonation to access the file. It has domain admin privileges. This is from my web.config file (username/password are altered):
<identity impersonate="true" password="pass" userName="domain\user" />
I use this code to open the PDF in a window:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
strPath = "file://san01/prodeng/" + Mid(strPath, 4)
strPath = Replace(strPath, "\", "/")
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
This doesn't work. It redirects me to the "DrawingError.aspx" page. This link dispalys the "Session("DWGPath")" variable. I can take this variable and paste it in to my browser and the PDF opens without problem.
However, if I alter my code to this:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Try
Dim strPath As String = CStr(Session("DWGPath"))
Dim pdfPath As String = strPath
Dim client As WebClient = New WebClient()
Dim buffer As Byte() = client.DownloadData(pdfPath)
Response.ContentType = "application/pdf"
Response.AddHeader("content-length", buffer.Length.ToString())
Response.BinaryWrite(buffer)
Response.End()
Catch exa As WebException
Response.Redirect("DrawingError.aspx")
Catch ex As Exception
Throw ex
End Try
End Sub
It still doesn't work.
The account also has full control privileges to the folder that contains the PDFs.
Any help or insight would be appreciated. Thank you!
EDIT: IF I throw exa then I get this:
The account used is a computer account. Use your global user account or local user account to access this server.
I assume you're running IIS. Go to the application pool for this app and change the identity that it's running under to be the domain\user account. See if that fixes your problem.
You want to make sure that the password on this account doesn't change or else it will fail when the password expires.
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
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!
Im trying to send a Docx file via this form that I made, the email sends fine.
but the docx file gets back corrupted..
this is my backgroudn code:
'Add the namespace for the email-related classes
Imports System.Net.Mail
Partial Class SendAttachment
Inherits System.Web.UI.Page
Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SendEmail.Click
'Make sure a file has been uploaded
If String.IsNullOrEmpty(AttachmentFile.FileName) OrElse AttachmentFile.PostedFile Is Nothing Then
Throw New ApplicationException("Egad, a file wasn't uploaded... you should probably use more graceful error handling than this, though...")
End If
' UPDATE THIS VALUE TO YOUR EMAIL ADDRESS
Const ToAddress As String = "pelleg#shakuff.co.il"
'(1) Create the MailMessage instance
Dim mm As New MailMessage(UsersEmail.Text, ToAddress)
'(2) Assign the MailMessage's properties
mm.Subject = "שלוחת קורות חיים"
mm.Body = Body.Text
mm.IsBodyHtml = False
'Attach the file
mm.Attachments.Add(New Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName))
'(3) Create the SmtpClient object
Dim smtp As New SmtpClient
'(4) Send the MailMessage (will use the Web.config settings)
smtp.Send(mm)
'Show the EmailSentForm Panel and hide the EmailForm Panel
EmailSentForm.Visible = True
EmailForm.Visible = False
End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
'On the first page load, hide the EmailSentForm Panel
EmailSentForm.Visible = False
End If
End Sub
End Class
its the sendemail.aspx.vb file.
any suggestions?
totally off the top of my head, but try setting:
AttachmentFile.PostedFile.InputStream.Position = 0
before you call:
'Attach the file mm.Attachments.Add(New Attachment(AttachmentFile.PostedFile.InputStream, AttachmentFile.FileName))