Authenticated mail sending not working in .NET - asp.net

I have a script sending mail from my .net application.
My hosting provider requires the emails to be authenticated if I don't want to cause delays in receiving them (currently delay is almost a day from sending).
I have build mail sending script with authentication but it doesn't work - it doesn't produce any error or exception but the emails are still not received instantly.
In the other hand similar script made in classic asp works and I receive mails instantly.
Am I missing something?
This is asp script (which works):
<%
Set objEmail = CreateObject("CDO.Message")
objEmail.From = "sender#mydomain.com"
objEmail.To = "recipient#gmail.com"
objEmail.Subject = "Test Mail"
objEmail.Textbody = "Test Mail"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.mydomain.com"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = "1"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myusername"
objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "mypassword"
objEmail.Configuration.Fields.Update
objEmail.Send
If Not err.number=0 Then
Response.write "ERROR: " & err.Description
err.Clear
end if
%>
This is .NEt (which doesn't work):
public static void SendEmail(string senderName, string senderEmail, string recipient, string comments, string subject, bool inTemplate)
{
MailAddress from = new MailAddress(senderEmail);
MailAddress to = new MailAddress(recipient);
MailMessage mail = new MailMessage(from, to);
mail.ReplyToList.Add(senderEmail);
mail.Subject = subject;
mail.IsBodyHtml = true;
mail.Body = comments;
Configuration configurationFile = WebConfigurationManager.OpenWebConfiguration(HttpContext.Current.Request.ApplicationPath);
MailSettingsSectionGroup mailSettings = configurationFile.GetSectionGroup("system.net/mailSettings") as MailSettingsSectionGroup;
NetworkCredential basicCredentials = new NetworkCredential(mailSettings.Smtp.Network.UserName, mailSettings.Smtp.Network.Password);
SmtpClient smtpClient = new SmtpClient();
smtpClient.Host = mailSettings.Smtp.Network.Host;
smtpClient.Port = mailSettings.Smtp.Network.Port;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = basicCredentials;
try
{
smtpClient.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
These are my web.config settings:
<system.net>
<mailSettings>
<smtp>
<network host="mail.mydomain.com" userName="myusername" password="mypassword" port="25" />
</smtp>
</mailSettings>
</system.net>
The web.config settings are correct but even if I input server settings manually into the class - it doesn't work.
Thanks

Try this:
MailMessage mailMsg = new MailMessage();
mailMsg.To.Add("test#hotmail.com");
// From
MailAddress mailAddress = new MailAddress("you#hotmail.com");
mailMsg.From = mailAddress;
// Subject and Body
mailMsg.Subject = "subject";
mailMsg.Body = "body";
// Init SmtpClient and send on port 587 in my case. (Usual=port25)
SmtpClient smtpClient = new SmtpClient("mailserver", 587);
System.Net.NetworkCredential credentials =
new System.Net.NetworkCredential("username", "password");
smtpClient.Credentials = credentials;
smtpClient.Send(mailMsg);

It turned out that the problem was at the hosting provider side. Not sure what they did but it eventually started to work.

Related

Operation has timed out error on ASP.NET

I'm getting an error while sending email through asp.net SMTP server. It says operation has timed out. How to overcome the exception?
try
{
MailMessage mail = new MailMessage();
SmtpClient SmtpServer = new SmtpClient();
mail.From = new MailAddress("mymailid#hotmail.com");
mail.To.Add("yourmailid#gmail.com");
mail.Subject = "Verify your account";
mail.Body = "Your OTP for your account verification is "+OTP.ToString()+". Enter your OTP in verification window.";
mail.IsBodyHtml = true;
SmtpServer.UseDefaultCredentials = false;
SmtpServer.Timeout = 20000;
SmtpServer.Send(mail);
errmsgShow.Text = "Mail sent";
}
catch (Exception e)
{
//string errMsg = "alert('OTP sending failed. Error: " +e.ToString()+ "')";
//ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", errMsg, true);
//Console.WriteLine(e.Message.ToString());
errmsgShow.Text = e.ToString();
}
I have also included below in the web.config
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="smtp.live.com" port="25" userName="mymailid#hotmail.com" password="password" enableSsl="true"/>
</smtp>
</mailSettings>
</system.net>
Try removing SmtpServer.Timeout = 20000;.
Update:
i see you have SmtpClient SmtpServer = new SmtpClient();.
SmtpServer is a property of SmtpMail SmtpMail.SmtpServer. Try changing the variable name, something like
SmtpClient mailClient = new SmtpClient();

Unable to send in asp.net application

I want to send an e-mail, but it shows the following error:
try
{
string filename = Server.MapPath("~/App_Data/FeedBack.txt");
string mailbody = System.IO.File.ReadAllText(filename);
mailbody = mailbody.Replace("##Name##", txtName.Text);
mailbody = mailbody.Replace("##Email##",txtEmail.Text);
mailbody = mailbody.Replace("##contact##",txtContact.Text);
mailbody = mailbody.Replace("##Commnect##",txtSuggestion.Text);
MailMessage myMessage = new MailMessage();
myMessage.Subject = "Responce from website";
myMessage.Body = mailbody;
myMessage.From = new MailAddress("amitverma0511#gmail.com","Amit Kumar");
myMessage.To.Add(new MailAddress("amitverma1150#gmail.com", "Amit Verma"));
SmtpClient mysmptclient = new SmtpClient("localhost");
mysmptclient.Host = "smtp.gmail.com";
mysmptclient.Port = 587;
mysmptclient.Credentials = new System.Net.NetworkCredential("amitverma0511#gmail.com", "Myemailpassword");
mysmptclient.Send(myMessage);
formatTable.Visible = false;
lblMail.Text = "Your Feedback has been sent successfully";
lblMail.ForeColor = Color.Green;
}
catch(Exception expt)
{
lblMail.Text = "unable to sent the feedback .Check your smpt server"+expt.Message;
lblMail.ForeColor = Color.Red;
}
My web.config file looks like this:
<system.net>
<mailSettings>
<smtp from="amitverma0511#gmail.com">
<network host="smtp.gmail.com" port='587' password="myemailpass" userName="amitverma0511#gmail.com"/>
</smtp>
</mailSettings>
</system.net>
As far as I know, you are overwriting your web.config settings by using localhost (your IIS) as a SMTP Server.
If you have not configured your IIS to serve as a SMTP server this naturally will not work.
But, if you want to use localhost and not the google servers just use this 3 lines:
SmtpClient MySmtpClient = new SmtpClient();
MySmtpClient.UseDefaultCredentials = true;
MySmtpClient.Send(mailMessage);
You should remove the string localhost from SmtpClient mysmptclient = new SmtpClient("localhost"); and it should work, if all your other settings are correct.
Further information can be found here.

Send email using asp.net

I want to send email using asp.net, I have configured my SMTP service and added 127.0.0.1 as relay. I used to my home the application was success but I installed on the office it cannot send email why?
here is the C# code
MailMessage objemail = new MailMessage();
objemail.To.Add(new MailAddress("apthodisiac#gmail.com"));
objemail.From = new MailAddress(Request.Form["inputEmail"].ToString());
objemail.Subject = Request.Form["inputSubject"].ToString();
objemail.Body = "Dari: " + Request.Form["inputName"].ToString() + "\n\n" +
"Phone: " + Request.Form["inputPhone"].ToString() + "\n\n" +
Request.Form["inputMsg"].ToString();
objemail.IsBodyHtml = true;
objemail.Priority = MailPriority.Normal;
SmtpClient objSmtpClient = new SmtpClient();
objSmtpClient.Send(objemail);
here is the web.config configuration
<system.net>
<mailSettings>
<smtp deliveryMethod="Network">
<network host="127.0.0.1" port="25" userName="yyyy" password="xxxxx" />
</smtp>
</mailSettings>
</system.net>
My office use proxy, is that the problem I cannot send email? please advice
May be port number is wrong try with port number "587" something as shown below:
private void SendEmail(string from, string to, string subject, string body)
{
MailMessage mail = new MailMessage(new MailAddress(from), new MailAddress(to));
mail.Subject = subject;
mail.Body = body;
SmtpClient smtpMail = new SmtpClient("smtp.gmail.com");
smtpMail.Port = 587;
smtpMail.EnableSsl = true;
smtpMail.Credentials = new NetworkCredential("xxx#gmail.com", "xxx");
// and then send the mail
smtpMail.Send(mail);
}
you can try this:
objSmtpClient.EnableSsl = true

asp.net send mail in vb.net

This is web config
<appSettings>
<add key="SmtpServer" value="gmail.com"/>
<add key="SmtpUtilisateur" value="superman#gmail.com"/>
<add key="SmtpPassword" value="12345678"/>
</appSettings>
This my vb method
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage
Dim utilisateur As String
Dim pass As String
Dim server As String
utilisateur = ConfigurationSettings.AppSettings("StmpUtilisateur")
pass = ConfigurationSettings.AppSettings("SmtpPassword")
server = ConfigurationSettings.AppSettings("SmtpServer")
Message.From = "superman#gmail.com"
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1")
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", utilisateur)
Message.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtppassworld", pass)
SmtpMail.SmtpServer = server
Try
SmtpMail.Send(Message)
Catch ex As Exception
Label1.Text = ex.Message
End Try
End Sub
I get an error like the "transport fail in connection to server"
I don't know why this is not work well...
Thank's for helping me!
This in vb.net
First, it is recommended to use System.Net.Mail instead of SmtpMail, since the latter has been declared obsolete by Microsoft.
Second, the Gmail SMTP server requires a secure connection which can be set using SmtpClient.EnableSsl.
Your example could be changed to the following:
Sub SendSimpleMail()
Dim utilisateur As String = ConfigurationSettings.AppSettings("StmpUtilisateur")
Dim pass As String = ConfigurationSettings.AppSettings("SmtpPassword")
Dim server As String = ConfigurationSettings.AppSettings("SmtpServer")
Dim Message As New Mail.MailMessage()
Message.From = "superman#gmail.com"
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
' You won't need the calls to Message.Fields.Add()
' Replace SmtpMail.SmtpServer = server with the following:
Dim client As New SmtpClient(server)
client.Port = 587
client.EnableSsl = true
client.Credentials = new System.Net.NetworkCredential(utilisateur,pass);
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
If you replace the appsettings in the web.config with the following specific block, the SmtpClient will automatically configure itself accordingly:
<system.net>
<mailSettings>
<smtp from="superman#gmail.com">
<network host="smtp.gmail.com"
password="12345678"
userName="superman#gmail.com"
enableSsl="true"
port=587/>
</smtp>
</mailSettings>
</system.net>
This would reduce your method to:
Sub SendSimpleMail()
Dim Message As New Mail.MailMessage()
Message.To = "superman#gmail.com"
Message.Subject = "test"
Message.Body = "salut je voulais savoir comment tu allais"
Dim client As New SmtpClient()
Try
client.Send(Message)
Catch ex As Exception
' ...
End Try
End Sub
MailMessage msg = new MailMessage {From = new MailAddress(txtGAddress.Text, “Sender’s Name”)};
msg.To.Add(new MailAddress(txtTo.Text));
msg.Subject = txtSubject.Text;
msg.Body = txtMessage.Text;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient
{
Host = “smtp.gmail.com”,
Credentials = new NetworkCredential(txtGAddress.Text, txtGPassword.Text),
Port = 587,
EnableSsl = true
};
Label1.Visible = true;
try
{
smtp.Send(msg);
Label1.Text = “Email sent accessfully.”;
}
catch (Exception exeption)
{
Label1.Text = exeption.Message;
}
You will need to set the port number to be used when sending via gmail using the smtpserverport property
I think this is 587 for Google
Also I believe that gmail will require an SSL connection which you can set using the smtpusessl property

Sending e-mail in ASP .Net

How to send e-mail on ASP .Net using outlook address??
I've tried this code but no luck:
Dim mailMessage As System.Net.Mail.MailMessage = New System.Net.Mail.MailMessage()
mailMessage.From = New System.Net.Mail.MailAddress("fromAddress")
mailMessage.To.Add(New System.Net.Mail.MailAddress("toAddress"))
mailMessage.Priority = Net.Mail.MailPriority.High
mailMessage.Subject = "Subject"
mailMessage.Body = "test"
Dim smtpClient As System.Net.Mail.SmtpClient = New System.Net.Mail.SmtpClient("xxx.outlook.com", portNumber)
smtpClient.Send(mailMessage) //--> got error here
But while I'm execute the code, it got this error message:
The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.1 Client was not authenticated
I've tried to add a line of code:
smtpClient.Credentials = New System.Net.NetworkCredential(username, password)
But still can't send the e-mail.
Anyone can help?
Try smtpClient.UseDefaultCredentials = false; before you set new Credentials
Try to set smtpClient.EnableSsl to true / false depending on your environment
I'm guessing you are using Exchange 2007 or later as backend?
Anyway, your mail server does not allow you to send mails anonymously. You'll either need to supply a username/password in your code or allow unauthenticated relaying from your webserver.
Talk to your IT guys what they prefer.
I did it using c#.This may help you.Please check.
MailMessage msg1 = new MailMessage();
msg1.From = strEFrom;
msg1.To = strETo;
msg1.Cc = strECC;
msg1.Subject = "Hi";
msg1.Priority = MailPriority.High;
msg1.BodyFormat = MailFormat.Html;
msg1.Body = strBody;
SmtpMail.SmtpServer = ConfigurationSettings.AppSettings["MailServer"].ToString();
SmtpMail.Send(msg1);
and in web.config file
<appSettings>
<add key="MailServer" value=""/>
</appSettings>
Try these settings check for .EnableSSL property to true/false and the smtp post number on which your mail server listen for outgoing mail.
When you do not set enable the SSL settings in outlook for gmail outlook configuration then it gives same error but the reason was found the SSL settings..
well try this may it will solve your problem..
msg.IsBodyHtml = true;
msg.Body = mailContent;
SmtpClient mailClient = new SmtpClient("smtp.mail.yahoo.com", 25);
NetworkCredential NetCrd = new NetworkCredential(frmyahoo, frmpwd);
mailClient.UseDefaultCredentials = false;
mailClient.Credentials = NetCrd;
mailClient.EnableSsl = false;
mailClient.DeliveryMethod = SmtpDeliveryMethod.Network;
mailClient.Send(msg);
if it does not solve your problem then check your mail server/ client for the problem.
public static bool SendMail(string mailAccount, string password, string to, string subject, string message)
{
try
{
NetworkCredential loginInfo = new NetworkCredential(mailAccount, password);
MailMessage msg = new MailMessage();
msg.From = new MailAddress(from);
msg.To.Add(new MailAddress(to));
msg.Subject = subject;
msg.Body = message;
msg.IsBodyHtml = false;
SmtpClient client = new SmtpClient("smtp.gmail.com");
client.EnableSsl = true;
client.UseDefaultCredentials = false;
client.Credentials = loginInfo;
client.Send(msg);
return true;
}
catch (Exception)
{
return false;
}
}
I use this code to send mail from my gmail account.
was having the same error, moved the client.UseDefaultCredentials = false; before setting client.Credentials and everything works.

Resources