ASP.net several methods of sending mail--practical tips

Source: Internet
Author: User
Tags decrypt mail account mailmessage server port smtpclient

MailMessage
provides properties and methods to create a mail message object. You can usually build your mail program by building the MailMessage object and then setting its properties.

commonly used properties:
from--Send the address of the message
to--accept the address of the message
Subject--Message title
Priority--Message priority (valid value is High,low,normal)
Attachments--Returns a collection that represents the attachment
Bcc--The BCC address
cc-CC address
Body-Gets or sets the content of the e-mail message
BodyFormat-Gets or sets the MailFormat enumeration value , this value specifies the format of the message body message (HTML format, text format)
Bodyencoding--The encoding encoding of the specified message (mainly Base64,uuencode)
Urlcontentbase : The URL encoding in an HTML-formatted message
Urlcontentlocation: Priority of message information (high, Medium,low)

SmtpMail
is responsible for the SMTP protocol that sends the message. Provides properties and methods to send mail messages by using a federated data object for the message component of Windows CDOSYS. The
SmtpMail class uses the Send method, which is designed to send messages with two overloaded methods.

1. Smtpmail.send ("Address of Mail Sent", "Address to accept Mail", "title of Message", "Content of mail Message") This method is simple enough to send messages with attachments.

2. Smtpmail.send (MailMessage) This method is complex, flexible, suitable for sending attachments, and can set various property values for MailMessage objects. If we use ASP.net to write a mail-sent program, then how do we get SMTP first? There are two ways: The first method calls SMTP of the currently well-known mail service providers, such as Sina, Sohu, NetEase's free e-mail SMTP; the second method is to install an SMTP virtual server, which is installed together with IIS.

MailAttachment
object classes related to message attachments, which are used primarily to provide properties and methods to create a message attachment object.
Constructors
Create an Attachment object
MailAttachment objmailattachment = new MailAttachment ("D:\test. TXT ");/send an attachment to a message
Call form

Copy Code code as follows:

MailMessage objmailmessage= New MailMessage ();
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object

Encapsulated message Sending class

Copy Code code as follows:

Using System;
Using System.Data;
Using System.Configuration;
Using System.Web;
Using System.Web.Security;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.WebControls.WebParts;
Using System.Web.UI.HtmlControls;
Using System.Net;
Using System.Net.Mail;
Using System.Text;

