Unable to send emails to external domain using SMTP - asp.net

I am not able to send emails to external domain addresses like 'user.one#asdf.com' using the code below.
SmtpClient smtpClient = new SmtpClient(smtpMailServer);
smtpClient.UseDefaultCredentials = true;
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.EnableSsl = true;
//Sending mail.
smtpClient.Send(mailMessage);
I get an exception -
Mailbox unavailable. The server response was: 5.7.1 Unable to relay for xxx#example.com
If I change the DeliveryMethod to -
smtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
I am able to send the emails on my local machine. But it fails on the production site with an exception -
Cannot get IIS pickup directory
Can you please suggest me what to do?

I had this issue and authenticating fixed it see below:
SmtpClient client = new SmtpClient(EmailServer, 25);
var SmtpUser = new System.Net.NetworkCredential("domain\\username", "password");
client.Credentials = SmtpUser;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
I had to use the double slash since one slash is the escape character so use two for it to work.

If you were to look up the MX record for the destination address (in your example, it is asdf.com) and then use that for the host property of SmtpClient, it should accept the message for delivery without authentication since it's to a local user. This is not easy to do since System.Net doesn't provide a managed DNS class that can return MX records but you can P/invoke unmanaged code to do it. Otherwise you will need to be sure that whatever SMTP server you are connecting to will relay for you and then set the Credentials property of SmtpClient to the appropriate credentials for connecting to that server. Setting the DeliveryMethod to PickupDirectoryFromIIS still only writes a file to the IIS pickup directory so it's only writing a file, it isn't doing an actual send.

You usually need to authenticate with the external mail server using a username/password. As you are using an external server this will not know the credentials you are passing. This may be your issue.

I faced this issue, which I solved by added a domain with "*.com" as domain name and type as "remote", under IIS 6.0 Manager/SMTP Virtual Server/Domains.
Although in my case the SMTP server allowed anonymous access.

Related

Unable to send email from web application

I've looked this up and it seems other people have had the same issue mainly because Gmail was blocking the application's access, however I've turned on the "allow less secure apps to connect" option and the problem remains.
Here's the complete error message I get from ASP.net when I try to connect.
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've tried connecting using port 465 and I get a time-out (I assumed it was the Explicit/Implicit SSL problem)
Here's the code that I'm using to send the emails.
Dim Mail As New MailMessage
Dim EmailToMail As New MailAddress(EmailTo, EmailToName)
Mail.To.Add(EmailToMail)
Dim FromE As New MailAddress(EmailFrom, "Something")
Mail.From = FromE
Mail.Body = Body
Mail.Subject = Title
Dim SmtpClient As New SmtpClient("smtp.gmail.com", 587)
SmtpClient.UseDefaultCredentials = False
Dim SmtpCredentials As New System.Net.NetworkCredential("MyEmail", "MyPassword")
SmtpClient.DeliveryMethod = SmtpDeliveryMethod.Network
SmtpClient.EnableSsl = True
SmtpClient.Send(Mail)
I've also tried setting UseDefaultCredentials to True, which didn't work.
your credentials are stored only within variable not within smtp instance.
set
SmtpClient.Credentials = SmtpCredentials
also make sure you use System.Net.Mail
This can happen if your password no longer meets Gmail's complexity criteria. Try logging in and changing it.
There can be a few issues.
Your firewall could be blocking it... (bypass it)
The port is blocked... (you need to allow it)
If you have two-step verification enabled on your email account...
Your credentials you are creating are never applied to the server credentials.
I have already answered this same question before here and will answer all of your problems. It also includes a link for sending the emails that I have answered as well.

Configure SMTP server on windows server 2008 to send emails from web application

