Tool-SharePoint mail function

Source: Internet
Author: User
Tags mailmessage smtpclient
Using System;
Using System. Collections. Generic;
Using System. text;
Using System. net. mail;
Using Microsoft. Sharepoint. administration;

Namespace Newhire
{
Class Mailentity
{
Internal Void Sendemail (String From, String To, String CC, String Subject, String Body, String Userid, String Password)
{
Smtpclient SC = Null ;
Mailmessage message = Null ;
Try
{
SC = createsmtpclient (userid, password );
Message = buildmailmessage (from, to, CC, subject, body );
SC. Send (Message );
Message. Dispose ();
}
Catch
{
Throw ;
}
Finally
{
Message = Null ;
SC = Null ;
}
}

/// <Summary>
/// Authenication smtpclient
/// </Summary>
/// <Param name = "userid"> User ID (sys-nyc1-sp) </Param>
/// <Param name = "password"> Password </Param>
/// <Returns> Return smtpclient object </Returns>
Private Smtpclient createsmtpclient ( String Userid, String Password)
{
Smtpclient client = Null ;
Try
{
String Smtpserver = spadministrationwebapplication. Local. outboundmailserviceinstance. server. address;
Client = New Smtpclient (smtpserver );
Client. Credentials = New System. net. networkcredential (userid, password );
}
Catch
{
Throw ;
}
Return Client;
}

/// <Summary>
/// Get mailmessage object
/// </Summary>
/// <Param name = "from"> From </Param>
/// <Param name = "to"> To </Param>
/// <Param name = "cc"> CC </Param>
/// <Param name = "subject"> Subject </Param>
/// <Param name = "body"> Body </Param>
/// <Returns> Return mailmessage object </Returns>
Private Mailmessage buildmailmessage ( String From, String To, String CC,String Subject, String Body)
{
Mailmessage message = New Mailmessage ();
Try
{
Message. From = New Mailaddress (from );
Message. to. Add ();
If (CC! = String . Empty)
Message. CC. Add (CC );
Message. Subject = subject;
Message. isbodyhtml = True ;
Message. Body = body;
}
Catch
{
Throw ;
}
Return Message;
}

}
}

2. The default port for Asp.net SSL is 465.CodeIs:

Mailmessage mess =New Mailmessage ();
Mess. From = New Mailaddress ( " Ggggggghai@163.com " );
// The send e-mail address must be correct.
Mess. Subject = This . Txttitle. Text. Trim ();
// Email Subject
Mess. isbodyhtml =True ;
Mess. bodyencoding = system. Text. encoding. utf8;
// Email code
Mess. Body = This . Txtcontent. Text. Trim ();
// Email body

Smtpclient client = New Smtpclient ();
Client. Host = " Smtp.163.com " ;
// The SMTP server must be correct. After testing, you can use smtp.sina.com (. cn) smtp.163.com smtp.126.com
If (Txtadd. Text! = "" )
{
Mess. attachments. Add ( New Attachment (txtadd. Text ));
}
Client. Credentials = New System. net. networkcredential ( " Ggggggghai " , " 123456 " );
// Verify that the user name and password are correct.
Mess. to. Add ( New Mailaddress ( This . Txtreceiver. Text. Trim ()));
// The email address for receiving the email must be correct.
Try
{
Client. Send (MESS );
MessageBox. Show ( " Sent successfully! " );
}
Catch (Exception ee)
{
MessageBox. Show (EE. Message );
}
}

Private Void Form1_load ( Object Sender, eventargs E)
{
This . Txtsender. Text = " G_zhanhai@163.com " ;
}

Private Void Btnadd_click ( Object Sender, eventargs E)
{
If (Openfiledialog1.showdialog () = dialogresult. OK)
{
Txtadd. Text = openfiledialog1.filename;
}
}

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.