Implement mail sending in ASPnet

Source: Internet
Author: User
Tags mailmessage smtpclient

System. net. Mail is used to send emails to simple SMTP servers.

1. Modify the webconfig File

<System.net>
<Mailsettings>
<SMTP>
<Network host = "SMTP host" Port = "25" username = "username" Password = "password"/>
</SMTP>
</Mailsettings>

</System.net>

2. click the button to send emails.

Code

Using System;
Using System. Collections. Generic;
Using System. LINQ;
Using System. Web;
Using System. Web. UI;
Using System. Web. UI. webcontrols;
Using System. net. mail;

Public Partial Class_ Default: system. Web. UI. Page
{
Protected VoidPage_load (ObjectSender, eventargs E)
{

}

Public   Void Sendemail ( String From, String To, String BCC, String CC, String Subject, String Body, String Mattachment)
{
Mailmessage newemail =   New Mailmessage ();
// The address of the email sender or recipient.
// Mailaddress from = new mailaddress ("ben@contoso.com", "Ben Miller ");
Newemail. From =   New Mailaddress (from, from );

Newemail. to. Add ( New Mailaddress ());
// You can also add an address to the Collection newemail. To = new mailaddresscollection (). Add (New mailaddress ());
Newemail. bcc. Add ( New Mailaddress (BCC ));
If ( ! String. isnullorempty (CC ))
{
Newemail. CC. Add ( New Mailaddress (CC ));
}
Newemail. Subject = Subject;
Newemail. Body = Body;
Attachment msgattach =   New Attachment (mattachment ));
Newemail. attachments. Add (msgattach );
Newemail. isbodyhtml =   True ;
Newemail. Priority = Mailpriority. normal;

Smtpclient msmtpclient= NewSmtpclient ();
Msmtpclient. Send (newemail );
}

Protected   Void Button#click ( Object Sender, eventargs E)
{
String From =   " Sender " ;
String To =   " Recipient " ;
String BCC =   " BCC) " ;
String CC =   " CC) " ;
String Subject =   " Topic " ;
String Body =   " Email body " ;
String Mattachment =   " Attachment " ;
Sendemail (from, to, BCC, CC, subject, body, mattachment );
}
}

 

 

 

 

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.