public class SendMail
... {
Public SendMail ()
... {
}
private string _host;
/**////<summary>
Server address
</summary>
public string Host
... {
Get ... {return _host;}
Set ... {_host = value;}
}
private int _port;
/**////<summary>
Server port
</summary>
public int Port
... {
Get ... {return _port;}
Set ... {_port = value;}
}
private string _smtpusername;
/**////<summary>
Sender Mailbox User Name
</summary>
public string Smtpusername
... {
Get ... {return _smtpusername;}
Set ... {_smtpusername = value;}
}
private string _sendemail;
/**////<summary>
Sender's mailbox account (only receive encrypted string, decrypt when sending mail)
</summary>
public string SendEmail
... {
Get
... {
return _sendemail;
}
Set ... {_sendemail = value;}
}
private string _replytoemail;
/**////<summary>
Reply Person e-mail account
</summary>
public string Replytoemail
... {
Get ... {return _replytoemail;}
Set ... {_replytoemail = value;}
}
private string _replyusername;
/**////<summary>
Reply Person User name
</summary>
public string Replyusername
... {
Get ... {return _replyusername;}
Set ... {_replyusername = value;}
}
private string _getemail;
/**////<summary>
Recipient mailbox Account
</summary>
public string Getemail
... {
Get ... {return _getemail;}
Set ... {_getemail = value;}
}
private string _smtppassword;
/**////<summary>
Sender's mailbox password (only receive encrypted string, decrypt when sending mail)
</summary>
public string Smtppassword
... {
Get ... {return _smtppassword;}
Set ... {_smtppassword = value;}
}
private string _content;
/**////<summary>
Message content
</summary>
public string Content
... {
Get ... {return _content;}
Set ... {_content = value;}
}
private string _title;
/**////<summary>
Message headers
</summary>
public string Title
... {
Get ... {return _title;}
Set ... {_title = value;}
}
Private string[] _cc = null;
/**////<summary>
CC E-Mail
</summary>
Public string[] CC
... {
Get ... {return _cc;}
Set ... {_cc = value;}
}
Private string[] _bcc = null;
/**////<summary>
The Secret Send mailbox
</summary>
Public string[] Bcc
... {
Get ... {return _BCC;}
Set ... {_bcc = value;}
}
/**////<summary>
Send mail
</summary>
<returns> return Success </returns>
public bool Send ()
... {
Try
... {
MailMessage objmailmessage;
Objmailmessage = new MailMessage (SendEmail, _getemail, _title, _content);
if (!string. IsNullOrEmpty (_replytoemail) &&!string. IsNullOrEmpty (_replyusername))
... {
mailaddress reply = new MailAddress (_replytoemail, _replyusername);
ObjMailMessage.ReplyToList.Add (reply);
}
objmailmessage.bodyencoding = encoding.getencoding (936);
Objmailmessage.isbodyhtml = true;
if (CC!= null && cc. Length > 0)
... {
foreach (String ccaddress in cc)
... {
OBJMAILMESSAGE.CC.ADD (New MailAddress (ccaddress));
}
}
if (Bcc!= null && bcc. Length > 0)
... {
foreach (String bccaddress in Bcc)
... {
OBJMAILMESSAGE.BCC.ADD (New MailAddress (bccaddress));
}
}
SmtpClient client = new SmtpClient (This._host, This._port);
if (! String.IsNullOrEmpty (this. Smtpusername) &&! String.IsNullOrEmpty (this. Smtppassword))
... {
Client. Credentials = new NetworkCredential (this. Smtpusername, this. Smtppassword);
}
Client. Enablessl = false;
Client. Send (Objmailmessage);
Objmailmessage.dispose ();
return true;
}
Catch
... {
return false;
}
}
}

Calling methods and steps:
1, create an object of the SendMail class, and then send the message to this object the necessary parameters,
2, call Send () method.

The attachment function of the message, you can also expand the SendMail class according to the above introduction. Here is not an example.

To send mail using SMTP from the native SMTP virtual server in asp.net
First, the SMTP configuration.
(1) Right-click "SMTP Virtual Server" Select "Properties"-> on the General tab, set IP address (P), I set 192.168.1.100.
(2) Select "Access" tab, click "Relay", select "only the following list" (the default is selected), click "Add", in "Single computer" add 192.168.1.100.
Tip, if not completed (2), there will be common one of the error prompts: The server rejected one or more recipient addresses. The server response is: 5.7.1 Unable to relay for scucj@126.com (friendship hint: error in the mail address is different) and then start the core code, in fact, and method (a) almost. The main difference with (a) is: 1. SMTP, 2.objmailmessage.from This method can be easily filled in, but (i) don't just fill out the core code that uses ASP.net (C #) to send messages is as follows:

Copy Code code as follows:

Core code Start
Using System.Web.Mail;
MailMessage objmailmessage;
MailAttachment objmailattachment;
Create an Attachment object
Objmailattachment = new MailAttachment ("D:\test.txt");//Send message attachment
Create a mail message
Objmailmessage = new MailMessage ();
Objmailmessage.from = "mysina@sina.com";//Source Email address
objmailmessage.to = "scucj@126.com"/destination email address, which is sent to me ha
Objmailmessage.subject = "Mail send title: Hello";//Send the title of the message
Objmailmessage.body = "Mail Send the content: test whether to send success!" ";/Send the contents of the message
OBJMAILMESSAGE.ATTACHMENTS.ADD (objmailattachment);//attach attachment to mail message object
SMTP address
Smtpmail.smtpserver = "192.168.1.100";
Start sending mail
Smtpmail.send (Objmailmessage);

The above two methods are introduced here. The easiest way to use the above method is to add a server button to the page and put the statement except the reference in the button click event. Of course, don't forget to put the quoted statement on top.

Related Article

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.