Send email with FQDN including directory - c#

I'm trying to send email with C#. Our email provider suggested that I use mail.example.com/exchange instead of mail.example.com
string mailServer;
mailServer = "mail.example.com";
mailServer = "mail.example.com/exchange";
SmtpClient smtpClient = new SmtpClient(mailServer);
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
smtpClient.Credentials = new NetworkCredential("username", "password");
smtpClient.Send("from#example.com", "to#foo.com", "subj", "email body");
When mailServer does not include a directory, after a long pause, I get:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond
When mailServer does include a directory, with no pause at all, I get:
System.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: The remote name could not be resolved: 'mail.example.com/exchange'
How can I send an email with C# for a FQDN that includes a directory?

SMTP doesn't support the concept of directories, so you might want to get an email provider that knows what they're talking about.

That seems more like a webmail address than an SMTP address. DNS isn't going to know what to do with that path. Verify that they are talking about SMTP connections (very likely they have an alternate port they use)

Related

C# - Sending an email, via Gmail or Other?

Server: VDS
OS: Windows Server 2008 R2
Application: None
Library (DLL used by an application): Yes, C#
I am trying to send mail via C# using from what I read, gmail service. Basically just a test email to myself would be a start to know it works. If you have to ask, the information is stored in config.json file rather than directly in the code, hence the "AccountRecovery.AccountRecoveryConfig".
I cannot seem to get it to work! When using certain ports I get different errors!
PORT 465 - With Credentials
ERROR:
2016-02-05 02:52:33 - Command: ERROR: System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed.
PORT 587 - With Credentials
ERROR:
2016-02-05 02:55:50 - Command: ERROR: System.Net.Mail.SmtpException: 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 have no idea what to do. Am I doing something wrong?
public static void SendEmail(string email)
{
MailMessage mail = new MailMessage(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, email);
SmtpClient client = new SmtpClient();
client.Timeout = 30000;
client.Host = AccountRecovery.AccountRecoveryConfig.HostSMTPServer;
client.Port = AccountRecovery.AccountRecoveryConfig.HostPort;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Credentials = new System.Net.NetworkCredential(AccountRecovery.AccountRecoveryConfig.ServerEmailAddress, AccountRecovery.AccountRecoveryConfig.ServerEmailPassword);
client.EnableSsl = true;
//client.ServicePoint.MaxIdleTime = 1;
mail.Subject = AccountRecovery.AccountRecoveryConfig.EmailSubjectLine;
mail.Body = AccountRecovery.AccountRecoveryConfig.EmailBodyLine;
mail.IsBodyHtml = false;
client.Send(mail);
}
The correct port is 587 for google, this error:
2016-02-05 02:55:50 - Command: ERROR: System.Net.Mail.SmtpException:
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
You should give access to less secure applications. Here is the LINK where you can do it for your current logged google account.

creating smtp server and read the emails c#

I am given a task to create a new smtp mail server which can receive mail using C#.
While going through the articles i read we can send emails via SMTP but we have to receive or read using POP.
I was directed to links by some stackoverflow already existing questions:
Rnwood and sourceforge
Rnwood I am sorry but i did not understand how to use it.
source forge the msi asked to download if we run it, it asks to download framework 1.1.4322 which will not install in my system and throw error.
Usually there are codes for sending messages so I tried msdn example
I used localhost as the server and 587 as the port.
which gives me error (for any port 587,25)
I also found an article here which actually monitors the localhost and specified port when I try to run the msdn code.
But still I am unable to send email to test in any way.
So is there any way I can code to set up smtp in my own server and receive email and test.
Setting up and configuring a mail server is a completely different ball game than just sending or reading emails from an existing IMAP / POP3 server.
A mail server consists of a number of components such as:
A Mail Transfer Agent (MTA) that handles SMTP traffic and which is responsible for sending email from your users to an external MTA and to receive email from an external MTA.
Mail Delivery Agent which retrieves mail from the MTA and places it in the recipient's mailbox.
A domain name with appropriate DNS records and an SSL certificate.
A server that provides IMAP / POP3 functionality.
In short... stick to publicly available mail servers...
In your post you referenced the SmtpClient from the .NET framework. That library is used to connect to an existing mail server. You can use it like this.
MailMessage message = new MailMessage();
message.From = new MailAddress("your.email.address#example.com", "Your name");
MailAddress recipientsMailAddress = new MailAddress("the.recipients.email#example.com");
message.To.Add(recipientsMailAddress);
message.Subject = "The subject of your email";
message.Body = "The body / content of your email";
message.IsBodyHtml = false; // You can set this to true if the body of your email contains HTML
SmtpClient smtpClient = new SmtpClient
{
Credentials = new NetworkCredential("Your username/email", "Your password"),
EnableSsl = true, // Will be required by most mail servers
Host = "The host name of the mail server", //
Port = 465 // The port number of the mail server
};
smtpClient.Send(message);
If you have a Gmail account, you can use their SMTP server in your C# application, simply use these settings and it should all work.
Hostname: smtp.gmail.com
Port: 587
Username: your_email#gmail.com
Password: ********
RequireSSL: true
Have a look at SmtpListener, I think it does what you want.
It isn't a standard email server which will receive new emails throught SMTP, store them on disk and allow you to retrieve them using POP.
SmtpListener will create a SMTP server that will receive email and allow you to react to any new email through code.
However, please note that you will have to configure it in your production environment like a real SMTP server, including MX DNS entries.

Using SmtpClient, and getting "the target machine actively refused it"

