Different email sending solutions in. Net Environment

Source: Internet
Author: User
Tags getstream mailmessage

From: http://dev.yesky.com/msdn/241/3417241.shtml

1. Use the mail provided by Outlook

For example, I have met u8 production and manufacturing (demo version). The mail function is to call the ActiveX component of outlook.

Advantage: simple development

Disadvantages: relying on outlook components, SMTP mail service

Email sentCodeAs follows:

The following is a reference clip:
Private sub send ()
Dim outobj as new outlook. Application ()
Dim item as outlook. mailitemclass
Item = outobj. createitem (0)
Item. To = "lihonggen0@163.com"
Item. Subject = "hello"
Item. Body = "hell"
Item. attachments. Add ("C: \ abc.txt ")
Item. Send ()
End sub

2. Web development: reference the system. Web. Mail class in ASP. NET.

The system. Web. Mail namespace contains a class that allows you to construct and send messages using the cdosys (collaborative data object for Windows 2000) Message component. Mail messages are sent through the SMTP mail service built in Microsoft Windows 2000 or any SMTP server. Classes in this namespace can be stored in ASP. NET or any Managed Application.Program

Mailattachment provides attributes and methods for constructing email attachments.

Mailmessage provides the attributes and methods used to construct an email.

Smtpmail provides the attributes and methods used to send messages using the collaborative data object (cdosys) Message component of Windows 2000.

Emails can be sent through the built-in SMTP mail service in Microsoft Windows 2000 or any SMTP server. The types in the system. Web. Mail namespace can be used in ASP. NET or any hosting application.

For SMTP server settings, some free mail providers no longer provide SMTP services for all emails. When sending emails, you need to verify the user information and consider SMTP User authentication issues.

If the SMTP server is on a local computer, the sending speed is very fast and you don't have to worry about it. If it's not a local server, it's best not to use this too much for sending. First, it's a speed problem, the second is to send too many emails, and the SMTP server may consider it as spam and refuse services.

The Code is as follows:

The following is a reference clip:
Private sub button#click (byval sender as system. Object, byval e as system. eventargs) handles button1.click
Dim mailobj as new mailmessage ()
Dim SMTP as smtpmail
'Define the SMTP server name
SMTP. smtpserver = "smtp.xxx.com"
'Define the mail sending Address
Mailobj. From = "lihonggen@XXX.com"
'Define the mail Receiving address
Mailobj. To = "AAA@XXX.com"
'Defines the mail's dark sending Address
Mailobj. bcc = "aa@sina.com"
'Define the CC address of the mail
Mailobj. Cc = "aaa@sina.com"
'Define the subject of the email.
Mailobj. Subject = "topic"
'Define the subject of the mail
Mailobj. Body = "email subject! "
'Emails are sent in HTML format.
Mailobj. bodyformat = mailformat. html
'Define the mail's limited level, which is set to high here
Mailobj. Priority = mailpriority. High
'Attach an attachment to the sent Email
Mailobj. attachments. Add (New mailattachment ("C: \ aa.doc "))
SMTP. Send (mailobj)
End sub

3. Use System. net. sockets in VB. NET or C # To develop Windows applications

It is also based on SMTP protocol

1. SMTP Protocol Introduction

1. The client establishes a TCP/IP connection through port 25 of the server.

Server: 220 server.com Simple Mail Transfer Service ready

2. The client uses the "HELO" command to identify the sender

Client: HELO server.com

Server: 250 server.com

3. The client sends the MAIL command, and the server uses OK as the response to indicate that it is ready to receive

Client: mail from: A@ B .com 〉

Server: 250 OK

4. The client sends the RCPT command to identify the recipient, and the server responds to the request to accept the email for the recipient.

Client: rcpt to: c@d.com> 〉

Server: 250 OK

5. send an email with the command data after the negotiation ends.

Client: Data

Server: 354 start mail input: end with <CRLF >. <CRLF> 〉

6. The client ends the input content and sends it out.

Client: Subject: <CRLF> 〉

Content <CRLF> 〉

. <CRLF> 〉

7. The client exits with the quit command.

Client: Quit

Server Side: 250 server.com closing transmission channel

Advantage: You can develop your own components on this basis. With sockets, we can develop network programming.

