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>