. Net password retrieval

Source: Internet
Author: User
Tags mailmessage smtpclient

Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Net. Mail;
Using System. IO;

/// <Summary>
/// Summary of sendEmail
/// </Summary>
Public static class sendEmail
{
Static sendEmail ()
{
//
// TODO: add the constructor logic here
//
}


/// <Summary>
/// Email sending program
/// </Summary>
/// <Param name = "from"> sender's email address </param>
/// <Param name = "fromname"> sender display name </param>
/// <Param name = "to"> who to send (email address) </param>
/// <Param name = "subject"> title </param>
/// <Param name = "body"> content </param>
/// <Param name = "username"> email logon name </param>
/// <Param name = "password"> email password </param>
/// <Param name = "server"> email server smtp server address </param>
/// <Param name = "fujiian"> attachment </param>
/// <Returns> send OK </returns>
/// Call method SendMail ("abc@126.com", "someone", "CBA @126.com", "hello", "my test mail", "Mailbox login name", "Mailbox password ", "smtp.126.com ","");
///
Public static string SendMail (string from, string fromname, string to, string subject, string body, string username, string password, string server, string fujiian)
{
Try
{
// Mail sending class
MailMessage mail = new MailMessage ();
// Who sent the email
Mail. From = new MailAddress (from, fromname );
// Send
Mail. To. Add ();
// Title
Mail. Subject = subject;
// Content Encoding
Mail. BodyEncoding = Encoding. Default;
// Sending priority
Mail. Priority = MailPriority. High;
// Email content
Mail. Body = body;
// Whether to send messages in HTML Format
Mail. IsBodyHtml = true;
// Attachment
If (fujian. Length> 0)
{
Mail. Attachments. Add (new Attachment (Fuji ));
}
// Email server and Port
SmtpClient smtp = new SmtpClient (server, 25 );
Smtp. usedefacrecredentials = true;
// Specify the sending Method
Smtp. DeliveryMethod = SmtpDeliveryMethod. Network;
// Specify the logon name and password
Smtp. Credentials = new System. Net. NetworkCredential (username, password );

// Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); // basic authentication
// Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", username); // set your username here
// Mail. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", password); // set your password here

// Timeout
Smtp. EnableSsl = false;
Smtp. Timeout = 10000;
Smtp. Send (mail );
Return "successfully sent, please check ";
}
Catch (Exception exp)
{
Return exp. Message;
}
}

// Read the HTML of the specified URL for later sending the webpage
Public static string ScreenScrapeHtml (string url)
{

// Read stream and prevent garbled characters on Chinese pages
StreamReader reader = new StreamReader (System. Net. WebRequest. Create (url). GetResponse (). GetResponseStream (), System. Text. Encoding. UTF8 );
String str = reader. ReadToEnd ();
Reader. Close ();
Return str;
}

/// <Summary>
/// Send an email
/// </Summary>
/// <Param name = "server"> smtp address </param>
/// <Param name = "username"> User name </param>
/// <Param name = "password"> password </param>
/// <Param name = "from"> sender address </param>
/// <Param name = "to"> recipient's address </param>
/// <Param name = "subject"> email title </param>
/// <Param name = "body"> email body </param>
/// <Param name = "IsHtml"> whether the email is in HTML format </param>
Public static string SendMail (string from, string to, string subject, string body, string server, string username, string password, bool IsHtml)
{
Try
{
// Set SMTP verification. The default port is 25. If you need other settings, modify
SmtpClient mailClient = new SmtpClient (server, 25 );


// Specify how to send an email.
// Network email is sent to the SMTP server over the Network.
// PickupDirectoryFromIis copies the email to the selected directory and then transmits it through the local Internet Information Service (IIS.
// SpecifiedPickupDirectory copies the email to the directory specified by the SmtpClient. PickupDirectoryLocation attribute, which is then transmitted by an external application.

MailClient. DeliveryMethod = System. Net. Mail. SmtpDeliveryMethod. Network;


// Create an email object
MailMessage mailMessage = new MailMessage (from, to, subject, body );

// Define the email body and the subject encoding method
MailMessage. BodyEncoding = System. Text. Encoding. GetEncoding ("gb2312 ");
MailMessage. SubjectEncoding = System. Text. Encoding. GetEncoding ("gb2312 ");
// MailMessage. BodyEncoding = Encoding. Default;
// Obtain or set a value that indicates whether the email body is HTML
MailMessage. IsBodyHtml = IsHtml;

// Specify the mail priority
MailMessage. Priority = MailPriority. High;

 

// Sender authentication; otherwise, 163 cannot be sent
// Indicates that the default credential of the current login user is used for identity authentication and includes the user name and password
MailClient. usedefacrecredentials = true;
MailClient. Credentials = new System. Net. NetworkCredential (username, password );

// Send
MailClient. Send (mailMessage );
Return "sent successfully ";
}
Catch (Exception exp)
{
Return exp. Message;
}
}

// Send plaintxt
Public static void SendText (string from, string to, string subject, string body, string server, string username, string password)
{
SendMail (from, to, subject, body, server, username, password, false );
}

// Send HTML content
Public static string SendHtml (string from, string to, string subject, string body, string server, string username, string password)
{
Return SendMail (from, to, subject, body, server, username, password, true );
}

// Send a webpage
Public static string SendWebUrl (string from, string to, string subject, string server, string username, string password, string url)
{
// Send a webpage
Return SendHtml (from, to, subject, ScreenScrapeHtml (url), server, username, password );

}
}

 

 

 

 

Background call example:

Response. write ("send attachment:" + sendEmail. sendMail ("shaxiaozier@sohu.com", "someone", "hou@sina.com", "hello", "my test mail", "shaxiaozier@sohu.com ", "*********", "smtp.sohu.com", Server. mapPath ("~ /Fujiian/wap.doc ")));


Response. write ("the email body is in HTML Format:" + sendEmail. sendMail ("shaxiaozier@sohu.com", "hou@sina.com", "theme", "body <br/> another line", "smtp.sohu.com", "shaxiaoier ", "*********", false ));


Response. write ("the email body is not in HTML Format:" + sendEmail. sendMail ("shaxiaozier@sohu.com", "hou@sina.com", "theme", "body <br/> another line", "smtp.sohu.com", "shaxiaoier ", "*********", true ));


Response. write ("sending webpage:" + sendEmail. sendWebUrl ("shaxiaozier@sohu.com", "hou@sina.com", "zhuiti", "smtp.sohu.com", "shaxiaoier", "*********", "http://www.baidu.com "));



Link: http://blog.csdn.net/mypc2010/article/details/7836961

Related Article

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.