Walkthrough: A small email project developed by the application eventhandle

Source: Internet
Author: User
Tags mailmessage smtpclient

Project Background: The Mail Server of exchange, which sets that all users cannot send external mail. Objective: To create a list for users to submit data, enter: the "recipient address", "email content", "title", and attachments are then approved by the approval process provided by moss. The Code is as follows: public override void itemupdated (spitemeventproperties properties)
{
// Obtain the list of trigger events
Spsite site = new spsite (properties. siteid );
Spweb web = site. openweb (properties. openweb (). Id );
Splist list = web. Lists [properties. listid];
Splistitem item = List. Items. getitembyid (properties. listitemid );
// Obtain the creator of the list item
String struservalue = convert. tostring (item ["author"]);
Int intindex = struservalue. indexof (';');
Int intid = int32.parse (struservalue. substring (0, intindex ));
Spuser Muser = web. siteusers. getbyid (intid );
// Obtain approval information
Spmoderationinformation mymi = item. moderationinformation;
String getstate = mymi. Status. tostring ();
If (getstate = "approved ")
{
// Set the email sending component
Mailmessage mail = new mailmessage ("infozh@fzh.com", convert. tostring (item ["recipient address"]);
Mail. subjectencoding = encoding. utf8;
// Email Subject
Mail. Subject = convert. tostring (item ["title"]);
Mail. isbodyhtml = false; // whether the content is allowed in HTML Format
Mail. bodyencoding = encoding. utf8;
// Email content
Mail. Body = convert. tostring (item ["email content"]);
// The reply address of the email. The email cannot be sent but can be received by the user.
Mail. replyto = new mailaddress (Muser. Email, Muser. Name );
Mail. From = new mailaddress ("infozh@fzh.com", Muser. Email); // to and CC need to add the email address using the Add Method
// Add an attachment
For (INT I = 0; I <item. attachments. Count; I ++)
{
String strpath = item. attachments. urlprefix + item. attachments [I]. tostring (); // URL of the attachment to be sent
String strfilename = strpath. substring (strpath. lastindexof ("/") + 1); // file name
WebClient WC = new WebClient ();
Memorystream M = new memorystream ();
WC. usedefacrecredentials = true;
Byte [] FS = WC. downloaddata (strpath );
M = new memorystream (FS); // capture the file and store it in the memory.
Attachment MA = new attachment (M, strfilename );
Mail. attachments. Add (MA );
}
// Set verification information
Smtpclient SMTP = new smtpclient ("MailServer ");
SMTP. Credentials = new networkcredential ("username", "password"); // SMTP Verification
SMTP. Send (Mail );
Mail. attachments. Dispose (); // After the email is sent, the attachment is locked.
Sentinform (mail. Subject, Muser. Email, convert. tostring (item ["recipient address"]);
}
}

// Send a notification email
Void sentinform (string substr, string soueml, string tareml)
{
Mailmessage sentok = new mailmessage ("moss@fzh.com", soueml );
Sentok. subjectencoding = encoding. utf8;
Sentok. Subject = "Ann: the Internet mail you submitted has been sent successfully ";
Sentok. Body = "The sent Email Subject is:" + substr + "the recipient address is:" + tareml;
Smtpclient smtp1 = new smtpclient ("MailServer ");
Smtp1.credentials = new networkcredential ("username", "password"); // SMTP Verification
Smtp1.send (sentok );
} Today, a new method is found on the Web. set the authentication information of the SMTP server in config, so that you do not need to set verification information for each smtpclient object as follows: <system.net> <mailsettings> <SMTP from = "test@foo.com"> <network host = "smtpserver1" Port = "25" username = "username" Password = "secret" defaultcredentials = "true "/> </SMTP> </mailsettings> </system.net>

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.