Email sending mainly uses mailmessage and smtpclient.
Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. net. mail;
/// <Summary>
/// Email abstract description
/// </Summary>
Public Class Email
{
Public Email ()
{
//
// Todo: add the constructor logic here
//
}
/// <Summary>
/// Initialize email information
/// </Summary>
/// <Param name = "Address"> </param>
/// <Returns> </returns>
Public Mailmessage initmailmessage ( String Address)
{
Mailmessage mail = New Mailmessage ();
Mail. From = New Mailaddress (Address ); // Sender
Mail. to. Add (Address ); // Recipient
Mail. Subject = " Test email sending " ; // Topic
Mail. Body = " Email test content " ; // Content
//Encoding format of the subject and body
Mail. subjectencoding = system. Text. encoding. utf8; mail. bodyencoding = system. Text. encoding. utf8;
Mail. isbodyhtml =True;//The email body can be HTML encoded.
Mail. Priority = mailpriority. normal;//Priority
//BCC: CC the email to a person other than the recipient. All recipients cannot see the BCC address.
Mail. bcc. Add ("1052647407@qq.com");
//CC-CC refers to CC a letter to a person other than the recipient. All recipients can see at the CC address to whom the letter is copied.
Mail. CC. Add ("1052647407@qq.com");
Mail. attachments. Add (NewAttachment ("D: \ 1.doc"));//Add attachment
ReturnMail;
}
/// <Summary>
/// Send email
/// </Summary>
/// <Returns> </returns>
Public Bool Sendemail ()
{
Smtpclient client =New Smtpclient ();
//Obtain or set the creden。 used to authenticate the sender.
Client. Credentials =NewSystem. net. networkcredential ("1052647407@qq.com","Password xxxx");
// Encrypted by SSL (Secure Sockets Layer), the port number of the 163 mailbox SSL protocol is 465/994, and the port is 25 When SSL is disabled,
// The SSL protocol port number of QQ mail is 465 or 587. When SSL is disabled, port 25 is also used. However, emails sent after SSL encryption fail. I do not know the specific cause.
// Client. enablessl = true;
Client. Port = 25 ; // Port Number
Client. Host = " Smtp.qq.com " ; // Obtains or sets the name or IP address of the host used for SMTP transactions.
Try
{
Client. Send (initmailmessage ( " 1052647407@qq.com " ));
Return True ;
}
Catch(System. net. Mail. smtpexception ex)
{
Return False;
}
}
}