Asp.net Email transmission)

Source: Internet
Author: User
As required by the project, I used to retrieve the user's forgot password through password issues. As a result, the customer needs to retrieve the password automatically sent by email and use its own email server. So what should I do? I still need to leave an email server interface, which is difficult to do. I am so overwhelmed. I checked it online for one afternoon and tested how to automatically send emails one afternoon, I have used almost four methods and tried again. I configured them in Webconfig or used popular Jmail. I don't know if this is an interface. Well, I don't know how to configure it for the mail server. I did not configure it for it anyway. Well, if a friend who has configured the mail server saw this article, I hope you can give me some tips and give me some advice on how to change my email server and how to configure it.

The following describes my summary:
Method 1:

Using System. Web. Mail;

Public void sendMail ()
{
MailMessage mail1 = new MailMessage ();
Mail1.Body = "body here ";
Mail1.From = "xxx@xxx.com ";
Mail1.To = "yyy@yyy.com ";
Mail1.Fields. Add ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", 1 );
Mail1.Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendusername", "xxx@xxx.com ");
Mail1.Fields. Add ("http://schemas.microsoft.com/cdo/configuration/sendpassword ","********");
SmtpMail. SmtpServer = "mail.xxx.com ";
SmtpMail. Send (mail1 );
}

The Fields added above are used for SMTP mail authentication. If your mail server does not require authentication, You can omit these statements.

Method 2:

Using System. Net. Mail;

Method 1: send an email to a single address without setting the web. config file
Public void SendMail ()
{
String mailto = "to@company.com ";
String mailfrom = "from@company.com ";
System. Net. NetworkCredential credential = new System. Net. NetworkCredential ("from_username", "from_password ");
SmtpClient smtp = new SmtpClient ("smtp.company.com ");
Smtp. Credentials = credential;

MailMessage message = new MailMessage (mailfrom, mailto );
Message. SubjectEncoding = System. Text. Encoding. UTF8;
Message. BodyEncoding = System. Text. Encoding. UTF8;
Message. Subject = "subject here ";
Message. Body = "body here ";
Smtp. Send (message );
Message. Dispose ();
}

Method 2: send an email to a single address and set the web. config file
Public void SendMail ()
{
String mailto = "to@company.com ";
String mailfrom = "from@company.com ";

MailMessage message = new MailMessage (mailfrom, mailto );
Message. SubjectEncoding = System. Text. Encoding. UTF8;
Message. BodyEncoding = System. Text. Encoding. UTF8;
Message. Subject = "subject here ";
Message. Body = "body here ";
Smtp. Send (message );
Message. Dispose ();
}

Add the following to web. config:
<System.net>
<MailSettings>
<Smtp from = "from@company.com">
<Network host = "smtp.company.com" port = "25" userName = "from_username" password = "from_password"/>
</Smtp>
</MailSettings>
</System.net>

Method 3: send a group email and set the web. config file.
Public void SendEmail ()
{
String mailto = "to1@company.com, to2@company.com ";
String title = "mail title here ";
String content = "mail content here ";

SmtpClient smtp = new SmtpClient ();
MailMessage message = new MailMessage ();
MailAddressCollection address = new MailAddressCollection ();
String [] mailtos = mailto. Split (',');
For (int I = 0; I <mailtos. Length; I ++)
{
Address. Add (mailtos [I]);
}
Foreach (MailAddress add in address)
{
Message. To. Add (add );
}
Message. SubjectEncoding = System. Text. Encoding. UTF8;
Message. BodyEncoding = System. Text. Encoding. UTF8;
Message. Subject = title;
Message. Body = content;
Smtp. Send (message );
Message. Dispose ();
Address. Clear ();
}

Add the following to web. config:
<System.net>
<MailSettings>
<Smtp from = "from@company.com">
<Network host = "smtp.company.com" port = "25" userName = "from_username" password = "from_password"/>
</Smtp>
</MailSettings>
</System.net>

Using the above method, if the computer running the mail program is installed with anti-virus software such as mail monitoring, there will be a warning of failure, but it has actually been sent successfully. The solution is to disable the monitoring function of anti-virus software.

