Sending emails in. net

Source: Internet
Author: User
Tags mailmessage smtpclient

1. Add the following configuration information (host-SMTP service address, Port-port number, username-user name, and password-password) to <configuration> in Web. config. Please modify it yourself ).

< System.net >
< Mailsettings >

SMTP >
Network host =" smtpserver " port =" 25 " username =" uid " password =" PWD " />
SMTP >

</Mailsettings>
</System.net>

2. ASPX page htmlCode


< Table Border = "0" >
< Tr >
< TD >
Sender
</TD>
< TD >
< ASP: textbox Runat = "Server" ID = "Emailfrom" > </ASP: textbox>
</TD>
</Tr>
< Tr >
< TD >
Recipient
</TD>
< TD >
< ASP: textbox Runat = "Server" ID = "Emailto" > </ASP: textbox>
</TD>
</Tr>
< Tr >
< TD >
Topic
</TD>
< TD >
< ASP: textbox Runat = "Server" ID = "Subject" > </ASP: textbox>
</TD>
</Tr>
< Tr >
< TD >
Attachment
</TD>
< TD >
< ASP: fileupload ID = "Attachment" Runat = "Server" />
</TD>
</Tr>
< Tr >
< TD >
Content
</TD>
< TD >
< ASP: textbox Runat = "Server" ID = "Body" Textmode = "Multiline" Columns = "50" Rows = "10" > </ASP: textbox>
</TD>
</Tr>
< Tr >
< TD Colspan = "2" Align = "Center" >
< ASP: button Runat = "Server" ID = "Btnsend" Text = "Send" Onclick = "Btnsend_click" />
</TD>
</Tr>
</Table>

3. instantiate a mailmessage and set its attributes.

Mailmessage Mm =NewMailmessage (emailfrom. Text, emailto. Text );

Mm. Subject = subject. text;
Mm. Body = body. text;
// Html format
Mm. isbodyhtml =True;

// Add an attachment
Mm. attachments. Add (NewAttachment (attachment. postedfile. inputstream, attachment. filename ));

/** // * Other attributes such as CC and priority can be set in attributes of the mailmessage class */

4. instantiate an smtpclient and call its send method. The parameter is the mailmessage object.

Smtpclient SC = New Smtpclient ();

// Set SMTP in programming mode (without web. config)
// SC. Host = "";
// SC. Port = 25;
// SC. Credentials = new networkcredential ("username", "password ");

Try
{
SC. Send (mm );
Response. Write ("OK");
}
Catch (Exception ex)
{
// SMTP-related errors
If (EXIs Smtpexception)
{
// Ex. tostring ();
Response. Write ("SMTP sending failed");
}
Else
{
Response. Write (ex. tostring ());
}
}

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.