From Email Name in ASP.NET - asp.net

I am trying to send email from X#gmail.com to Y#yahoo.com using System.Net.Mail,
Below is my code
Dim message As MailMessage = New MailMessage()
message.From = New MailAddress(Me.txtFromEmail.Text, Me.txtFromName.Text)
message.Sender = New MailAddress(Me.txtFromEmail.Text, Me.txtFromName.Text)
message.ReplyToList.Add(New MailAddress(Me.txtFromEmail.Text))
message.To.Add(New MailAddress(Me.txtTo.Text))
message.Subject = Me.txtSubject.Text & " " & Now.ToString
message.Body = Me.txtMessage.Text
If Me.txtCC.Text <> "" Then message.CC.Add(Me.txtCC.Text)
If Me.txtBCC.Text <> "" Then message.Bcc.Add(txtBCC.Text)
Dim mclient As SmtpClient = New SmtpClient()
mclient.Host = "smtp.gmail.com"
mclient.Port = 587
mclient.EnableSsl = True
mclient.Credentials = New System.Net.NetworkCredential("email#myapp.com", "somepassword")
mclient.Send(message)
Above code sends email without any error....
Issues: At the recipient inbox it listed as Sender Name <email#myapp.com> instead of Sender Name <x#gmail.com>
How can I set it as Sender Name <x#gmail.com>

Related

Send email failed conversion from string "" to type 'Boolean' is not valid

Imports System.Net.Mail
Partial Class ch5_proj4_ch5_proj4
Inherits System.Web.UI.Page
Protected Sub EmailBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles EmailBtn.Click
' insert code here
Dim MySmtpClient As New SmtpClient()
Dim MM As New MailMessage()
Try
MySmtpClient.Host = "stmp.gmail.com"
MySmtpClient.Port = 587
MySmtpClient.EnableSsl = True
MySmtpClient.UseDefaultCredentials = True
MySmtpClient.Credentials = New System.Net.NetworkCredential()
'mail message
MM.To.Add(New MailAddress(TextBox2.Text, TextBox1.Text))
Dim fromAddress As New MailAddress(TextBox4.Text, TextBox3.Text)
MM.From = fromAddress
MM.Subject = TextBox4.Text
MM.IsBodyHtml = RadioButtonList1.SelectedValue
MM.Body = txtMessage.Text
MySmtpClient.Send(MM)
Label5.Text = "Email successfully sent."
Catch exc As Exception
Label5.Text = "Send email failed" + exc.Message
End Try
MM = Nothing
MySmtpClient = Nothing
End Sub
End Class
What am I doing wrong? Every time I do it I have an error
"Send email failed Conversion from string "" to type 'Boolean' is not valid."
Please help.
Try to change this MM.IsBodyHtml = RadioButtonList1.SelectedValue to this MM.IsBodyHtml = If(RadioButtonList1.SelectedValue = "True",True,False)

error with sending mail vb.net

i'm having a problem with sending mail. my code is so easy as below.
when this code starts it gives the error:
Request for the permission of type 'System.Net.Mail.SmtpPermission,
System, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089' failed.
when i searched for this error, they recommend changing web.config trust full setting or port from 587 to 25 and other stuff, but none work. what else could it be?
Sub MailGonder()
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Host = "mail.excelinefendisi.com"
SmtpServer.Credentials = New Net.NetworkCredential("info#excelinefendisi.com", "mypassword")
SmtpServer.Port = 587
mail.From = New MailAddress("info#excelinefendisi.com")
mail.To.Add("volkan.yurtseven#hotmail.com")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP"
SmtpServer.EnableSsl = True
SmtpServer.Send(mail)
End Sub
Use this code, work perfect for me :
Dim iphost As IPHostEntry = Dns.GetHostByName(Dns.GetHostName())
Dim Emailmessage As New MailMessage()
Emailmessage.From = New MailAddress("from_Email#hotmail.com")
Emailmessage.To.Add("To_Email#hotmail.com")
Emailmessage.Subject = "tTest Mail"
Emailmessage.Body = ("This is for testing SMTP")
Dim smtp As New SmtpClient("smtp.live.com")
smtp.Port = 587
smtp.EnableSsl = True
smtp.Credentials = New System.Net.NetworkCredential("your_email#hotmail.com.com", "Password")
smtp.Send(Emailmessage)

Creating User twice in sql server for new registration

