ASP. NET email attachment code

Source: Internet
Author: User
Tags mailmessage
First, I would like to mention SMTP (Oh, Master will skip this section !). SMTP is short for "Simple Mail Transfer
Protocol. It is a set of specifications used to transmit mails from the source address to the destination address. It is used to control the transfer mode of mails. SMTP protocol belongs
TCP/IP protocol cluster, which helps each computer locate the next destination when sending or transferring letters. The SMTP server is the mail sending server that follows the SMTP protocol. (Reference 1)
Next, we will briefly introduce the objects, attributes, and methods for sending emails in the namespace system. Web. Mail class library. (Reference 2)
(1) It has three classes: smtpmail, mailmessage, and mailattachment. These three objects are applied in the sample code in this article! (Reference 3)
1. mailmessage: Provides attributes and methods to create an email message object. (Provides properties and methods for constructing an e-mail message .)
2. mailattachments-Provides attributes and methods to create an email attachment object. (Provides properties and methods for constructing an E-Mail Attachment .)
3. smtpmail-provides properties and methods by using Windows 2000 cdosys
To send mail messages ). (Provides properties and methods for sending
Messages using the Collaboration Data Objects for Windows 2000 (cdosys)
Message component)
(2) attributes of each class.
1. Briefly introduce the attributes of smtpmail:
Smtpserver -- SMTP address.
2. Describes attributes of the mailmessage object.
From -- email sending Address
To -- Address of the email recipient
Subject -- mail title
Priority -- mail priority (valid values: High, low, normal)
Attachments -- returns a set that represents an attachment.
BCC-BCC address
CC-CC address
Body -- get or set the content of the email message
Bodyformat -- gets or sets the enumerated value of mailformat. This value specifies the message body email format (HTML format and text format)
Bodyencoding -- specify the message encoding method (mainly including base64 and uencode)
The other few are not important. Niu Base
The difference between BCC and CC is that BCC means that the recipient who receives emails in your group cannot see the number of people you sent and their email addresses, CC refers to the people who receive emails when sending emails to the group. You can view the number of people you sent and their email addresses.
(3) The send method of the smtpmail class, which is used to send emails. There are two overload methods.
1. smtpmail. Send ("mail address", "mail address", "mail title", "mail message content"
This method is very simple and is not suitable for sending emails with attachments.
2. smtpmail. Send (mailmessage)
This method is complex and flexible. It is suitable for sending attachments and can set various attribute values of the mailmessage object.

If we use ASP. NET to write a mail sending program, how should we first obtain SMTP. There are two methods: the first method calls the SMTP of a well-known mail service provider, such as the new
Lang, Sohu, Netease's free email SMTP; the second method is to install an SMTP virtual server, which is installed together When IIS is installed (the installation process is omitted ).
1. Use SMTP of well-known mail service providers to send emails in ASP. NET

First, you need to register a free email address on their mail site. Because you want to use SMTP from the mail service provider, they need to verify their identity, so as to avoid a large amount of spam. Assume that
A free email is registered on the Sina mail site (mail.sina.com.cn) with the username mysina and password 123. This account is fictitious. Please use your registered
Replace the user name and password. We learned from Sina's mail site that its SMTP address is smtp.sina.com.cn. We need to send mail to scucj@126.com (my mailbox address.
The core code for sending emails using ASP. NET (C #) is as follows:
// Start with the core code
Using system. Web. mail;
Mailmessage objmailmessage;
Mailattachment objmailattachment;
// Create an attachment object
Objmailattachment = new mailattachment ("D: // test.txt"); // attachments for sending emails
// Create an email message
Objmailmessage = new mailmessage ();
Objmailmessage. From = "mysina@sina.com"; // source email address
Objmailmessage. To = "scucj@126.com"; // destination email address, that is, send it to me
Objmailmessage. Subject = "mail title: Hello"; // The Mail title
Objmailmessage. Body = "mail sender content: test whether the email is successfully sent! "; // Content of the email
Objmailmessage. attachments. Add (objmailattachment); // attaches the attachment to the mail message object.
// Use Sina's SMTP to send emails. You need to use Microsoft. NET Framework SDK V1.1 and later versions.
// Basic Permissions
Objmailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1 ";
// User Name
Objmailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "mysina ";
// Password
Objmailmessage. Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword", "123 ";
// If the preceding three lines of code are not available, the following error message is displayed: the server rejects one or more recipient addresses. Server Response: 554: client host rejected: Access Denied
// SMTP address
Smtpmail. smtpserver = "smtp.sina.com.cn ";
// Start sending emails
Smtpmail. Send (objmailmessage );
// The core code ends.
This tutorial comes from 97xxoo tutorial Network (97 xxoo) to view the complete tutorial, please click: http://97xxoo.org/article/1/2008/20081111315.shtml

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.