Last post Jun 28, 2017 08:45 AM by Rajani123
Jun 22, 2017 09:12 AM|mamidi234|LINK
Here I am using asp.net for sending mails to gmail.
In my local system the output is successful but after I was uploaded into server It was displayed sending mail failed .
This is my web.config code for sending mails:
<smtp deliveryMethod="Network" from="email@example.com">
<network host="smtp.gmail.com" port="587" userName="firstname.lastname@example.org" password="123456" enableSsl="true"/>
This my aspx.cs code:
protected void sendMail()
MailMessage mail = new MailMessage();
mail.From = new MailAddress("email@example.com");
mail.Subject = "Mail From Admin...";
Literal ltrlBody = new Literal();
ltrlBody.Text = txtschoolcode.Text;
mail.Body = ltrlBody.Text;
mail.IsBodyHtml = true;
mail.Priority = MailPriority.High;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Credentials = new System.Net.NetworkCredential
smtp.EnableSsl = true;
If any possible reasons let me know
Thanks in advance
Jun 22, 2017 11:33 AM|Eric Du|LINK
According to your code and description, caused this error reasons is more. Could you tell us the detailed error message, this will better help you!
Please add the defaultCredentials="true" to your network element:
Please check all properties in network element:
<network> Element (Network Settings):
Here are someone who meet the similar problem as you, please check:
Failure sending mail. error in asp .netFailure sending mail. error in asp .net:
C# - Failure sending mail:
Jun 22, 2017 11:45 AM|PatriceSc|LINK
If the previous answer doesn't help, it seems you just show exception.Message which just shows the message for the outer exception. Use exception.ToString() to get the full story including the very first exception that happened in the exception chain.
For now my guess is :
- a firewall issue
- if hosted on a remote server it might be a gmail safety measure that warns about using an unusual location (you should have a mail in the mailbox that gives instructions about how to allow that)
It's alwats best to start from the actual error message so that you have to deal with fixing a known problem rather than having first to try to guess about the actual problem.
Jun 27, 2017 05:41 AM|mamidi234|LINK
Hi Eric Du,
Thanks for giving reply.
I added defaultCredentials="true" to network element but its not working again it shows sending mail failed .
If any cause have please help me
Thank in advance.
Jun 27, 2017 06:30 AM|gaurabchatterjee|LINK
Hi, I think this is regarding the SSL.
please add the following code .
smtp.UseDefaultCredentials = true;
smtp.EnableSsl = true;
something like this
string to = "firstname.lastname@example.org";
string from = "email@example.com";
MailMessage message = new MailMessage(from, to);
message.Subject = "Using the new SMTP client.";
message.Body = @"Using this new feature, you can send an e-mail message from an application very easily.";
SmtpClient client = new SmtpClient(server);
// Credentials are necessary if the server requires the client
// to authenticate before it will send e-mail on the client's behalf.
client.UseDefaultCredentials = true;
client.EnableSsl = true;
client.Send(message);Or you can put that configuration section on web config
</configuration>Or if possibible post the detaile error message . Another thing you can do change the Gmail port in your web config to 465
Jun 27, 2017 08:20 AM|PatriceSc|LINK
You are just looking at exception.Message that will always shows the same "Failure sending mail" message regardless of the root cause. See exception details (ie exception.ToString() if creating your own log) so that you can get the full story and narrow
down more quickly what is the actual problem.
Jun 28, 2017 06:27 AM|mamidi234|LINK
I was added exception.ToString() after i added it I am getting this error any one help me ASAP
Request for the permission of type 'System.Net.Mail.SmtpPermission, System, Version=220.127.116.11, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.
Jun 28, 2017 07:23 AM|PatriceSc|LINK
Ah, your app currently runs using a "partial trust" mode which is mostly used on shared servers. This is your own server or do you use an hosting service ?
Edit: Or according to
https://social.msdn.microsoft.com/Forums/en-US/a6517803-d73b-4064-a1f9-76f9a24f1125/request-for-the-permission-of-type-systemnetmailsmtppermission?forum=netfxnetcom it can also happen if you configured IIS to take application files from a network share.
Jun 28, 2017 08:45 AM|Rajani123|LINK
I saw ur code. But smtpclient needs two parameters namely- Host and port.
U mentioned host, but not Port.
smtp.Port = 587;