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