tags:

views:

413

answers:

4
+2  Q: 

Sending Emails C#

Dear all,

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 attachments.

And there is a requirement to set the from address too.

Most important thing is mail format should be in HTML

+3  A: 

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 [email protected] Add entry in the "Blind Copy To" or "BCC" field [email protected] 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.

Mahin
Here the issue it "Mailto:" wont support HTML format mails, and body size is limited to 255 chars, so I can't use it :(
Buddhi Dananjaya
And it also will not allow you to add Attachments.. Sorry me too. But according to your requirement, IMHO, mailto: is the best available solution or you should popup your own Mail sender.
Mahin
A: 

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.

Jeremy McGee
I just want to pop up a new mail dialog of the default mail client. Not automate, user will sent it manually.
Buddhi Dananjaya
A: 

Hi, 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.

Sumeet
This even let you add an attachment:Attachment attach = new Attachment("c:\filename.txt"); mail.Attachments.Add(attach);
Sumeet
Sorry I misunderstand your question, and replied only how to send mail .. your issue is configuring new outlook mail the way you want. (mail send task will be done by outlook itself). For that you can access following link, that I found for you:http://social.msdn.microsoft.com/forums/en-US/vsto/thread/64f5ba58-5dd0-48e7-a706-a2de214f5be9Oryou can create an object of outlook application.Outlook.Application objOutlk = new Outlook.Application();and create its new item i.e.objMail = objOutlk.CreateItem(olMailItem);now you can configure this objMail..
Sumeet
Thank you for replies, I just want to open a new mail dialog with given values, for fields like "from", "to", etc... exactly similar to action "mailto:" in html
Buddhi Dananjaya