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;
}
}