I'm developing web pages using Razor C# language in WebMatrix. I have a hosted website, I'm trying to incorporate an email system into it.
As per this article on WebMail I have set up the WebMail settings in my _AppStart.cshtml page. I've got my settings from my service provider. He's provided me a sample code using CDO object:
dim config, sch
set config = CreateObject("CDO.Configuration")
sch = "http://schemas.microsoft.com/cdo/configuration/"
with config.Fields
.item(sch & "sendusing") = 2 ' cdoSendUsingPort
.item(sch & "smtpserver") = "myserver"
.item(sch & "smtpserverport") = 25
.item(sch & "smtpusessl") = False
.item(sch & "smtpconnectiontimeout") = 60
.item(sch & "smtpauthenticate") = 1 'basic auth
.item(sch & "sendusername") = "myemail#email.com"
.item(sch & "sendpassword") = "password"
.update
end with
Set myMail=CreateObject("CDO.Message")
With myMail
.Configuration = config
.Subject = Request.Form("txtSubject")
.From = Request.Form("txtFrom")
.To = Request.Form("txtTo")
.TextBody = Request.Form("txtMessage")
Call .Send()
End With
As you can see the above code is made in CDO. I'm trying to use the WebMail in Razor. The only point where I'm stuck is that my email server is not SSL but requires basic auth. I can't find any authentication setting in WebMail. How do I set the SMTP authentication in WebMail? This is my current code:
WebMail.SmtpServer = "myserver";
WebMail.SmtpPort = 25;
WebMail.EnableSsl = false;
WebMail.UserName = "myemail#email.com";
WebMail.Password = "password";
WebMail.From = "Support <myemail#email.com>";
Thanks in advance!
Basic authentication with mail servers usually consists of providing a user name and password. You set those using the WebMail.UserName and WebMail.Password properties.
By the way, your provider has given you sample code for sending mail using CDO in classic ASP. It is of no use to you.
Here's a basic example in c#. The Smtp class takes username password.
MailMessage mail = new MailMessage("emailfrom","emailto");
mail.From = new MailAddress("emailfrom");
mail.Subject = txtsbjct.Text;
string Body = txtmsg.Text;
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "localhost";
smtp.Credentials = new System.Net.NetworkCredential
("youremail", "yourpassword");
Related
I have a ASP.NET web form for sending emails using SMTP. The hosting provider is 1and1. It worked for year. It stopped working several weeks ago. I receive the following error:
"The SMTP server requires a secure connection or the client was not authenticated. The server response was: Authentication required"
1and1 support is unable to explain the reason for the change. I would appreciate your help (see the VB.NET code below).
I tried to change the port to 465 and 587, as well, I tried to change "EnableSsl" flag to true state - nothing worked.
I wrote another code that works, but it allows to send an email with my email address in the "from" field (see the code below). But I would like to send an email with visitors' address in the "from" field.
' code that worked once
Dim mailMessage As New MailMessage()
Dim MailAddress As String
Try
'creating an instance of the MailMessage class
mailMessage.From = New MailAddress(txtEmail.Text)
'senders email address
mailMessage.To.Add("me#mysite.net")
'recipient's email address
mailMessage.Subject = "Form Submission: " & txtSubject.Text
'subject of the email message
mailMessage.IsBodyHtml = False
'message text format. Can be text or html
mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
Dim smtp As New SmtpClient("smtp.ionos.com")
smtp.Port = 25
smtp.EnableSsl = False
smtp.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
Catch ex As Exception
lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me#mysite.net"
End Try
' code that is working today, but my email address must be in the "from" field
Dim mailMessage As New MailMessage()
Dim MailAddress As String
Try
'creating an instance of the MailMessage class
mailMessage = New MailMessage()
mailMessage.From = New MailAddress("me#mysite.net")
'senders email address
mailMessage.To.Add("me#mysite.net")
'recipient's email address
mailMessage.Subject = "Form Submission: " & txtSubject.Text
'subject of the email message
mailMessage.IsBodyHtml = False
'message text format. Can be text or html
mailMessage.Body = txtName.Text & vbCr & txtEmail.Text
'message body
mailMessage.Priority = MailPriority.Normal
'email priority. Can be low, normal or high
Dim smtp As New SmtpClient'("smtp.ionos.com")
smtp.UseDefaultCredentials = False
smtp.Credentials = New Net.NetworkCredential("me#mysite.net", "MyPassword")
smtp.Host = "smtp.ionos.com"
smtp.Port = 587
smtp.EnableSsl = True
smtp.Send(mailMessage)
'using the static method "Send" of the SmtpMail class to send the mail
Response.Write("Mail sent")
Catch ex As Exception
lblError.Text = "Form processing error occurred." & vbCr & "Please send your request to: me#mysite.net"
End Try
This question already has answers here:
Gmail Error :The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
(21 answers)
Closed 7 years ago.
hi i know this question is a bit redundant but i am experiencing it differently here is my code on sending email after creation of account on asp.net
Dim newreg As MembershipUser = Membership.GetUser(context.Request("username"))
Dim newid As Guid
If newreg.ProviderUserKey IsNot Nothing Then
newid = DirectCast(newreg.ProviderUserKey, Guid)
End If
Dim body As String = String.Empty
Dim reader As StreamReader = New StreamReader(context.Server.MapPath("~/Account/email.htm"))
body = reader.ReadToEnd
body = body.Replace("{UserName}", context.Request("username").ToString)
body = body.Replace("{Url}", "http://wwww.123.com/Account/activate.aspx?id=" & context.Request("username").ToString & "&usertype=" & givetype(context.Request("username")) & "&actid=" & newid.ToString)
Dim mailMessage As MailMessage = New MailMessage
mailMessage.From = New MailAddress(ConfigurationManager.AppSettings("UserName"))
mailMessage.Subject = "Account Activation"
mailMessage.Body = body
mailMessage.IsBodyHtml = True
mailMessage.To.Add(New MailAddress(context.Request("email").ToString))
Dim smtp As SmtpClient = New SmtpClient
smtp.Host = ConfigurationManager.AppSettings("Host")
smtp.EnableSsl = True
Dim NetworkCred As System.Net.NetworkCredential = New System.Net.NetworkCredential
NetworkCred.UserName = ConfigurationManager.AppSettings("UserName")
NetworkCred.Password = ConfigurationManager.AppSettings("Password")
smtp.UseDefaultCredentials = False
smtp.Credentials = NetworkCred
smtp.Port = Integer.Parse(ConfigurationManager.AppSettings("Port"))
smtp.Send(mailMessage)
the odd thing is when i run the website on localhost gmail allows it but when i run the website on a vps it rejects and gives this error
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.5.1 Authentication Required
any help will do tnx
got it working the problem was gmail is blocking the request to send the email because of the 2 step verification i created a password for the app and now gmail is not blocking the request to send e-mails hope this helps if somebody will encounter the same issue with gmail
Here is the codes:
Dim SmtpServer As New SmtpClient()
Dim mail As New MailMessage()
Dim attachment As System.Net.Mail.Attachment
SmtpServer.Credentials = New _
Net.NetworkCredential("administrator#company.com", "1234")
SmtpServer.Port = 25
SmtpServer.Host = "SmtpServer"
mail = New MailMessage()
mail.From = New MailAddress("user#company.com.my")
mail.To.Add("recipient#external.com")
mail.CC.Add("user1#company.com")
mail.CC.Add("user2#company.com")
mail.Headers.Add("Disposition-Notification-To", "user1#company.com") 'Read receipt
mail.DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure 'Fail delivery notification
mail.Subject = "Sending Documents"
mail.Body = "To Whom May Concern," & vbCrLf _
& vbCrLf & "Please refer to the attachment for the documents." & vbCrLf & _
"NOTE : This is an automatically generated email and will be sent daily."
For Each path As String In attch
attachment = New System.Net.Mail.Attachment(path)
mail.Attachments.Add(attachment)
Next
Try
SmtpServer.Send(mail)
SmtpServer = Nothing
Catch ex As Exception
Response.Write(ex.ToString)
Exit Sub
End Try
Problem is only internal email receiving, not external email.
No errors shown during the code execution.
Any idea on how to solve this?
Or do I need to configure something at the Microsoft Exchange Server?
Also the server using the MailMarshal to do the filtering.
An advanced thanks for the contributing feedback.
Make sure that "mail.From" value is in the list allowed by SMTP server. It could be that relaying is not allowed.
I am sick and tired of my hosting company. I think you can help me here.
I have simple need that I have my complete website written in Classic ASP. Now I want to have a page which sends email using CDOSYS. I wanted to have that script in ASP from my hosting company that what setting should i have. They always send me code in C#.
here is they send always:
CDOSYS is part of the System.Web.Mail namespace and is installed by default on Windows 2000 and Windows XP platforms. It replaces CDONTS for sending SMTP email messages and can be used with our IIS 6 and IIS 7 Windows hosting accounts. The following code sample demonstrates how to create, format, and send email.
private void SendEmail()
{
const string SERVER = "relay-hosting.secureserver.net";
MailMessage oMail = new System.Web.Mail.MailMessage();
oMail.From = "emailaddress#domainname";
oMail.To = "emailaddress#domainname";
oMail.Subject = "Test email subject";
oMail.BodyFormat = MailFormat.Html; // enumeration
oMail.Priority = MailPriority.High; // enumeration
oMail.Body = "Sent at: " + DateTime.Now;
SmtpMail.SmtpServer = SERVER;
SmtpMail.Send(oMail);
oMail = null; // free up resources
}
Here is my ASP which I written to use same thing but doesn't work:
Set objCDOSYSMail = Server.CreateObject("CDO.Message")
Set objCDOSYSCon = Server.CreateObject ("CDO.Configuration")
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "relay-hosting.secureserver.net"
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objCDOSYSCon.Fields("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objCDOSYSCon.Fields.Update
With objCDOSYSMail
.To = strEmail
.BCc = "a#gmail.com"
.Cc = "b#gmail.com"
.From = "sales#b.com"
.Subject = "Thank you!"
.HTMLBody = "Hello<br></br><h3>Thank you for your enquiry. <br/>"
Set objCDOSYSMail = Nothing
Set objCDOSYSCon = Nothing
I don't know how to use their code.... please help
You can use the following code in a classic ASP page to send email with CDOSYS:
Set objMail = Server.CreateObject("CDO.Message")
Set objConf = Server.CreateObject("CDO.Configuration")
Set objFlds = objConf.Fields
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 'cdoSendUsingPort
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.your-site-url.com" 'your smtp server domain or IP address goes here
objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 'default port for email
'uncomment next three lines if you need to use SMTP Authorization
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "your-username"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "your-password"
'objFlds.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1 'cdoBasic
objFlds.Update
objMail.Configuration = objConf
objMail.FromName = "Your Name"
objMail.From = "your#address.com"
objMail.To = "destination#address.com"
objMail.Subject = "Email Subject Text"
objMail.TextBody = "The message of the email..."
objMail.Send
Set objFlds = Nothing
Set objConf = Nothing
Set objMail = Nothing
I use CDONTS when I need to send up send email form on websites. Recently changed my hosting company to godaddy. I realized my send email form gives "permission denied" error. I called GoDaddy support. They told me I should use relay server "relay-hosting.secureserver.net" in my codes. I thought relay server used only with CDO.
How can I include it to my codes?
My codes
Set objEmail = Server.CreateObject("CDONTS.NewMail")
objEmail.To = MailTo
objEmail.Cc = MailCc
objEmail.From = MailFrom
objEmail.Subject = MailSubject
objEmail.Body = MailBody
objEmail.Send
Set objEmail = nothing
A web search for "cdonts relay" yielded this example which relates directly to your issue with GoDaddy.com - Based on everything I've read, this only works with CDO.Message instead of CDONTS.NewMail.
UPDATE: Here's the modified code
Set objMail = Server.CreateObject("CDO.Message")
objMail.From = MailFrom
objMail.To= MailTo
objMail.Subject = MailSubject
objMail.TextBody = MailBody
objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "relay-hosting.secureserver.net"
objMail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMail.Configuration.Fields.Update
objMail.Send
set ojbMail = nothing