Add the following namespace:

Using System. Net;
Using System. Net. Cache;
Using System. Net. Mail;
Using System. Net. Configuration;
Using System. Web. Configuration;

Applicable framework: asp.net framework 2.0/. net framework3.0/. net framework3.5

Set the network connection of. net framework in web. config

 

XML/HTML code
  1. <System.net>
  2. </System.net>

Configure stmp mail sending as follows:

 

XML/HTML code
  1. // Parent element: configuration (configuration of all namespaces is provided)
  2. <System.net>
  3. // You can add the following element settings.
  4. // AuthenticationModules: Set the module used to verify WEB requests
  5. // ConnectionManagement: sets the maximum number of connections to the WEB server.
  6. // DefaultProxy: sets the http proxy server.
  7. // MailSettings: Configure smtp
  8. // RequestCaching: controls the network request Cache Mechanism
  9. // Settings: Configure basic network options for System. NET
  10. // <WebRequestModules> element (network settings): Specify the module's request information from the WEB server.
  11. <MailSettings>
  12. // Set the mail sending method in deliveryMethod, which is in the network format
  13. <Smtp deliveryMethod = "Network" from = "xxx@yyy.com">
  14. // Host email sending server
  15. // UserName used for identity authentication when sending an email
  16. // Password for verification as follows
  17. <Network host = "smtp.yyy.com" userName = "xxx@yyy.com" password = "zzzzzzz"/>
  18. </Smtp>
  19. </MailSettings>
  20. </System.net>

 

Obtain smtp configuration in web. config programmatically

NetSectionGroup class

Namespace
System. Net. Configuration: Provides the application with a class for programmatically accessing and updating the settings in the Configuration file System. Net namespace.
Assembly
System

Definition:
Public sealed class NetSectionGroup: ConfigurationSectionGroup

Note:
This class provides program-based access to information stored in the configuration file.
This class corresponds to the system.net element (network settings) in the network settings document.
This section provides configuration settings for the following namespaces:
System. Net
System. Net. Cache
System. Net. Mail

Read configuration/system.net/mailsettings/stmpconfiguration in the program

 