I am trying to use System.Net.Mail for an application to send an email, but get this exception:
System.Net.Mail.SmtpException: Failure sending mail. --->
System.Net.WebException: Unable to connect to the remote server --->
System.Net.Sockets.SocketException: No connection could be made
because the target machine actively refused it 198.238.39.170:25
The code I am using is:
string mailserver = "Scanmail.ofm.wa.lcl";
MailMessage msg = new MailMessage("albert#einstein.net", "snark#snarky.com", "Very subjective", "A message body!");
SmtpClient client = new SmtpClient(mailserver);
client.Send(msg);
Obviously the email addresses above are fictional, but in the actual code it uses real email addresses in the system. The mail server address is accurate.
I am tempted to think that I need to put some kind of security credentials in there, but not sure where - although #Andre_Calil's advice suggests this is not the problem, and that possibly the mail server is configured to prevent my development machine from connecting. So how is this to be overcome?
So, as we were talking, your server is probably configured to deny relay from every machine, which is a recommended security setting.
From your development machine, open a prompt (command) and type telnet SMTP_SERVER_IP_ADDRESS 25. This command will try to stablish a basic socket connection on port 25, which is the default SMTP port. If it's successful, you'll still have to discover whether the server requires authentication.
However, if it's unsuccesful, you'll have to put your code on hold until you can get the help of a sysadmin.
One other thing to try is to repeat this same test from a app server, because the SMTP server may be configured to allow app_server and deny everybody_else.
Regards

Unable to send mail from ASP.NET web page

I have a Contact Us page in my website from which I am trying to send mail.Here is my code
MailMessage feedBack = new MailMessage();
feedBack.To.Add("some#some.com");
feedBack.From = new MailAddress("MyClient#some.com");
feedBack.Subject = "Mail from My Client's Website.";
feedBack.Body = "Sender Name: " + Name.Text + "<br/><br/>Sender Last Name:"+LastName.Text+"<br/><br/>Sender Company:"+Company.Text+"<br/><br/>Sender Designation:"+Designation.Text+"<br/><br/>Sender Email:"+Email.Text+"<br/><br/>Sender Phone No:"+ PhoneNo.Text+"<br/><br/>Sender Enquiry:"+Enquiry.Text;
feedBack.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.MyClient.com"; //Or Your SMTP Server Address
//smtp.Port = 25;
//smtp.EnableSsl =false;
smtp.Credentials = new System.Net.NetworkCredential("MyClient#some.com","XXXX");
//Or your Smtp Email ID and Password
smtp.Send(feedBack);
All the time I keep getting this error
A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond 64.37.118.141:25
I have verified with my client and all the details are correct(smtp server name,credentials).
I also tried setting port to 587 and 465 but it did not work.
Can someone help me out with this?
What can be the cause?
I am not able to find it.
Any suggestions are welcome.
I tested the ip above with port 25 through telnet and got the following response:
Command:
telnet 64.37.118.141 25
Response:
Could not open connection to the host, on port 25: Connect failed
Most likely port 25 is being blocked. You will need to find what port is used with your pop/smtp servers.
MEDIUM TRUST
If you are on a shared hosting service, chances are that ASP.Net is set to run in medium-trust (as it should). This restricts SMTP to port 25 only, you cannot use any other port.
SMTP problems when ASP.NET is not running in full-trust
SO similar post
Have you checked that the server that the web application resides on is set up to be allowed to send mail through the SMTP server? Trust connections sometimes have to be setup to allow relaying off one server throught another.

C# SmtpClient error: not local host?

I'm sending a simple mail with attachment using SmtpClient but I get this error:
Mailbox unavailable. The server response was: not local host
example.com, not a gateway
System.Net.Mail.SmtpFailedRecipientException:
Mailbox unavailable.
The server response was: not local host example.com, not a gateway at
System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, SmtpFailedRecipientException& exception) at
System.Net.Mail.SmtpClient.Send(MailMessage message)
And the code:
public static void CreateMessageWithAttachment(byte[] compressed)
{
// Create a message and set up the recipients.
MailMessage message = new MailMessage(
"noreply#example.com",
"recepient#example.com",
"Hello.",
"How are you?");
// Create the file attachment for this e-mail message.
Stream attachStream = new MemoryStream(compressed);
Attachment attachment = new Attachment(attachStream, MediaTypeNames.Application.Octet);
message.Attachments.Add(attachment);
//Send the message.
SmtpClient client = new SmtpClient("123.12.12.123");
// Add credentials if the SMTP server requires them.
client.Credentials = CredentialCache.DefaultNetworkCredentials;
client.Send(message);
attachment.Dispose();
}
Of course the domain and IP is valid in the original code. I have tried using both "localhost" and IP but getting same error. Googling returned 3 results of which the helpful one seems to be in chinese and firewall preventing me from translating it.
Thanks in advance.
I've searched for: C# smtpclient error The server response was: not local host example.com, not a gateway and got 27K+ results.
If you are using localhost have a look at this page it says:
This is a relay error. Make sure you can relay through the SmtpMail.SmtpServer
either by your IP address, by your MailMessage.From address, or if you need to
authenticate, check out 3.8 How do I authenticate to send an email?
If SmtpMail.SmtpServer is set to "127.0.0.1" or "localhost", and you are using
the built in IIS SMTP Service, you can allow relaying for 127.0.0.1 by
1) Opening the IIS Admin MMC
2) Right-Clicking on the SMTP Virtual Server and selecting Properties
3) On the Access tab, click the Relay button
4) Grant 127.0.0.1 (or the IP address used by System.Web.Mail) to the
Computers list.
5) Close all dialogs
6) Restarting the SMTP Service

Categories