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!
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.
Using the following function:
Public Sub SendMail(ByVal SendFrom As String, ByVal SendTo As String, ByVal Subject As String, ByVal Body As String)
Dim client As New SmtpClient
Dim message As New MailMessage
message.Body = Body
message.Subject = Subject
message.From = New MailAddress(SendFrom)
message.To.Add(New MailAddress(SendTo))
client.Port = "25"
client.Host = "smtp.myserver.com"
client.Send(message)
End Sub
I call it with
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs) Handles btnSubmit.Click
Dim iandamsb As New StringBuilder
iandamsb.AppendLine("Please make the following changes:")
iandamsb.AppendLine("")
iandamsb.AppendLine("Current name:" & txtCurrentName.Text)
iandamsb.AppendLine("New name:" & txtNewName.Text)
iandamsb.AppendLine("New username:" & txtNewUsername.Text)
iandamsb.AppendLine("Applications:" & txtOtherApplications.Text)
Dim iandambody As String = iandamsb.ToString
SendMail(txtRequesterEmail.Text, "ayockel#mydomain.com", "Name Change Request - " & txtCurrentName.Text, iandambody)
End Sub
It works just fine, however it is sending two emails instead of one. Can anyone figure out why it's sending a duplicate?
I would venture to guess that you have the button click event bound twice: once through an OnClick attribute in the markup:
<asp:Button OnClick="btnSubmit_Click" runat="server" ... />
and then again through the code-behind via Handles:
Protected Sub btnSubmit_Click(sender As Object, e As System.EventArgs)
Handles btnSubmit.Click
I would remove one of them if that's the case. I would keep the latter so you know that the btnSubmit_Click event is properly wired at compile time.
A discussion of this issue.
I have created test project to send message via google talk using Jabber library. As I already have test project that can send message successfully using agsXMPP, I want to imitate this project to use jabber library instead. However, there is no message sent even though the code run pass sending message command without any error. It seems that the password has not even been checked as it didn't enter OnAuthError event.
My test project is ASP.NET Web application project using VB.NET language. There are 4 textboxes to fill in: sender account (txt_Sender), sender's password (txt_Password), message to be sent (txt_Message) and receiver account (txt_Receiver) and also 1 button for sending the message (btn_Send). I test by using my email account (xxx1#gmail.com) and send message to my friend (xxx2#gmail.com). Here are my VB code
Imports jabber
Imports jabber.client
Imports Microsoft.Win32
Imports System.Threading
Imports jabber.protocol.client
Imports jabber.connection
Public Class TestSendMsg
Inherits System.Web.UI.Page
Public done As ManualResetEvent
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
done = New ManualResetEvent(False)
End Sub
Private Sub btn_Send_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btn_Send.Click
Dim jcSender As JabberClient = New JabberClient()
Dim jidSender As New jabber.JID(txt_Sender.Text.Trim)
With jcSender
.User = jidSender.User
.Password = txt_Password.Text.Trim
.Server = jidSender.Server
.AutoReconnect = True
.AutoRoster = True
End With
With jcSender
Try
AddHandler .OnAuthenticate, New bedrock.ObjectHandler(AddressOf j_OnAuthenticate)
'AddHandler .OnAuthenticate, AddressOf j_OnAuthenticate
AddHandler .OnPresence, AddressOf j_OnPresence
AddHandler .OnBeforePresenceOut, AddressOf j_OnBeforePresenceOut
AddHandler .OnAuthError, AddressOf j_OnAuthError
AddHandler .OnAfterPresenceOut, AddressOf j_OnAfterPresenceOut
.Connect()
.Login()
.IsAuthenticated = True
.Message(txt_Reciever.Text.Trim, txt_Message.Text.Trim)
Catch ex As Exception
MsgBox(ex.Message)
End Try
.Close()
.Dispose()
End With
End Sub
Private Sub j_OnAfterPresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthError(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnBeforePresenceOut(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Registered: " & iq.BaseURI)
'done.Set()
End Sub
Private Sub j_OnPresence(ByVal sender As Object, ByVal pres As Presence)
'Dim j As JabberClient = CType(sender, JabberClient)
'j.Message(TARGET, "Presence: " & pres.BaseURI)
'done.Set()
End Sub
Private Sub j_OnAuthenticate(ByVal sender As Object)
' Sender is always the JabberClient.
Dim j As JabberClient = CType(sender, JabberClient)
j.Message(txt_Reciever.Text.Trim, "Test OnAuthenticate")
' Finished sending. Shut down.
done.Set()
End Sub
End Class
You need to wait for OnAuthenticate before sending your message.
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))