C # code
  1. // NetSectionGroup has already been introduced above
  2. // GetSectionGroup obtains the configuration of system.net from the specified configuration file
  3. // WebConfigurationManager. OpenWebConfiguration
  4. // Open web. config in the root directory of the application to generate the System. Configuration. Configuration object instance.
  5. SmtpSection cfg = NetSectionGroup. GetSectionGroup (WebConfigurationManager. OpenWebConfiguration ("~ /Web. config "). MailSettings. Smtp;

 

Send email

C # code
  1. Try {
  2. // Instantiate an email message object
  3. MailMessage email = new MailMessage (cfg. From, mailto );
  4. Email. IsBodyHtml = true;
  5. Email. Body = "content of the email to be sent. The preceding settings support html content. In addition, you can specify the email. BodyEncoding attribute to set the encoding of the email content ";
  6. Email. Subject = "email Subject, you can specify the email. SubjectEncoding attribute to set the email Subject encoding ";
  7. // Instantiate an smtp client object for sending emails
  8. System. Net. Mail. SmtpClient stmp = new SmtpClient (cfg. Network. Host );
  9. // Set whether to send the message and whether authentication is required. If the following credentials is not required
  10. Stmp. usedefacrecredentials = true;
  11. Stmp. Credentials = new System. Net. NetworkCredential (cfg. Network. UserName, cfg. Network. Password );
  12. // Send an email
  13. Stmp. Send (email );
  14. }
  15. Catch (Exception ex)
  16. {

[Transfer]: http://www.cnblogs.com/matrix/archive/2004/05/20/10495.aspx

 

Currently, most emails require STMP authentication,
. NET
System. Web. Util
System. Web. Mail
No.
I wrote this article to help you develop projects.

Find a component namedJMail
You can find it online, download it, and install it.
Reference jmail. dll to the Project

 

Private void button#click (object sender, System. EventArgs e)
{

Jmail. Message Jmail = new jmail. Message ();

DateTime t = DateTime. Now;
String Subject = "From EMail. net ";
String body = "12:12:12 ";
String FromEmail = "ljt21@163.com ";
String ToEmail = "xiao-maolover@163.com ";
// Silent attribute: If set to true, JMail will not throw an exception error. JMail. Send () will return true or false Based on the operation results
Jmail. Silent = true;
// Logs created by Jmail, provided that the loging attribute is set to true
Jmail. Logging = true;
// Character Set, default: "US-ASCII"
Jmail. Charset = "GB2312 ";
// Contentype of a letter. The default value is "text/plain"): If you send an email in HTML format, change it to "text/html.
Jmail. ContentType = "text/html ";
// Add recipient
Jmail. AddRecipient (ToEmail ,"","");
Jmail. From = FromEmail;
// Sender's email user name
Jmail. MailServerUserName = "ljt21 ";
// Sender's email password
Jmail. MailServerPassWord = "****";
// Set the mail title
Jmail. Subject = Subject;
// Add attachments to the email. (If there are multiple attachments, you can add Jmail. AddAttachment ("c: \ test.jpg", true, null. [Note]: add an attachment to delete the above Jmail. ContentType = "text/html. Otherwise, garbled characters will appear in the email.
Jmail. AddAttachment ("c: \ test.jpg", true, null );
// Email content
Jmail. Body = body + t. ToString ();
// Jmail sending Method
Jmail. Send ("smtp.163.com", false );
Jmail. Close ();
}
So OK ..!
Tested, no problem.

Asp.net automatic mail sending Method
Today, there is a module that requires the automatic email sending function, so you can simply write one and record it as accumulation.

1. Configure the web. config file first:

<System.net>
<MailSettings>
<Smtp from = "Emailname">
<Network host = "smtp.163.com" userName = "Emailname" password = "Emailpassword"
Port = "25" defaultCredentials = "false"/>
</Smtp>
</MailSettings>
</System.net>

2. Then write the email sending function:

//// <Summary>
/// Email sending method (with attachment)
/// </Summary>
/// <Param name = "mailto"> recipient address. Such as: receiver@163.com </param>
/// <Param name = "mailsubject"> email title </param>
/// <Param name = "mailbody"> email body </param>
/// <Param name = "mailFrom"> email recipient address. Such as: sender@163.com </param>
/// <Param name = "list"> attachment path </param>
/// <Returns> </returns>
Public bool MySendMail (string mailto, string mailsubject, string mailbody, string mailFrom, ArrayList list)
{
Try
{
// Email recipient address
System. Net. Mail. MailAddress from = new System. Net. Mail. MailAddress (mailFrom );
// Such as test@163.com, preliminary test, can not use the test@sina.com, with 163 mail server, you must use 163 mailbox User Name
// Recipient address
System. Net. Mail. MailAddress to = new System. Net. Mail. MailAddress (mailto); // such as test@tom.com
System. Net. Mail. MailMessage mail = new System. Net. Mail. MailMessage (from, );
Mail. Subject = mailsubject;
Mail. Body = mailbody;
// Set the server as follows
System. Net. Mail. SmtpClient mySmth = new System. Net. Mail. SmtpClient ();
// Add attachments as follows
Int count = list. Count;
For (int I = 0; I <count; I ++)
{
System. Net. Mail. Attachment data = new System. Net. Mail. Attachment (list [I]. ToString ());
Mail. Attachments. Add (data );
}
MySmth. Send (mail );
Mail. Dispose ();
Return true;
}
Catch
{
Return false;
}
}

3. Finally, call the function:

// Automatically send the email
String mailSubject = "Member registration confirmation letter ";
String mailBody = "body content. ";
String mailFrom = ConfigurationManager. receivetask[ "SendMail"];
ArrayList List = new ArrayList ();
List. Add (Server. MapPath (ConfigurationManager. receivetask[ "SendMailText"]);
If (MySendMail(this.txt Email. Text, mailSubject, mailBody, mailFrom, List ))
{
...
// The message is sent successfully and processed accordingly.
}
Else
{
...
// Failed to send and Process
Return;
}

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.