How can I , from a custom c# application, create and send/receive mails from MS Exchange?
I am assuming this is not directly
possible with the standard framework
mail classes.
If I could say that this needs to work with MS Exchange 2003 and 2007 what are my options?
Ideally I dont want to buy a third party component so if this is possible with c# then what are the steps for creating a library that can send a new mail or receive a mail into a custom application.
The C# app will be local, as in the same network as the Exchange server.
Have you tried using the built-in .Net mail assemblies?
If you create an SmtpClient client = new SmtpClient("my-email-server"), does smtp.Send not work?
----- with code
If the machine has a mail account setup then no, it should use the ones from the system so long as you set DefaultNetworkCredentials:
SmtpClient smtp = new SmtpClient("mailserver");
smtp.Credentials = CredentialCache.DefaultNetworkCredentials;
You can create some though and use those instead:
SmtpClient client = new SmtpClient("myserver");
client.Credentials = new NetworkCredential("username", "password", "domain");
Have you put the following code in your web.config file?
<system.net><mailSettings><smtp><network
host="host.name.com" port="port number"
userName="username" password="password"/></smtp></mailSettings></system.net>
There's a number of routes to look at: MAPI, CDO, 3rd party libraries etc.
What version of Exchange is it you're working with as I think 2007 has some web services that you can use that OWA plugs in to.
Related
Wonder if you can help please? I created an email notification system for windows app. This app is accessed via file server and citrix. It works perfectly using via file server and sends email notification using logged in users local outlook.
But when the user attempt to send notifications while accessing the same application via citrix it fails to run as we do not have the outlook access via citrix due to corporate policies.
We have been suggested to either request installation of outlook on citrix server (which will take a long time before it can be approved from top management) or re-write the code to access the local device outlook through citrix.
Does any one have any suggestion or example on how to proceed with such case?
Thank you very much for your help in advance.
You could use Smtp mail client:
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("shiraz#address.com");
message.Subject = "This is the Subject line";
message.From = new System.Net.Mail.MailAddress("BSingh#address.com");
message.Body = "This is the message body";
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("**yoursmtphost**");
smtp.Send(message);
I would recommend to use MailKit for email notifications since smtp client is obsolete
From MS site:
Important
We don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Use MailKit or other libraries instead. For more information, see SmtpClient shouldn't be used on GitHub.
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.
We are using Exchange Web Services to send mail in out WCF application, here is a little code snippet.
//using ExchangeWebServices;
var email = new MessageType();
email.IsFromMe = false;
email.From = new SingleRecipientType();
email.From.Item = new EmailAddressType();
email.From.Item.EmailAddress = message.From;
email.ToRecipients = message.To.Select(to => new EmailAddressType { EmailAddress = to }).ToArray();
It works fine but it's filling up the sent mail folder in for the "appserver" user who sends the mail. Is this something we can configure in the app to "not copy it to the sent folder" or does this need to be done by an administrator for the exchange serer?
The reason I ask is cause the admin is a third party consultant so if it could be done without bothering them that would be great.
Thanks! Happy Holidays!
Not sure if this is an option or not, but if you can use SmtpClient rather than Exchange Web Services, you can send email without a copy going to a 'sent mail' folder. Obviously you have access to an Exchange server, so you'd just need to have Exchange's SMTP server configured such that your application server can relay through it. Otherwise you could setup a new SMTP server using the SMTP functionality included in IIS:
SmtpClient
Configuring SMTP in IIS 7
I have an application that needs to send e-mails. Currently, this is what I am using:
System.Net.Mail.MailMessage MyMailMessage = new System.Net.Mail.MailMessage();
MyMailMessage.From = new System.Net.Mail.MailAddress(fromemail.Text);
MyMailMessage.To.Add(toemail.Text);
MyMailMessage.Subject = subject.Text;
MyMailMessage.Body = body.Text;
System.Net.Mail.SmtpClient SMTPServer = new System.Net.Mail.SmtpClient("smtp.gmail.com");
SMTPServer.Port = 587;
SMTPServer.Credentials = new System.Net.NetworkCredential("email", "password");
SMTPServer.EnableSsl = true;
SMTPServer.Send(MyMailMessage);
Is there a simple way to send an e-mail without having to login to a server? Thank you.
GMail's SMTP server always requires authentication. You may need to setup your own server to send email without authentication.
Configure an SMTP server into your local network (behind a firewall to avoid being a spam source) and use it directly. You can create one in IIS.
There are 2 ways to achieve this:
1) Use your local smtp server (e.g. one with IIS on Win2003/2008 server) and write messages to the local pickup queue). This is possible with minimal changes.
2) You need to resolve the target smtp server. For example when you want to send an email to somebody at msn.com, you'll need to get the MX record for msn.com, e.g. something like mx1.msn.com. You can then directly connect to this SMTP server and send your email to the (local) recipient. Note that there are no built-in ways to resolve the MX-host in .NET (in the sense there are no methods on the Dns class to accomplish this) - you need to do it "manually". Also most SMTP hosts will reject connections from home/residential IP addresses.
You need an SMTP server that does not require authentication, however to stop it being a SPAM server, it needs some other kind of protection like a firewall.
I am working MS C# 2008. I created Windows form application. And I need to send email from my application. so how do I configure smtp settings?
EDIT
I got The following Exception
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
on smtp.send(message);
I have not installed IIS so is it required for desktop app?
You can add the SMTP settings within the App.Config
http://www.mitchelsellers.com/blogs/articletype/articleview/articleid/8/net-20-smtp-settings.aspx
And then use System.Net.Mail.SmtpClient and System.Net.Mail.MailMessage to send and create the emails.
c = new System.Net.Mail.SmtpClient();
msg = new System.Net.Mail.MailMessage();
System.Net.Mail.MailAddress a = new System.Net.Mail.MailAddress( sEmailAddress, sWho );
msg.To.Add( a );
msg.From = new System.Net.Mail.MailAddress("");
msg.ReplyTo = new System.Net.Mail.MailAddress("");
msg.Subject = "Web Inquiry";
msg.Body = msgBody.ToString();
c.Send( msg );
why are a lot of you that are making the "IIS" suggestions using this as the backbone to solve the problem? What if this is a deployed application? you going to have the client install and run IIS on their mediocre system just use the mail functionality of your app???
That doesn't make sense to me.
Those of you looking for a solution on sending emails thru win apps, do a google search on "using gmail to send email in c#".
-Rob