How to send Email from windows application in c#? - c#

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

Related

creating Email notification functionality in C# win app

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.

keeping mail from going into the sent folder when using Exhange Web Service to send

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

Troubleshooting "Failure sending mail" error when sending email via Windows service (works in Windows Forms application)

When i tried to send mail from Windows service, i got the exception with message "Failure sending mail".
The same code works in the windows forms application.
The windows service is running in local system account?
Kindly help me in resolving this issue.
Here is the code that sends the email:
SmtpClient smtp = new SmtpClient("XXXX", 25);
MailAddress from = new MailAddress("admdept#test.com","DRMUpdater");
MailAddress to = new MailAddress("drm_dro3#test.com","DRM");
MailMessage email = new MailMessage(from, to);
email.Subject = "DRMShell Updation Failed for user: " + userName;
email.Body = String.Empty;
smtp.Send(email);
Is it possible that your SMTP server needs authentication? And it might be ok with your normal account, but the Local System fails authentication.
You can try this either by setting the service to run under your account or by specifying specific credentials during the connection.
You can change the credentials by setting the UseDefaultCredentials property to false and creating a new NetworkCredential in the property Credentials.

How to send receive mail from MS Exchange

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.

C# - Send e-mail without having to login to server

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.

Categories