Disadvantage: the number of programs is relatively large,

The code for sending an email is as follows:

The following is a reference clip:
Dim senddata as string
Dim szdata as byte ()
Dim CRLF as string
CRLF = "\ r \ n"
'Create a connection to port 25 of the server
Dim smtpserv as new tcpclient (txtsmtp. Text, 25)
Lstlog. Items. Clear ()
'Display initial server information
Dim. netstrm as. networkstream
. Netstrm = smtpserv. getstream ()
Dim rdstrm as new streamreader (smtpserv. getstream ())
If rdstrm. Readline () <> "" Then lstlog. Items. Add (rdstrm. Readline ())
'
Senddata = "HELO server" + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Indicates the sender
Senddata = "mail from:" + "<" + txtfrom. Text + ">" + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Indicates the recipient
Senddata = "rcpt to:" + "<" + txw.text + ">" + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Prepare the sent content
Senddata = "data" + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Send topic
Senddata = "Subject:" + txtsub. Text + CRLF
'Send content
Senddata = senddata + txtmsg. Text + CRLF
'End sending
Senddata = senddata + "." + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Exit
Senddata = "quit" + CRLF
Szdata = system. Text. encoding. ASCII. getbytes (senddata. tochararray ())
. Netstrm. Write (szdata, 0, szdata. length)
Lstlog. Items. Add (rdstrm. Readline ())
'Close the connection
. Netstrm. Close ()
Rdstrm. Close ()
Lstlog. Items. Add ("Connection closed ")
Lstlog. Items. Add ("sent successfully ")
You can also refer to: use C # To create the mail sending component (SMTP)

4. The basic sending component cdonts provided by IIS SMTP

You don't have to download it specially. Microsoft already provides this component. As long as 2000 is installed, nt smtp will have it.

Advantage: components are provided by the operating system.

Disadvantages: Poor functionality and poor scalability

Mymail = Createobject ("cdonts. newmail ")

Mymail. From = *** 'mail sender's mailbox

Mymail. To = *** 'mail recipient's mailbox

Mymail. Cc = *** CC

Mymail. bcc = *** 'bcc sent

Mymail. Subject = *** 'subject of the letter

Mymail. Body = *** 'mail body

'Set the priority. The value 0 indicates not important. The value 1 indicates normal. The value 2 indicates important.

Mymail. Importance = 2

Mymail. Send ()

Mymail = nothing

5. Use the jmail component

Jmail has the following features:

(1) attachments can be sent;

(2) The detailed log capability allows you to view the problem;

(3) set the mail sending priority;

(4) supports sending emails in multiple formats, such as HTML or TXT. This is a free component.

(5) BCC/CC/emergency mail sending capability;

(6) The most important thing is the free components, which are worth using because they do not have to be paid.

Website: http://www.dimac.net/. the current region is 4.3.

Common attributes of the jmail component:

Body

Logging call log records for debugging

Priority email priority, from 1 to 5

Sender sender

Serveraddress SMTP Server IP address or name

Subject subject

Common Methods for jmail components:

Addattachment specifies the attachment file

Add addreiient to a recipient

Addrecipientbcc hidden copy CC, only known to the sender and BCC recipients.

Addrecipientcc copy CC

Execute sends an email

After understanding the necessary attributes and methods, the remaining part receives the email entered by the user and sends it as a parameter to the addrecipient method. Then, fill in other attributes as needed, and finally send the email using the execute method. For example:

The following is a reference clip:
Dim jmail
Jmail = server. Createobject ("jmail. smtpmail ")
Jmail. Logging = true
Jmail. serveraddress = "202.96.144.48"
Jmail. Sender = "lihonggen0@163.com"
Jmail. Subject = "subject ."
Jmail. Body = "body ."
Jmail. addrecipient ("bbbb@163.com ")
Jmail. addattachment ("C: \ go.gif ")
Jmail. Priority = 3
Jmail. Execute ()
Jmail = nothing

Summary: The options are determined based on the purpose and needs of the program. Several solutions are provided in this article for your reference. For example, u8 is integrated into your own software by calling the outlook component. The management system I have compiled has the same powerful function as the self-Writing component (SMTP), but coding takes a long time!

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.