. Net C # Send email code

Source: Internet
Author: User
Tags mailmessage smtpclient

Reprinted from: http://blog.csdn.net/fhbcn/archive/2008/03/01/2137819.aspx

1. send an emailProgram(Use the class under system. Web. Mail)

  Public     Static     Bool  Sendmail (  String  Mailhead,  String  Mailbody,  String Emailadd)
...{
If (Emailadd ! = "" )
...{
// Create a mailmessage object
System. Web. Mail. mailmessage mailmsg = New System. Web. Mail. mailmessage ();
// Set the recipient's email address
Mailmsg. = Emailadd;
// Set the sender's email address
Mailmsg. From = " Wicresoft@126.com " ;
// Set Email Subject
Mailmsg. Subject = Mailhead;
// Set email content
Mailmsg. bodyformat = System. Web. Mail. mailformat. html;

If (Mailbody ! = "" )
...{
Mailmsg. Body = Mailbody;

// Set to support server Verification
Mailmsg. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/smtpauthenticate " , " 1 " );
// Set User Name
Mailmsg. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendusername " , " Wicresoft " );
// Set User Password
Mailmsg. Fields. Add ( " Http://schemas.microsoft.com/cdo/configuration/sendpassword " , " Wangwei " );
Try
...{
// Set email sending server 202.108.5.142
System. Web. Mail. smtpmail. smtpserver = " 202.108.5.142 " ;
// Send email
System. Web. Mail. smtpmail. Send (mailmsg );
Return True ;
}
Catch
...{
Return False ;
}
}
Else
Return False ;
}
Else
Return False ;
}


This article is from the csdn blog. For more information, see http: // Blog.csdn.net/fhbcn/archive/2008/03/01/4107819.aspx

 

2. net2.0 provides an alternative (using system. NW)

 

  Using  System;
Using System. Collections. Generic;
Using System. text;

Using System. net. mail;

Namespace Mailsender
...{
Class Program
...{
Static String Strhost = String . Empty;
Static String Straccount = String . Empty;
Static String Strpwd = String . Empty;
Static String Strfrom = String . Empty;

Static Void Main ( String [] ARGs)
...{
Strhost = " Smtp.163.com " ; // Stmp server address
Straccount = " Jailu " ; // SMTP service account
Strpwd = " 123456789 " ; // SMTP service Password
Strfrom = " Jailu@163.com " ; // Sender's email address

Console. writeline (Sendmail ( " Jailu@qq.com " , " This is a test email. " , " This is the body of a test email. " ) ? " Success " : " Unsuccess " );

Console. Readline ();
}

/**/ /// <Summary>
/// Send email
/// </Summary>
/// <Param name = "to"> Recipient email address </Param>
/// <Param name = "title"> Email Subject </Param>
/// <Param name = "content"> Body content </Param>
/// <Returns> </returns>
Static Bool Sendmail ( String To, String Title, String Content)
...{
Smtpclient = New Smtpclient ();
Smtpclient. deliverymethod = Smtpdeliverymethod. Network; // Specify email sending Method
Smtpclient. Host = Strhost ;; // SMTP Server
Smtpclient. Credentials = New System. net. networkcredential (straccount, strpwd ); // Username and password

Mailmessage = New Mailmessage (strfrom, );
Mailmessage. Subject = Title; // Topic
Mailmessage. Body = Content; // Content
Mailmessage. bodyencoding = System. Text. encoding. utf8; // Body code
Mailmessage. isbodyhtml = True ; // Set to HTML Format
Mailmessage. Priority = Mailpriority. High; // Priority

Try
...{
Smtpclient. Send (mailmessage );
Return True ;
}
Catch
...{
Return False ;
}
}
}
}


This article is from the csdn blog. For more information, see http: // Blog.csdn.net/fhbcn/archive/2008/03/01/4107819.aspx

 

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.