I'm trying to create a new user and everything works fine but it enters the same records twice to my db. I have already tried to kept some breakpoints but couldn't find where am I going wrong.
This is my Registration.aspx code:
Protected Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
Dim msg As MailMessage
Dim UserID As String
Dim ActivationUrl As String = String.Empty
Dim emailId As String = String.Empty
UserID = Guid.NewGuid.ToString
'Create ConnectionString and Inser Statement
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
Dim insertSql As String = "INSERT INTO Users (UserID,UserName,Password,Email,Mobile,Address,CreatedDate)" & " values (#UserID,#UserName,#Password,#Email,#Mobile,#Address,#CreatedDate)"
'Create SQL connection
Dim con As New SqlConnection(connectionString)
'Create SQL Command And Sql Parameters
Dim cmd As New SqlCommand(insertSql, con)
Dim usernumber As New SqlParameter()
usernumber.ParameterName = "#UserID"
usernumber.Value = UserID
cmd.Parameters.Add(usernumber)
Dim username As New SqlParameter()
username.ParameterName = "#Username"
username.Value = txtUserName.Text.ToString()
cmd.Parameters.Add(username)
Dim password As New SqlParameter()
password.ParameterName = "#Password"
password.Value = txtPassword.Text.ToString()
cmd.Parameters.Add(password)
Dim email As New SqlParameter()
email.ParameterName = "#Email"
email.Value = txtEmail.Text.ToString()
cmd.Parameters.Add(email)
Dim mobile As New SqlParameter()
mobile.ParameterName = "#Mobile"
mobile.Value = txtMobile.Text.ToString()
cmd.Parameters.Add(mobile)
Dim address As New SqlParameter()
address.ParameterName = "#Address"
address.Value = txtAddress.Text.ToString()
cmd.Parameters.Add(address)
Dim createddate As New SqlParameter()
createddate.ParameterName = "#CreatedDate"
createddate.Value = Date.Now.ToString("MM/dd/yyyy hh:mm:ss tt")
cmd.Parameters.Add(createddate)
Try
con.Open()
cmd.ExecuteNonQuery()
lblMsg.Text = "User Registration successful"
Catch ex As SqlException
Dim errorMessage As String = "Error in registering user"
errorMessage += ex.Message
Throw New Exception(errorMessage)
Finally
con.Close()
End Try
Try
'Sending activation link in the email
msg = New MailMessage()
Dim smtp As New SmtpClient()
emailId = txtEmail.Text.Trim()
'sender email address
msg.From = New MailAddress("voletykiran#gmail.com")
'Receiver email address
msg.[To].Add(emailId)
msg.Subject = "Confirmation email for account activation"
'For testing replace the local host path with your lost host path and while making online replace with your website domain name
ActivationUrl = Server.HtmlEncode("http://localhost:8769/UserRegistration/ActivateAccount.aspx?UserID=" & FetchUserId(emailId) & "&Email=" & emailId)
msg.Body = "Hi " & txtUserName.Text.Trim() & "!" & vbLf & "Thanks for showing interest and registring in <a href='http://www.webcodeexpert.com'> webcodeexpert.com<a> " & " Please <a href='" & ActivationUrl & "'>click here to activate</a> your account and enjoy our services. " & vbLf & "Thanks!"
msg.IsBodyHtml = True
smtp.Credentials = New NetworkCredential("voletykiran#gmail.com", "india#1a")
smtp.Port = 587
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
smtp.Send(msg)
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Confirmation Link to activate account has been sent to your email address');", True)
Catch ex As Exception
ScriptManager.RegisterStartupScript(Me, Me.[GetType](), "Message", "alert('Error occured : " & ex.Message.ToString() & "');", True)
Return
Finally
ActivationUrl = String.Empty
emailId = String.Empty
con.Close()
End Try
End Sub
Private Function FetchUserId(emailId As String) As String
Dim cmd As New SqlCommand()
cmd = New SqlCommand("SELECT UserID FROM users WHERE Email=#Email", con)
cmd.Parameters.AddWithValue("#Email", emailId)
If con.State = ConnectionState.Closed Then
con.Open()
End If
Dim UserID As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
cmd.Dispose()
Return UserID
End Function
Private Sub clear_controls()
txtUserName.Text = String.Empty
txtPassword.Text = String.Empty
txtConfirmPassword.Text = String.Empty
txtEmail.Text = String.Empty
txtMobile.Text = String.Empty
txtAddress.Text = String.Empty
txtUserName.Focus()
End Sub
Can anyone say me where am I misleading?

Sending an e-mail in asp.net using vb

error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required. Learn more at
I tried all things that i can do for this error ,but unable to solve this. Please help!!
My .net code for sending e-mail is as follows:
Try
Dim oMsg As MailMessage = New MailMessage()
Dim sendr, receivr As MailAddress
sendr = New MailAddress("username#gmail.com")
receivr = New MailAddress(TextBox4.Text)
oMsg.From = sendr
oMsg.To.Add(receivr)
oMsg.Subject = "Asp.net testing"
oMsg.IsBodyHtml = True
oMsg.Body = "Hi everyOne"
'oMsg.BodyFormat = MailFormat.Html
'oMsg.Body = "<HTML><BODY><B>Dear " & TextBoxName.Text & "</B></br></br>Congratulations! You have been successfully registered.</br></br>"
'oMsg.Body = oMsg.Body & "Your Username is: " & TextBox1.Text & "</br>Your Password is: " & TextBox2.Text & " </br>Regards,</br>team SMVDU.</BODY></HTML>"
Dim client As SmtpClient = New SmtpClient()
Dim cred As System.Net.NetworkCredential = New System.Net.NetworkCredential("username#gmail.com", "*******", "smtp.gmail.com")
client.Credentials = cred
client.Host = "smtp.gmail.com"
client.Port = 587
client.DeliveryMethod = SmtpDeliveryMethod.Network
client.EnableSsl = True
client.UseDefaultCredentials = False
client.Send(oMsg)
Response.Write("<script>alert('E-mail sent')</script>")
Catch ex As Exception
Response.Write("" & ex.Message)
End Try
I am using windows 7 home basic. And all things that can be given is correct i.e email, password from my side.

Getting an error when sending email through Gmail SMTP

Basically I have attempted to send an email when a button is pressed.
With the following code, I get an error that says something about 'The SMTP server requires a secure connection or the client was not authenticated'.
What is causing this error?
Imports System.Net.Mail
Private Sub Button1_Click_2(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
SmtpServer.Credentials = New _
Net.NetworkCredential("MYEMAIL#gmail.com", "MYPASSWORD")
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False
SmtpServer.Port = 587
SmtpServer.Host = "smtp.gmail.com"
mail = New MailMessage()
mail.From = New MailAddress("MYEMAIL")
mail.To.Add("SENDINGADRESS")
mail.Subject = "Test Mail"
mail.Body = "This is for testing SMTP mail from GMAIL"
SmtpServer.Send(mail)
MsgBox("mail send")
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End Sub
You're very close, you need to set the following properties as well.
SmtpServer.EnableSsl = True
SmtpServer.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpServer.UseDefaultCredentials = False

Resources