Sending Emails C#
I am writing a application which need to open a new mail dialog, upon clicking on a button on it. Application users uses different mail clients like MS Outlook, Thunder Bird, etc...
Application should open a new mail dialog filled with information passed by my application, like TO, BCC, Subject and Body sometimes with a开发者_Go百科ttachments.
And there is a requirement to set the from address too.
Most important thing is mail format should be in HTML
Please refer to below article for more information about mailto: protocol sytax
http://www.ianr.unl.edu/internet/mailto.html
The MailTo command can do more than enter a single e-mail address in the "Send To" field while activating your e-mail program. It can also: Feature
Syntax Address message to multiple recipients , (comma separating e-mail addresses) Add entry in the "Subject" field subject=Subject Field Text Add entry in the "Copy To" or "CC" field cc=id@internet.node Add entry in the "Blind Copy To" or "BCC" field bcc=id@internet.node Add entry in the "Body" field body=Your message here Within the body use "%0A" for a new line, use "%0A%0A" for a new line preceded by a blank line (paragraph), see example below.Notes:
" " (beginning and ending double quotes) are necessary if any spaces are used
Mailto parameter should be preceded by "?" for the first or only parameter and "&" for second and subsequent parameter.
I doubt about Attachments. I dont think you can apply attachments with mailto links. But still lets see if some one knows about this.
if you want to send a mail programmatically in c#, C# is coming with "using System.Net.Mail" namespace. SmtpClient and MailMessage classes can do the work for you to explicitly send a mail configured with CC and BB and subjects settings by you.. . For e.g;
//instance of mail message
MailMessage mail = new MailMessage();
//create instance of smtpclient
SmtpClient smtp = new SmtpClient();
smtp.EnableSsl = true;
//recipient address
mail.To.Add(new MailAddress(userid));
//Formatted mail body
mail.IsBodyHtml = true;
string st = "";
st += "<HTML><BODY>";
st += "<span style='font-family: Verdana; font-size: 9pt'>";
st += "Dear Sir/Madam,<br/><br/>";
st += "We refer to your request for reseting your login password.";
st += "Please take note of your Login Password, as you need them every time you login.<br/><br/>";
st += "<b>Email/Userid: " + userid + "</b><br/>";
st += "<b>Password: " + password + "</b><br/><br/>";
st += "Regards</br>";
st += "XYZ Team";
st += "</span><br />";
st += "</HTML></BODY>";
mail.Body = st;
//send mail via smtp client
smtp.Send(mail);
Following is the Configuration settings for SMTP client that need to be done in the APP.config:
> <system.net>
> <mailSettings> <smtp from="">
> <network host="" port="" userName="" password=""
> defaultCredentials="false" />
> </smtp> </mailSettings> </system.net>
Note: you can define these values via code as well.
Hope it helps.
we are using this tool http://www.codeproject.com/KB/IP/CMapiEx.aspx.
Also check out that How do I popup the compose / create mail dialog using the user's default email client?
So the question is a little confusing: does the application need to automate Outlook and Thunderbird or do you need to send email using C# to clients that will use Outlook and Thunderbird?
If you need to automate the third-party applications then look to VBA for Outlook. (I'm not sure that Thunderbird can be automated in this way as I don't believe it has an externally visible macro language. But given that the corollary of Zawinski's Law of Software Envelopment is that every application gets a macro language to send email I may be wrong.)
If you want to send mail from a C# application take a look at the MailMessage (MSDN) component. This should do everything you need. You'll need to configure it appropriately to send mail in the MIME format text/html
: MSDN has an example of how to do this here that also includes alternate views for users that can't or won't read HTML mail.
精彩评论