I set up my hMailServer on my windows 2008 machine, and I'm trying to send emails.
When I do it with C#
MailMessage message = new MailMessage();
message.From = new MailAddress(from, "John");
message.To.Add(new MailAddress(to));
message.Subject = subject;
message.Body = body;
SmtpClient client = new SmtpClient("mail.example.com");
client.Credentials = new System.Net.NetworkCredential("john#example.com", "password");
client.Send(message);
But when I try to send emails with a windows live email client, it gives me an error
The connection to the server has failed
All the settings are exactly the same. I tried several email clients, but it doesn't work. It never happened to me before. I just moved from one machine to another, and got this problem.
I can receive mail in the client though...
Try to telnet to port 25, can it connect?
Open up command prompt:
telnet mail.example.com 25
If it cannot connect (which is what I expect) then you have a problem that is not code related but firewall related. (Or perhaps you're trying to connect to the wrong port if they're running SMTP on a non standard port)
Related
I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
I am developing a web-based sales-tracking application in Visual Studio 2015 (ASP.NET MVC). The site will be hosted on an Arvixe BusinessClass for Windows shared server. The domain of the server is mydomain.com, however this domain is actually hosted by one.com, which also provides mydomain.com email.
One of the functions of the site is to inform the line manager when a user reports a sale. The line manager is to be informed via email. The email account I am trying to send from is provided by one.com.
Using System.Net.Mail.MailMessage and sending via SmtpClient, trhe following code works in my development environment (Windows 10 pro) but not deployed (deployed on Arvixe BusinessClass for Windows shared server environment):
MailMessage message = new MailMessage();
message.To.Add("recipient#mydomain.com");
message.From = new MailAddress("sender#mydomain.com", "sender#mydomain.com");
message.IsBodyHtml = true;
message.Subject = "Subject";
message.Body = "Body<br />";
SmtpClient client = new SmtpClient();
client.DeliveryFormat = SmtpDeliveryFormat.SevenBit;
client.DeliveryMethod = SmtpDeliveryMethod.Network;
client.UseDefaultCredentials = false;
client.Host = "send.one.com";
client.Port = 587;
client.UseSSL = true;
client.Credentials = new NetworkCredential("sender#mydomain.com", "***");
client.Send(message);
On my Arvixe server I get the error:
"Unable to read data from the transport connection: net_io_connectionclosed".
Using this code I have also tried ports 465 and 25, with SSL set to true and false, and also host "mailout.one.com", without success.
I gave up and tried using MailKit with the following code:
MimeMessage message = new MimeMessage();
message.To.Add(new MailboxAddress("recipient#mydomain.com"));
message.From.Add(new MailboxAddress("sender#mydomain.com", "sender#mydomain.com"));
var bodyBuilder = new BodyBuilder();
bodyBuilder.HtmlBody = "htmlbody<br />";
bodyBuilder.TextBody = "textbody";
message.Body = bodyBuilder.ToMessageBody();
message.Subject = "Subject";
using (var client = new SmtpClient())
{
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
client.Connect("send.one.com", 587, MailKit.Security.SecureSocketOptions.StartTls);
client.Authenticate("sender#mydomain.com", "***");
client.Send(message);
client.Disconnect(true);
}
Again it works in my development environment, but not in production. Here I get the error:
"The operation is not allowed on non-connected sockets".
Of note, both solutions work in deployed environment if I change the host to 127.0.0.1 and add an example#mydomain.com email account and connected user account in Arvixe, but only for external email addresses. Addresses ending in mydomain.com obviously get rerouted back to the server itself, as the server's domain is mydomain.com, but as the email provider is one.com and not the Arvixe server, the email doesn't get through.
Other things I have tried without success:
-Setting the SPF record for mydomain.com to include my server address, and my server IP address.
-Using an Office365 account on a different domain to send from (host "smtp.office365.com")
I desperately need help solving this issue. How can I get my deployed app to send emails to any address via SMTP?
Arvixe/One.com suggestions (from support tickets and live chat):
SMTP Authentication must be used.
The local mail server, or localhost must be used to send email via web script. (try xxx.win.arvixe.com, localhost, or 127.0.0.1 - port 25 or 26, or xxxsecuremail.win.arvixe.com - port 465 for secure mail)
The FROM email address, and SMTP Authentication email address, must be a local user on the local server.
If you have your own remote email server and intend to send emails via web script to email addresses that are local to your domain, then that mail domain needs to be removed from the local server (we have to do this) and you will have to use an account from another domain local to the server to send mail from. Otherwise any email going to the local domain will be delivered locally and not to your remote server.
My intention is to send a mail from c# using outlook interop library.But the problem is the prod machine won't have outlook software installed in it.
Is ther a way to send mail using c# without outlook installed?
Even if it is intalled, will it require an account to be configured?
3.Can we specify the from address manually instead of accessing the outlook account?
Note: I am not going to use SMTP based email because the sent mails will not sync with the mail server.
Thanks
yes this is possible using C# alone.
user does not need to install outlook in client machine.
C# provides a namespace called System.Net.Mail. This has all the classes required to send a mail from C#. It does not have any dependency with OutLook.
Have a look below code snippet :
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("jeet#abc.come");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("From#XYZ");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("**yoursmtphost**");
smtp.Send(message);
In place of "yoursmtphost" you can configure the Ip address of machine as well.
Hope this solves your query. Don't forget to mark answered if done.
You can easily use Gmail free SMTP Server and send Mail using your Gmail account :
System.Net.Mail MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("yourfriend#yahoo.com");
message.Subject = "subject";
message.From = new System.Net.Mail.MailAddress("yourgmailaccount#gmail.com");
message.Body = "body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
smtp.Credentials = new System.Net.NetworkCredential("yourgmailaccount#gmail.com", "yourgmailpassword");
smtp.EnableSsl = true;
smtp.Send(message);
I tried to Send email from a Desktop app using SMTP sever but my network is secure and port is closed.
So, is there another way to send email like using Gmail api ?!
I use this code but doesn't work with me
public void Send_Mail(string HTMLBody, string MailTo)
{
MailMessage Mail = new MailMessage();
SmtpClient SmtpClient = new SmtpClient();
string MailSubject = "Subject;
string MailFrom = "from#xxxx.com";
Mail.Subject = MailSubject;
Mail.Body = HTMLBody;
Mail.To.Add(MailTo);
MailAddress From = new MailAddress(MailFrom);
Mail.From = From;
Mail.IsBodyHtml = true;
SmtpClient.Host = "host";
SmtpClient.Port = port;
SmtpClient.EnableSsl = true;
SmtpClient.Send(Mail);
}
If your network doesn't allow outbound connections to whatever port gmail uses (or restricts a particular protocol, or IP, etc), then there's nothing you can do. You would have to talk to the "network guys" to either remove this restriction for you or better yet, ask them to provide the local smtp server for you to use.
I've worked in a place where we had a similar problem. Desktop machines were not allowed to send emails, but servers could be permissioned to talk to an SMTP server.
What we ended up doing was writing a windows service that listened for messages placed on a queue (Tibco EMS in our case, but MSMQ would also do). The service took the messages from the queue and passed them onto the SMTP server is was permissioned to use.
It added an extra step, and process, to the system, but was enough to satisfy the compliance department.
Normally a "secure network" means that there is a firewall in place that restricts the traffic and only allows communication on certain ports like port 80 and maybe 8080.
Such networks (workplaces, shared office spaces, schools, eg.) usually have an outgoing SMTP server you could use. Alternatively you will need to use a server that can be contacted through the port(s) that are actually open or relay/tunneling the request through a third party.
I want to send email using the SMTP client in C#. Currently I am using this code:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("receiver");
message.Subject = "";
message.From = new System.Net.Mail.MailAddress("sender");
message.Body = "This is a test mail";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("smtp.gmail.com");
smtp.Credentials = new System.Net.NetworkCredential("username", "password");
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(message);
But I want to send mail with out using username and password by just providing the sender address. Is this possible in C#?
Is this possible with any SMTP server(Not google). I mean login to server anonymously and just provide your mail address.
No. Gmail does not allow anonymous senders. Thankfully.
If it was possible anyone could pretend to send emails from everyone else, rendering gmail as one big spam engine.
It depends on your server. In your code you are using Gmail and Gmail requires this.
You can set up your own SMTP server.
The SMTP server in IIS allows this per default if your application is on the same server as IIS.
In the end you'll have to send credentials if the server demands them.
You could send the credentials of the current security context if that's what you're looking for. See: http://msdn.microsoft.com/en-us/library/system.net.credentialcache.defaultnetworkcredentials.aspx.
I am trying to send email using Exchange 2007 from a console app using the following code and I get this error message in the exception that gets thrown on the Send call.
The SMTP server requires a secure
connection or the client was not
authenticated. The server response
was: 5.7.1 Client was not
authenticated
MailMessage message = new MailMessage();
message.From = new MailAddress("from#example.com");
message.To.Add("to#domain.com");
message.Subject = "test";
SmtpClient smtp = new SmtpClient(ConfigurationUtil.SMTPServer);
smtp.Credentials = new System.Net.NetworkCredential("from#example.com", "password");
smtp.Send(message);
This worked on Exchange 2003.
This ended up being an Exchange 2007 issue and had nothing to do with code.
From the error message it seems like you need to connect to Exchange via SSL.
SmtpClient smtp = new SmtpClient(ConfigurationUtil.SMTPServer, 465);
Substitute that port number for the port that your Exchange server's secure connection is listening on.