. How to automatically send messages with Outlook in net (C #)

Source: Internet
Author: User
Tags comments mailmessage

There is a requirement in your job that you need to use Outlook to send messages automatically. Don't get me wrong, I'm not going to do hacking, just want to use Outlook to send users feedback to us. That is, the user input their comments in the text box, press the Send button, comments automatically sent to the situation of the email to the developers, but also can not let users know that we have stolen their mailbox (although not hackers, the behavior is black enough ...) )

There are two reasons to fancy outlook

1, because of the company's policy, Outlook everyone will use (believe in many it, not IT companies are the case)

2, even if the offline status will not be an error, and so on the same time will send the message. The industrious Outlook function ah.

There are classes in the. NET Framework that simulate Outlook to connect to the SMTP server to send mail, this article is not mainly to say this, but put a section of the Internet to find a sample of this method code, to prevent someone by the title cheat in scold my title party.

MailMessage mail = new MailMessage();
mail.From ="JobAlarm";
mail.Subject ="Job Alarm";
mail.Priority = MailPriority.High;
mail.BodyEncoding = Encoding.GetEncoding("BIG5");
mail.BodyFormat = MailFormat.Text;
mail.To ="Receive Body 's Mail Address";
mail.Body ="The mail's Content";
SmtpMail.SmtpServer ="Smtp Server Address";
SmtpMail.Send(mail);

The following point, say, little sister in your powerful bloggers and peer inspired, think out two methods, unfortunately are not very satisfactory, finally had to give up the intention of sending user feedback with e-mail, (ah ...). I feel the rotten Tomatoes are flying towards me ... However, the two methods are written down, hoping to give you a little inspiration.

Method One: Call the Process.Start () method directly.

Process.Start(new ProcessStartInfo("mailto:user@126.com?subject=test&body=Dear:%0A%0DThis is a test mail."));

This method will appear in a mail box, of course, the title and content have been automatically filled out.

Next, I came up with a trick--using the Windows API GetWindow () method to get the handle to the mail window, then find the Send button and click directly.

Of course, is not actually I want to come out, nature is someone else to come out, but this recruit too loss, abandon. If you have a friend who is interested in this trick, try it.

For a sample code on the Windows API, you can refer to this post:

Http://topic.csdn.net/t/20040711/10/3163913.html

You don't have to write the virus in this way, ^0^.

Method Two: Formal approach, but also the way to go, call the. NET Outlook API to send.

Here is the step: 1, add reference Microsoft.Office.Interop.Outlook;

This must be reference above the right button, select Add to the line, the direct using will say not found.

2, the code is as follows:

Microsoft.Office.Interop.Outlook.Application outlookObj = new Microsoft.Office.Interop.Outlook.Application();
MailItem Item = (Microsoft.Office.Interop.Outlook.MailItem)
outlookObj.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

Item.To = "user@126.com";

Item.Subject = "hello";

Item.Body = "hello";

Item.Send();

However, it is annoying that, in this way, Outlook produces a dialog box that alerts the user that an unknown application is sending a message in her name.

Everyone can go and try. If you find a way to avoid this warning box, please tell me, thank you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.