Asp.net email sending

Source: Internet
Author: User
Tags mailmessage smtpclient

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

}
}

 

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.