Add Reference: Microsoft.Office.Interop.Outlook
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Net.Mail;usingSystem.Net;usingOutlook =Microsoft.Office.Interop.Outlook;namespacecommon{ Public classMailhelper { Public Static voidSendMail () {Outlook.Application olapp=NewOutlook.Application (); Outlook.MailItem MailItem=(Outlook.MailItem) Olapp.createitem (Outlook.OlItemType.olMailItem); Mailitem.to="[email protected]"; Mailitem.subject= DateTime.Now.ToString ("YYYYMMDD") +"_ Report"; MailItem.BodyFormat=Outlook.OlBodyFormat.olFormatHTML; stringContent ="Attachment is"+ DateTime.Now.ToString ("YYYYMMDD") +"data, please check, thank you! "; Content="each recipient,<br/> <br/> please focus on the following:<br/> <br/>"+ content +"<br/> <br/><br/><br/> This message is automatic email notification, please do not reply directly! Thank you. "; Content= content +"<br/>\r\n <br/>best regards!\r\n < ; br/>\r\n <br/> \ r \ <br/>== ============================================\r\n \ r \ < ; br/>\r\n <br/>\r\n \ r \ n ========================== ====================="; Mailitem.htmlbody=content; MAILITEM.ATTACHMENTS.ADD (@"C:\test.rar"); ((Outlook._mailitem) MailItem). Send (); MailItem=NULL; Olapp=NULL; } }}
In Microsoft Office Outlook, you need to configure a sender's mail account, and if it is a free mailbox like 163, you need to set up a "client authorization password" and then configure it with this client-authorized password to replace the mailbox's login password . Otherwise, it will not be able to send mail.
After the send is successful as shown:
C # calls Outlook to send a message instance