Asked by:
Failure sending mail in ASP.net

Question
-
User-1500873099 posted
Hello..
From a last couple of day.. i am facing the problem in sending email from my web site.
I use gmail account;
so my sender host is "smtp.gmail.com"
and port is 587.
But it shows "Failure sending mail" exception
Plz help...
Monday, November 4, 2019 12:49 PM
All replies
-
User475983607 posted
The error message us usually far more explicit than what's posted. Is there an inner exception? Along with the detailed error message, can you share the source code that causes the exception? What kind of application are you building? Is this a web application and perhaps a firewall is blocking gmail? What troubleshooting steps have you performed up to this point?
Monday, November 4, 2019 2:35 PM -
User61956409 posted
Hi Rajani123,
"Failure sending mail" exceptionPlease make sure you provide correct credentials (userName&password), and please check if you turn on "Allow less secure apps".
Besides, you can debug your code and check if you can get more error message, which would help troubleshoot the issue.
With Regards,
Fei Han
Tuesday, November 5, 2019 2:19 AM -
User-1500873099 posted
Hi.. Thanks for reply.
Yes.. inner exception message is
"Unable to connect to the remote server"
No. My firewall not blocking gmail.
Here is the complete source code that is am using
#########
//Base class for sending email
MailMessage _mailmsg = new MailMessage();//Set From Email ID
_mailmsg.From = new MailAddress("sender@gmail.com");//Set To Email ID
string[] MultiTo = sTO.Split(';');
foreach (string multiemail in MultiTo)
{
_mailmsg.To.Add(new MailAddress(multiemail));
}
//Set Subject
_mailmsg.Subject = "This is Subject";//Set Body Text of Email
_mailmsg.Body = "This is test message";//Now set your SMTP
SmtpClient _smtp = new SmtpClient();//Set HOST server SMTP detail
_smtp.Host = "smtp.gmail.com";//Set PORT number of SMTP
_smtp.Port = 587;//Set SSL --> True / False
_smtp.EnableSsl = true;
_smtp.Timeout = 300000;//Set Sender UserEmailID, Password
NetworkCredential _network = new NetworkCredential("sender@gmail.com", "SenderPassword");
_smtp.UseDefaultCredentials = true;_smtp.Credentials = _network;
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//Send Method will send your MailMessage create above.
_smtp.Send(_mailmsg);_mailmsg.Dispose();
###################
Tuesday, November 5, 2019 5:11 AM -
User-1500873099 posted
Yes.. I already change setting to turn on "Allow less secure apps".
My credential are correct..
Thanks
Tuesday, November 5, 2019 5:14 AM -
User61956409 posted
Hi Rajani123,
Yes.. I already change setting to turn on "Allow less secure apps".
My credential are correct..
Do you mean that you checked credential and turned on "Allow less secure apps", but the code still cause exception? As we mentioned, if there are any more detailed error message while debugging the source code.
With Regards,
Fei Han
Tuesday, November 5, 2019 7:21 AM -
User-1500873099 posted
Yes, I checked credential. In inner exception it shows
InnerException = {"The requested address is not valid in its context 74.125.200.108:587"}
Message = "Unable to connect to the remote server"
Thanks.
Tuesday, November 5, 2019 7:32 AM -
User61956409 posted
Hi Rajani123,
In inner exception it shows
InnerException = {"The requested address is not valid in its context 74.125.200.108:587"}
Message = "Unable to connect to the remote server"
If possible, please share the code snippet you are using (and please exclude credentials etc sensitive information), so that we can try to reproduce and troubleshoot the issue based on your code.
With Regards,
Fei Han
Tuesday, November 5, 2019 8:54 AM -
User-1500873099 posted
ok.
Here is code
//Base class for sending email
MailMessage _mailmsg = new MailMessage();//Set From Email ID
_mailmsg.From = new MailAddress("sender@gmail.com");//Set To Email ID
string[] MultiTo = sTO.Split(';');
foreach (string multiemail in MultiTo)
{
_mailmsg.To.Add(new MailAddress(multiemail));
}
//Set Subject
_mailmsg.Subject = "This is Subject";//Set Body Text of Email
_mailmsg.Body = "This is test message";//Now set your SMTP
SmtpClient _smtp = new SmtpClient();//Set HOST server SMTP detail
_smtp.Host = "smtp.gmail.com";//Set PORT number of SMTP
_smtp.Port = 587;//Set SSL --> True / False
_smtp.EnableSsl = true;
_smtp.Timeout = 300000;//Set Sender UserEmailID, Password
NetworkCredential _network = new NetworkCredential("sender@gmail.com", "SenderPassword");
_smtp.UseDefaultCredentials = true;_smtp.Credentials = _network;
_smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
//Send Method will send your MailMessage create above.
_smtp.Send(_mailmsg);_mailmsg.Dispose();
Tuesday, November 5, 2019 9:34 AM -
User475983607 posted
The error message is very clear and it is telling you that the SmtpClient cannot connect to gmail. Use telnet from a command prompt to verify your system can connect to smtp.gmail.com
telnet smtp.gmail.com 587
Also UseDefaultCredentials should be false.
_smtp.UseDefaultCredentials= false
If you are unable to connect using telnet then your firewall or virus checker is probably blocking the post as suggested above. Did you really check?
Tuesday, November 5, 2019 1:09 PM -
User1845908998 posted
If it is working on your local machine and not on the server, then the server cannot access the mail server you are using, for some reason.
Thursday, November 14, 2019 8:47 AM