How can I send local Email by using exchange with asp.net? - 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".

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.

the server rejected one or more recipient addresses. the server response was 550 5.7.1 unable to relay

I am sending mails using our company SMTP server. I get the problem when I am sending mail to other mails (out side of the company domain), returning the error:
"The server rejected one or more recipient addresses. The server
response was 550 5.7.1 unable to relay".
If the mail is within the company, then there is no error and mail sending successfully. My web application hosted in IIS.
I'll assume that there is nothing wrong in your code. I guess it is a configuration issue i.e SMTP server configured to not send emails to addresses that are not part of your company domain. If so you need to check with your Windows/Network team to confirm configuration applied at SMTP server level.
I have Solved this problem. For using this code you need to add the namespace Using System.Web.Mail;
Source Code:
MailMessage mail = new MailMessage();
mail.To = "yourfromemail#domain.com";
mail.From = "yourtodomain#domain.com";
mail.Subject = "Email test.";
mail.Body = "Your body text here";
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //basic authentication
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", "my_username_here"); //set your username here
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", "super_secret"); //set your password here
SmtpMail.SmtpServer = "127.0.0.1";
SmtpMail.Send( mail );
//your need to add localhost address to IIS Manager.
Goto IIS Manager -> Default SMTP Server ->Properties -> Access -> Relay -> allow only from the list below -> add -> 127.0.0.1 -> Click OK

what user info do I need to send an email from his mail server

I'm building an app where the user creates an email and where the email is sent from his mail server. I'm not looking to receive any emails, just to send them. I need to access his mail server to do this and I'm wondering a) what kind of email servers are there (Exchange, SMTP, POP3...) and what kind of info does the user need to provide me (ie. what fields do I need in the DB).
If you know of any pitfalls to avoid, please let me know.
Thanks for your suggestions.
SMTP and POP3 are protocols, not Email Servers.
If I understand correctly, you need to connect to different email servers based upon the client that's constructing the email to send the email through his/her mail server(?)
If that's so, you need to, for every client of yours, find out their mail server's IP address and the type of authentication, encryption, etc. that they support. Once you have that established, you need to connect to each server based with the specific credentials provided by your client and send the email through their SMTP server. Example:
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"jane#contoso.com",
"ben#contoso.com",
"Quarterly data report.",
"Hello, test email!.");
//Send the message.
SmtpClient client = new SmtpClient(server);
// Add credentials if the SMTP server requires them.
// YOU NEED TO CHANGE THIS PART DEPENDING ON THE SPECIFICS OF THE
//SMTP SERVER THAT YOU WILL BE USING
client.Credentials = CredentialCache.DefaultNetworkCredentials;
try {
client.Send(message);
}
catch (Exception ex) {
Console.WriteLine("Exception caught in CreateMessageWithAttachment(): {0}",
ex.ToString() );
}
Above example was taken almost verbatim, from here.
Sending email is easy, how you store and manage this is up to you:
Sending Email in ASP.NET 2.0
web.config:
<system.net>
<mailSettings>
<smtp>
<network
host="relayServerHostname"
port="portNumber"
userName="username"
password="password" />
</smtp>
</mailSettings>
</system.net>
Code:
MailMessage mail = new MailMessage(from, to, subject, message);
mail.IsBodyHtml = true;
SmtpClient client = new SmtpClient(); //used config settings
client.Send(mail);

Unable to send emails to external domain using SMTP

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.

Resources