i have this web application written in C# under ASP.NET 4.0 that runs on a Windows 2008 server. I want to send emails to users from username#mydomain.com but i am unable to. I'm using the following method (which worked using smtp.gmail.com) and using System.Net.Mail;
string from = "user#mydomain.com";
string password = "12345";
MailMessage mail = new MailMessage();
mail.To.Add(to);
mail.From = new MailAddress(from);
mail.Subject = title;
mail.Body = message;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "stmp ip here";
smtp.Port = 25;
smtp.Credentials = new System.Net.NetworkCredential(from, password);
smtp.EnableSsl = false;
smtp.Send(mail);
I have installed and configure SMTP server using basic authentication and outbound security -> basic authentication (using user#mydomain.com and 12345 as password). In IIS7 on my website i clicked on SMTP E-mail and entered the ip of my SMTP server together with the credentials from above.
Before doing all this config, i run into problems like the emails being stuck in the queue folder, server does not support ssl connections and others. Now, the only error i receive when sending email is logged from my application: The SMTP server requires a secure connection or the client was not authenticated. The server response was: 5.7.3 Client was not authenticated and the email is not arriving at destination. The Queue folder is empty and i don't have any errors from the SMTP server log!
I did alot of research on google about it but i can't fix it. Telnet is working as it should, port 25 is not blocked and disabling windows firewall is not helping either. I'm really stuck. Any ideas? Thanks!
Sounds like the server may require you to set 'EnableSsl' to 'true'. So, you could either try to disable this on your server or simply set 'EnableSSl = true' in your client. You will probably then need to change the port to 587 or (sometimes) 465.

Send email from ASP.NET application through an external exchange server

I have an ASP.NET application pointing the 2.0 version of the framework written in C# deployed in IIS6.
I am maintaining the application therefore some of its parts are not clear to me.
The application has to send email messages through an external exchange server to and from oldMailAddress and has been done correctly. Now I added a newMailAddress and I haven't changed anything else from the code.
The oldMailAddress receives the emails send through the application, the newMailAddress doesn't. I tried to send emails to newMailAddress from my mailbox and it works correctly.
I looked for solutions on the Internet and I saw that most of them are based on SMPT services installed in IIS6. Well, I do not have these services installed and I think it is not necessary since the application was working with oldMailAddress. Hereby I attach the code that I use, I hope anybody might help.
MailMessage oMail = new MailMessage();
switch (destination)
{
case "production":
strMailAddress = "newMailAdress#domain.eu";
oMail.From = new MailAddress(strMailAddress);
oMail.To.Add(strMailAddress);
break;
case "warehouse":
strMailAddress = "oldMailAdress#domain.eu";
oMail.From = new MailAddress(strMailAddress);
oMail.To.Add(strMailAddress);
break;
}
oMail.Subject = sTitle;
oMail.IsBodyHtml = true;
oMail.Body = sHTML;
SmtpClient oSmpt = new SmtpClient("external.smpt.eu");
oSmpt.Send(oMail);
My guess is that there is either an auth issue, or the email is being caught in a spam folder # newMailAddress.
If you still can't figure it out, then I would recommend enabling logging (if you have write permissions). I have an explanation of how to enable logging over at:
http://www.systemnetmail.com/faq/4.10.aspx

ASP.NET fails to send email through Gmail SMTP, but Outlook 2007 succeeds

Hi all
a couple weeks ago, my ASP.NET website stopped sending user comments which I implemented by emailing those comments to my email account through Gmail SMTP server (smtp.gmail.com). I opened the project on my development machine and again it fails to send the email after a couple minutes with the following exception message:
Failure sending mail
Unable to read data from the transport connection: net_io_connectionclosed.
I'm using Gmail's SMTP with port 465, and MailClient.EnableSSL = True.
The weird thing is my Office Outlook 2007 is using the same settings and it doesn't have any problems sending mail using the same Gmail account.
Any thoughts?
This code works for me. Note the port number is different to the 465 you are trying. I'm almost certain I also tried 465 first with no luck.
var msg = new MailMessage(new MailAddress(recipient, "SCS Web Site"), new MailAddress(toAddress))
{
Body = BuildMailBody(),
Subject = "SCS Web Site Message"
};
var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(myGmailUser, myGmailPassword)
};
smtp.Send(msg);
You wouldn't happen to have a firewall disallowing outbound connections here then, say, specifically allowed outlook to punch through . . .
One oddity I found with formmail going through gmail (on Windows server) was that the port number was the problem. My code was similar to yours, but it only worked when I changed the port to 25 rather than the 587 or 465. That might be what you need here.

How can I send local Email by using exchange with asp.net?

I have a project that need to sends notification for employees by local email. I use SmtpClient class to send email but didn't work! here is the code:
MailMessage message = new MailMessage();
message.From = new MailAddress("localmail1#company.com");
message.To.Add(new MailAddress("localmail2#company.com"));
message.Subject = "Sending mail";
message.Body = "Check sending email by Exchange from asp.net code <> ";
SmtpClient client = new SmtpClient("ExchangeDNS", 25);
try
{
client.Send(message);
}
catch (Exception exc)
{
Label1.Text = exc.Message.ToString();
}
When I click buttons it give me an SmtpException with message : Failure sending mail.
NB: we use Exchange server.
How can I solve it?
Use the fully qualified name for your server, for example, exchangeDNS.example.com. If that doesn't work, try the IP address. You may want to manually telnet to port 25 on the Exchange server, just to see if it is possible. Also, check whether your server requires an authenticated or encrypted connection. If so, you'll need to supply credentials with the request or change to use SSL and the secure port.
Assuming that you have the server and any required credentials correct (double check these) I would try increasing the timeout on the SMTP client.
Your code is fine. It's the configuration of your Exchange server that is suspect. Some options for your Exchange administrator:
Allow anonymous SMTP connections.
Whitelist the IPs, so that only your web servers can send mail in this manner.
Create an Active Directory account for the web server, and then have your class authenticate using a username and password.
If you post more details about your Exchange configuration, I can help, at the risk of having this question downmodded for "not programming related".

Resources